


'''simple docstring'''

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 						__SCREAMING_SNAKE_CASE					(      unittest.TestCase ):






     def  a_					(			self		)       ->    Any:
          _a						=		AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2"		)
          _a						=		AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"		).to(__UpperCamelCase		)
          _a						=		-1

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

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

          self.assertEqual(__UpperCamelCase     ,							__UpperCamelCase		)






     def  a_					(			self		)       ->    int:
          _a						=		AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2"		)
          _a						=		AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"		).to(__UpperCamelCase		)
          _a						=		-1

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

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

          self.assertEqual(__UpperCamelCase     ,							__UpperCamelCase		)






     def  a_					(			self		)       ->    str:
          _a						=		AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-gpt2"		)
          _a						=		AutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-random-gpt2"		).to(__UpperCamelCase		)
          _a						=		-1

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

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

          self.assertEqual(__UpperCamelCase     ,							__UpperCamelCase		)






     def  a_					(			self		)       ->    List[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
          _a						=		AutoTokenizer.from_pretrained("distilgpt2"		)
          _a						=		AutoModelForCausalLM.from_pretrained("distilgpt2"		).to(__UpperCamelCase		)
          _a						=		-1

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

          # 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
          _a						=		cs.out[:-1]  # Remove the final "\n"
          _a						=		tokenizer(__UpperCamelCase     ,							return_tensors="pt"		)
          self.assertEqual(streamer_text_tokenized.input_ids.shape     ,							(1, 1)		)






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

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

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





'''simple docstring'''

from __future__ import annotations

import requests






def __UpperCamelCase   (			__lowerCamelCase		: str				)      ->     dict:







     '''simple docstring'''






     _a						=		F"https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty"
     return requests.get(__lowerCamelCase				).json()






def __UpperCamelCase   (			__lowerCamelCase		: int = 10				)      ->     list[dict]:







     '''simple docstring'''






     _a						=		"https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty"
     _a						=		requests.get(__lowerCamelCase				).json()[:max_stories]
     return [get_hackernews_story(__lowerCamelCase				) for story_id in story_ids]






def __UpperCamelCase   (			__lowerCamelCase		: int = 10				)      ->     str:







     '''simple docstring'''






     _a						=		hackernews_top_stories(__lowerCamelCase				)
     return "\n".join("* [{title}]({url})".format(**__lowerCamelCase				) for story in stories				)


if __name__ == "__main__":
   print(hackernews_top_stories_as_markdown())

