L i2ddlmZddlZddlmZddlmZerddlmZGddZ Gd d e Z Gd d e Z Gd de Z y)) annotationsN)Queue) TYPE_CHECKING) AutoTokenizerceZdZdZdZdZy) BaseStreamerzG Base class from which `.generate()` streamers should inherit. ct)z;Function that is called by `.generate()` to push new tokensNotImplementedErrorselfvalues g/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/generation/streamers.pyputzBaseStreamer.put !##ct)zHFunction that is called by `.generate()` to signal the end of generationr rs rendzBaseStreamer.end$rrN)__name__ __module__ __qualname____doc__rrrrr r s$$rr c6eZdZdZdd dZdZdZdd dZdZy) TextStreamera) Simple text streamer that prints the token(s) to stdout as soon as entire words are formed. The API for the streamer classes is still under development and may change in the future. Parameters: tokenizer (`AutoTokenizer`): The tokenized used to decode the tokens. skip_prompt (`bool`, *optional*, defaults to `False`): Whether to skip the prompt to `.generate()` or not. Useful e.g. for chatbots. decode_kwargs (`dict`, *optional*): Additional keyword arguments to pass to the tokenizer's `decode` method. Examples: ```python >>> from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer >>> tok = AutoTokenizer.from_pretrained("openai-community/gpt2") >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2") >>> inputs = tok(["An increasing sequence: one,"], return_tensors="pt") >>> streamer = TextStreamer(tok) >>> # Despite returning the usual output, the streamer will also print the generated text to stdout. >>> _ = model.generate(**inputs, streamer=streamer, max_new_tokens=20) An increasing sequence: one, two, three, four, five, six, seven, eight, nine, ten, eleven, ``` c X||_||_||_g|_d|_d|_y)NrT) tokenizer skip_prompt decode_kwargs token_cache print_lennext_tokens_are_prompt)rrr r!s r__init__zTextStreamer.__init__Ks2"&*&*#rcLt|jdkDr|jddkDr tdt|jdkDr|d}|jr|jrd|_y|j j |j|jj|j fi|j}|jdr||jd}g|_d|_ nt|dkDrK|jt|dr.||jd}|xjt|z c_ n?||j|jddz}|xjt|z c_ |j!|y) zm Receives tokens, decodes them, and prints them to stdout as soon as they form entire words. rz'TextStreamer only supports batch size 1FN  )lenshape ValueErrorr r$r"extendtolistrdecoder!endswithr#_is_chinese_charordrfindon_finalized_text)rrtextprintable_texts rrzTextStreamer.putUs[ u{{ a EKKNQ$6FG G   !!HE    ; ;*/D '   /$t~~$$T%5%5L9K9KL == !$.."23N!D DN Y]t44Sb]C!$.."23N NNc.1 1N"$..4::c?Q3FGN NNc.1 1N ~.rc t|jdkDrO|jj|jfi|j}||j d}g|_d|_nd}d|_|j|dy)z;Flushes any remaining cache and prints a newline to stdout.rNT) stream_end)r+r"rr0r!r#r$r5)rr6r7s rrzTextStreamer.endws~ t 1 $(4>>(()9)9PT=O=OPD!$.."23N!D DNN&*# ~$?rc4t|d|sdydy)zNPrints the new text to stdout. If the stream is ending, also prints a newline.Tr9N)flushr)printrr6r:s rr5zTextStreamer.on_finalized_texts d$jBCdCrc|dk\r|dksF|dk\r|dks<|dk\r|dks2|dk\r|dks(|d k\r|d ks|d k\r|d ks|d k\r|dks |dk\r|dkryy)z6Checks whether CP is the codepoint of a CJK character.iNii4iMiiߦii?i@ii iiiiiTFr)rcps rr2zTextStreamer._is_chinese_charsr6\bFlf vg "-g "-g "-g "-f vg "-rNF)rrr boolr6strr:rB) rrrrr%rrr5r2rrrrr)s$B+ /D @DrrcHeZdZdZ d dfd Zdd dZdZdZxZS) TextIteratorStreamera Streamer that stores print-ready text in a queue, to be used by a downstream application as an iterator. This is useful for applications that benefit from accessing the generated text in a non-blocking way (e.g. in an interactive Gradio demo). The API for the streamer classes is still under development and may change in the future. Parameters: tokenizer (`AutoTokenizer`): The tokenized used to decode the tokens. skip_prompt (`bool`, *optional*, defaults to `False`): Whether to skip the prompt to `.generate()` or not. Useful e.g. for chatbots. timeout (`float`, *optional*): The timeout for the text queue. If `None`, the queue will block indefinitely. Useful to handle exceptions in `.generate()`, when it is called in a separate thread. decode_kwargs (`dict`, *optional*): Additional keyword arguments to pass to the tokenizer's `decode` method. Examples: ```python >>> from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer >>> from threading import Thread >>> tok = AutoTokenizer.from_pretrained("openai-community/gpt2") >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2") >>> inputs = tok(["An increasing sequence: one,"], return_tensors="pt") >>> streamer = TextIteratorStreamer(tok) >>> # Run the generation in a separate thread, so that we can fetch the generated text in a non-blocking way. >>> generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=20) >>> thread = Thread(target=model.generate, kwargs=generation_kwargs) >>> thread.start() >>> generated_text = "" >>> for new_text in streamer: ... generated_text += new_text >>> generated_text 'An increasing sequence: one, two, three, four, five, six, seven, eight, nine, ten, eleven,' ``` c bt|||fi|t|_d|_||_yN)superr%r text_queue stop_signaltimeoutrrr rLr! __class__s rr%zTextIteratorStreamer.__init__s1 KA=A' rc|jj||j|r2|jj|j|jyy)\Put the new text in the queue. If the stream is ending, also put a stop signal in the queue.rLN)rJrrLrKr>s rr5z&TextIteratorStreamer.on_finalized_textsF D$,,7  OO   0 0$,,  G rc|SrHrrs r__iter__zTextIteratorStreamer.__iter__ rc|jj|j}||jk(r t |SNrQ)rJgetrLrK StopIterationr s r__next__zTextIteratorStreamer.__next__s8##DLL#9 D$$ $/ !LrFNrrr rBrLz float | NonerArC) rrrrr%r5rSrY __classcell__rNs@rrFrFs=+\\`&59LXH rrFcHeZdZdZ d dfd Zdd dZdZdZxZS) AsyncTextIteratorStreamera' Streamer that stores print-ready text in a queue, to be used by a downstream application as an async iterator. This is useful for applications that benefit from accessing the generated text asynchronously (e.g. in an interactive Gradio demo). The API for the streamer classes is still under development and may change in the future. Parameters: tokenizer (`AutoTokenizer`): The tokenized used to decode the tokens. skip_prompt (`bool`, *optional*, defaults to `False`): Whether to skip the prompt to `.generate()` or not. Useful e.g. for chatbots. timeout (`float`, *optional*): The timeout for the text queue. If `None`, the queue will block indefinitely. Useful to handle exceptions in `.generate()`, when it is called in a separate thread. decode_kwargs (`dict`, *optional*): Additional keyword arguments to pass to the tokenizer's `decode` method. Raises: TimeoutError: If token generation time exceeds timeout value. Examples: ```python >>> from transformers import AutoModelForCausalLM, AutoTokenizer, AsyncTextIteratorStreamer >>> from threading import Thread >>> import asyncio >>> tok = AutoTokenizer.from_pretrained("openai-community/gpt2") >>> model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2") >>> inputs = tok(["An increasing sequence: one,"], return_tensors="pt") >>> # Run the generation in a separate thread, so that we can fetch the generated text in a non-blocking way. >>> async def main(): ... # Important: AsyncTextIteratorStreamer must be initialized inside a coroutine! ... streamer = AsyncTextIteratorStreamer(tok) ... generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=20) ... thread = Thread(target=model.generate, kwargs=generation_kwargs) ... thread.start() ... generated_text = "" ... async for new_text in streamer: ... generated_text += new_text >>> print(generated_text) >>> asyncio.run(main()) An increasing sequence: one, two, three, four, five, six, seven, eight, nine, ten, eleven, ``` c t|||fi|tj|_d|_||_tj|_ttd|_ y)NrL) rIr%asynciorrJrKrLget_running_looploophasattrhas_asyncio_timeoutrMs rr%z"AsyncTextIteratorStreamer.__init__sT KA=A!--/ ,,. #*7I#> rc|jj|jj||r;|jj|jj|jyy)rPN)rccall_soon_threadsaferJ put_nowaitrKr>s rr5z+AsyncTextIteratorStreamer.on_finalized_text(sL &&t'A'A4H  II * *4??+E+EtGWGW X rc|SrHrrs r __aiter__z#AsyncTextIteratorStreamer.__aiter__.rTrcK |jr[tj|j4d{|jj d{}dddd{nAtj |jj |jd{}|j k(r t|S77s7e#1d{7swY3xYw7:#tj$r twxYwwrV) rerarLrJrWwait_forrKStopAsyncIteration TimeoutErrorr s r __anext__z#AsyncTextIteratorStreamer.__anext__1s ''"??4<<888"&//"5"5"77E888&..t/B/B/Ddll[[((((** 878888\## !.  !sD/C$CC$C C C  C$%C &AC$'C"(C$,DC$ C  C$ CC CC$$DDrZr[rArC) rrrrr%r5rjror\r]s@rr_r_s=2j\`?&?59?LX?Y  rr_) __future__rraqueuertypingr models.autorr rrFr_rrrrtsQ # + $ $v<vrD<DNU Ur