






"""simple docstring"""






import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch




class 							lowerCAmelCase__       (    UpperCAmelCase__				):






      '''simple docstring'''

      @require_torch
      def _SCREAMING_SNAKE_CASE				(		self  :						Optional[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	'''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

          SCREAMING_SNAKE_CASE_   :						Dict							=	'''
mname = "hf-internal-testing/tiny-random-bert"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task="fill-mask", model=mname)
print("success")
        '''

          SCREAMING_SNAKE_CASE_   :						List[str]							=	'''
import socket
def offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")
socket.socket = offline_socket
        '''

          # Force fetching the files so that we can use the cache
          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	'''hf-internal-testing/tiny-random-bert'''
          BertConfig.from_pretrained(lowercase_)
          BertModel.from_pretrained(lowercase_)
          BertTokenizer.from_pretrained(lowercase_)
          pipeline(task='''fill-mask'''		,			model=lowercase_)

          # baseline - just load from_pretrained with normal network
          SCREAMING_SNAKE_CASE_   :						List[str]							=	[sys.executable, '''-c''', '''\n'''.join([load, run, mock])]

          # should succeed
          SCREAMING_SNAKE_CASE_   :						List[str]							=	self.get_env()
          # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
          SCREAMING_SNAKE_CASE_   :						Any							=	'''1'''
          SCREAMING_SNAKE_CASE_   :						Any							=	subprocess.run(lowercase_		,			env=lowercase_		,			check=lowercase_		,			capture_output=lowercase_)
          self.assertEqual(result.returncode		,			0		,			result.stderr)
          self.assertIn('''success'''		,			result.stdout.decode())
      @require_torch
      def _SCREAMING_SNAKE_CASE				(		self  :						Any):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						int							=	'''
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        '''

          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	'''
mname = "hf-internal-testing/tiny-random-bert"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task="fill-mask", model=mname)
print("success")
        '''

          SCREAMING_SNAKE_CASE_   :						Any							=	'''
import socket
def offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")
socket.socket = offline_socket
        '''

          # Force fetching the files so that we can use the cache
          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	'''hf-internal-testing/tiny-random-bert'''
          BertConfig.from_pretrained(lowercase_)
          BertModel.from_pretrained(lowercase_)
          BertTokenizer.from_pretrained(lowercase_)
          pipeline(task='''fill-mask'''		,			model=lowercase_)

          # baseline - just load from_pretrained with normal network
          SCREAMING_SNAKE_CASE_   :						str							=	[sys.executable, '''-c''', '''\n'''.join([load, run, mock])]

          # should succeed
          SCREAMING_SNAKE_CASE_   :						str							=	self.get_env()
          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	subprocess.run(lowercase_		,			env=lowercase_		,			check=lowercase_		,			capture_output=lowercase_)
          self.assertEqual(result.returncode		,			0		,			result.stderr)
          self.assertIn('''success'''		,			result.stdout.decode())
      @require_torch
      def _SCREAMING_SNAKE_CASE				(		self  :						Dict):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						Any							=	'''
from transformers import BertConfig, BertModel, BertTokenizer
        '''

          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	'''
mname = "hf-internal-testing/tiny-random-bert-sharded"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
print("success")
        '''

          SCREAMING_SNAKE_CASE_   :						Tuple							=	'''
import socket
def offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")
socket.socket = offline_socket
        '''

          # baseline - just load from_pretrained with normal network
          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	[sys.executable, '''-c''', '''\n'''.join([load, run])]

          # should succeed
          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	self.get_env()
          SCREAMING_SNAKE_CASE_   :						Tuple							=	subprocess.run(lowercase_		,			env=lowercase_		,			check=lowercase_		,			capture_output=lowercase_)
          self.assertEqual(result.returncode		,			0		,			result.stderr)
          self.assertIn('''success'''		,			result.stdout.decode())

          # next emulate no network
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	[sys.executable, '''-c''', '''\n'''.join([load, mock, run])]

          # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
          # env["TRANSFORMERS_OFFLINE"] = "0"
          # result = subprocess.run(cmd, env=env, check=False, capture_output=True)
          # self.assertEqual(result.returncode, 1, result.stderr)

          # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
          SCREAMING_SNAKE_CASE_   :						Tuple							=	'''1'''
          SCREAMING_SNAKE_CASE_   :						Optional[Any]							=	subprocess.run(lowercase_		,			env=lowercase_		,			check=lowercase_		,			capture_output=lowercase_)
          self.assertEqual(result.returncode		,			0		,			result.stderr)
          self.assertIn('''success'''		,			result.stdout.decode())
      @require_torch
      def _SCREAMING_SNAKE_CASE				(		self  :						int):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						int							=	'''
from transformers import pipeline
        '''
          SCREAMING_SNAKE_CASE_   :						List[str]							=	'''
mname = "hf-internal-testing/tiny-random-bert"
pipe = pipeline(model=mname)
        '''

          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	'''
import socket
def offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")
socket.socket = offline_socket
        '''
          SCREAMING_SNAKE_CASE_   :						Optional[int]							=	self.get_env()
          SCREAMING_SNAKE_CASE_   :						str							=	'''1'''
          SCREAMING_SNAKE_CASE_   :						Any							=	[sys.executable, '''-c''', '''\n'''.join([load, mock, run])]
          SCREAMING_SNAKE_CASE_   :						Tuple							=	subprocess.run(lowercase_		,			env=lowercase_		,			check=lowercase_		,			capture_output=lowercase_)
          self.assertEqual(result.returncode		,			1		,			result.stderr)
          self.assertIn(
              '''You cannot infer task automatically within `pipeline` when using offline mode'''		,			result.stderr.decode().replace('''\n'''		,			'''''')		,			)



      @require_torch
      def _SCREAMING_SNAKE_CASE				(		self  :						int):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_   :						str							=	'''
from transformers import AutoModel
        '''
          SCREAMING_SNAKE_CASE_   :						Tuple							=	'''
mname = "hf-internal-testing/test_dynamic_model"
AutoModel.from_pretrained(mname, trust_remote_code=True)
print("success")
        '''

          # baseline - just load from_pretrained with normal network
          SCREAMING_SNAKE_CASE_   :						Union[str, Any]							=	[sys.executable, '''-c''', '''\n'''.join([load, run])]

          # should succeed
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	self.get_env()
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	subprocess.run(lowercase_		,			env=lowercase_		,			check=lowercase_		,			capture_output=lowercase_)
          self.assertEqual(result.returncode		,			0		,			result.stderr)
          self.assertIn('''success'''		,			result.stdout.decode())

          # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
          SCREAMING_SNAKE_CASE_   :						List[str]							=	'''1'''
          SCREAMING_SNAKE_CASE_   :						List[Any]							=	subprocess.run(lowercase_		,			env=lowercase_		,			check=lowercase_		,			capture_output=lowercase_)
          self.assertEqual(result.returncode		,			0		,			result.stderr)
          self.assertIn('''success'''		,			result.stdout.decode())










"""simple docstring"""






from __future__ import annotations

import requests



def       _A    (__a							)				-> dict:
    """simple docstring"""






    SCREAMING_SNAKE_CASE_   :						Any							=	f'https://hacker-news.firebaseio.com/v0/item/{story_id}.json?print=pretty'
    return requests.get(__a							).json()



def       _A    (__a = 10							)				-> list[dict]:
    """simple docstring"""






    SCREAMING_SNAKE_CASE_   :						Any							=	'''https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty'''
    SCREAMING_SNAKE_CASE_   :						Dict							=	requests.get(__a							).json()[:max_stories]
    return [get_hackernews_story(__a							) for story_id in story_ids]



def       _A    (__a = 10							)				-> str:
    """simple docstring"""






    SCREAMING_SNAKE_CASE_   :						Tuple							=	hackernews_top_stories(__a							)
    return "\n".join('''* [{title}]({url})'''.format(**__a							) for story in stories							)


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


