


import unittest
from queue import Empty
from threading import Thread

from transformers import AutoTokenizer, TextIteratorStreamer, TextStreamer, is_torch_available
from transformers.testing_utils import CaptureStdout, require_torch, torch_device

from ..test_modeling_common import ids_tensor


if is_torch_available():
   import torch

   from transformers import AutoModelForCausalLM






@require_torch
class     UpperCAmelCase__       (					unittest.TestCase		):



    """simple docstring"""






    def _a					(			self    )  ->       Union[str, Any]:
         __UpperCamelCase    							=AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2'    )
         __UpperCamelCase    							=AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2'    ).to(A_    )
         __UpperCamelCase    							=-1

         __UpperCamelCase    							=ids_tensor((1, 5)					,   vocab_size=model.config.vocab_size    ).to(A_    )
         __UpperCamelCase    							=model.generate(A_					,   max_new_tokens=10					,   do_sample=A_    )
         __UpperCamelCase    							=tokenizer.decode(greedy_ids[0]    )

         with CaptureStdout() as cs:
              __UpperCamelCase    							=TextStreamer(A_    )
              model.generate(A_					,   max_new_tokens=10					,   do_sample=A_					,   streamer=A_    )
         # The greedy text should be printed to stdout, except for the final "\n" in the streamer
         __UpperCamelCase    							=cs.out[:-1]

         self.assertEqual(A_					,   A_    )






    def _a					(			self    )  ->       Tuple:
         __UpperCamelCase    							=AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2'    )
         __UpperCamelCase    							=AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2'    ).to(A_    )
         __UpperCamelCase    							=-1

         __UpperCamelCase    							=ids_tensor((1, 5)					,   vocab_size=model.config.vocab_size    ).to(A_    )
         __UpperCamelCase    							=model.generate(A_					,   max_new_tokens=10					,   do_sample=A_    )
         __UpperCamelCase    							=tokenizer.decode(greedy_ids[0]    )

         __UpperCamelCase    							=TextIteratorStreamer(A_    )
         __UpperCamelCase    							={'input_ids': input_ids, 'max_new_tokens': 10, 'do_sample': False, 'streamer': streamer}
         __UpperCamelCase    							=Thread(target=model.generate					,   kwargs=A_    )
         thread.start()
         __UpperCamelCase    							=''
         for new_text in streamer:
              streamer_text += new_text

         self.assertEqual(A_					,   A_    )






    def _a					(			self    )  ->       List[Any]:
         __UpperCamelCase    							=AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2'    )
         __UpperCamelCase    							=AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2'    ).to(A_    )
         __UpperCamelCase    							=-1

         __UpperCamelCase    							=ids_tensor((1, 5)					,   vocab_size=model.config.vocab_size    ).to(A_    )
         __UpperCamelCase    							=model.generate(A_					,   max_new_tokens=10					,   do_sample=A_    )
         __UpperCamelCase    							=greedy_ids[:, input_ids.shape[1] :]
         __UpperCamelCase    							=tokenizer.decode(new_greedy_ids[0]    )

         with CaptureStdout() as cs:
              __UpperCamelCase    							=TextStreamer(A_					,   skip_prompt=A_    )
              model.generate(A_					,   max_new_tokens=10					,   do_sample=A_					,   streamer=A_    )
         # The greedy text should be printed to stdout, except for the final "\n" in the streamer
         __UpperCamelCase    							=cs.out[:-1]

         self.assertEqual(A_					,   A_    )






    def _a					(			self    )  ->       Any:
         # Tests that we can pass `decode_kwargs` to the streamer to control how the tokens are decoded. Must be tested
         # with actual models -- the dummy models' tokenizers are not aligned with their models, and
         # `skip_special_tokens=True` has no effect on them
         __UpperCamelCase    							=AutoTokenizer.from_pretrained('distilgpt2'    )
         __UpperCamelCase    							=AutoModelForCausalLM.from_pretrained('distilgpt2'    ).to(A_    )
         __UpperCamelCase    							=-1

         __UpperCamelCase    							=torch.ones((1, 5)					,   device=A_    ).long() * model.config.bos_token_id
         with CaptureStdout() as cs:
              __UpperCamelCase    							=TextStreamer(A_					,   skip_special_tokens=A_    )
              model.generate(A_					,   max_new_tokens=1					,   do_sample=A_					,   streamer=A_    )

         # The prompt contains a special token, so the streamer should not print it. As such, the output text, when
         # re-tokenized, must only contain one token
         __UpperCamelCase    							=cs.out[:-1]  # Remove the final "\n"
         __UpperCamelCase    							=tokenizer(A_					,   return_tensors='pt'    )
         self.assertEqual(streamer_text_tokenized.input_ids.shape					,   (1, 1)    )






    def _a					(			self    )  ->       Tuple:
         __UpperCamelCase    							=AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2'    )
         __UpperCamelCase    							=AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2'    ).to(A_    )
         __UpperCamelCase    							=-1

         __UpperCamelCase    							=ids_tensor((1, 5)					,   vocab_size=model.config.vocab_size    ).to(A_    )
         __UpperCamelCase    							=TextIteratorStreamer(A_					,   timeout=0.001    )
         __UpperCamelCase    							={'input_ids': input_ids, 'max_new_tokens': 10, 'do_sample': False, 'streamer': streamer}
         __UpperCamelCase    							=Thread(target=model.generate					,   kwargs=A_    )
         thread.start()

         # The streamer will timeout after 0.001 seconds, so an exception will be raised
         with self.assertRaises(A_    ):
              __UpperCamelCase    							=''
              for new_text in streamer:
                   streamer_text += new_text









import socket
def 			_a (      ):


		"""simple docstring"""




		lowercase__            =      socket.socket(socket.AF_INET						,				socket.SOCK_STREAM     )
		lowercase__            =      socket.gethostname()
		lowercase__            =      1_23_12

		sock.connect((host, port)     )
		sock.send(B'''Hello server!'''     )

		with open('''Received_file'''						,				'''wb'''     ) as out_file:
				print('''File opened'''     )
				print('''Receiving data...'''     )
				while True:
						lowercase__            =      sock.recv(10_24     )
						if not data:
								break
						out_file.write(SCREAMING_SNAKE_CASE     )

		print('''Successfully received the file'''     )
		sock.close()
		print('''Connection closed'''     )


if __name__ == "__main__":
			main()


