

'''simple docstring'''


import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


lowerCAmelCase__						:       List[str]										=						"""hf-internal-testing/tiny-random-bert"""
lowerCAmelCase__						:       Any										=						os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
lowerCAmelCase__						:       Tuple										=						"""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""




class     a			(				unittest.TestCase       ):






						"""simple docstring"""




						def       __magic_name__							(							self					:      List[str]  ):



								'''simple docstring'''



								snake_case__						:       Union[str, Any]       						=  cached_file(_lowerCamelCase			,			_lowerCamelCase  )
								# Should have downloaded the file in here
								self.assertTrue(os.path.isdir(_lowerCamelCase  )  )
								# Cache should contain at least those three subfolders:
								for subfolder in ["blobs", "refs", "snapshots"]:
										self.assertTrue(os.path.isdir(os.path.join(_lowerCamelCase			,			_lowerCamelCase  )  )  )
								with open(os.path.join(_lowerCamelCase			,			'''refs'''			,			'''main'''  )  ) as f:
										snake_case__						:       Tuple       						=  f.read()
								self.assertEqual(_lowerCamelCase			,			os.path.join(_lowerCamelCase			,			'''snapshots'''			,			_lowerCamelCase			,			_lowerCamelCase  )  )
								self.assertTrue(os.path.isfile(_lowerCamelCase  )  )

								# File is cached at the same place the second time.
								snake_case__						:       int       						=  cached_file(_lowerCamelCase			,			_lowerCamelCase  )
								self.assertEqual(_lowerCamelCase			,			_lowerCamelCase  )

								# Using a specific revision to test the full commit hash.
								snake_case__						:       List[Any]       						=  cached_file(_lowerCamelCase			,			_lowerCamelCase			,			revision='''9b8c223'''  )
								self.assertEqual(_lowerCamelCase			,			os.path.join(_lowerCamelCase			,			'''snapshots'''			,			_lowerCamelCase			,			_lowerCamelCase  )  )







						def       __magic_name__							(							self					:      List[Any]  ):



								'''simple docstring'''



								with self.assertRaisesRegex(_lowerCamelCase			,			'''is not a valid model identifier'''  ):
										snake_case__						:       Any       						=  cached_file('''tiny-random-bert'''			,			_lowerCamelCase  )

								with self.assertRaisesRegex(_lowerCamelCase			,			'''is not a valid git identifier'''  ):
										snake_case__						:       int       						=  cached_file(_lowerCamelCase			,			_lowerCamelCase			,			revision='''aaaa'''  )

								with self.assertRaisesRegex(_lowerCamelCase			,			'''does not appear to have a file named'''  ):
										snake_case__						:       Union[str, Any]       						=  cached_file(_lowerCamelCase			,			'''conf'''  )







						def       __magic_name__							(							self					:      str  ):



								'''simple docstring'''



								with self.assertRaisesRegex(_lowerCamelCase			,			'''does not appear to have a file named'''  ):
										snake_case__						:       List[Any]       						=  cached_file(_lowerCamelCase			,			'''conf'''  )

								with open(os.path.join(_lowerCamelCase			,			'''refs'''			,			'''main'''  )  ) as f:
										snake_case__						:       Any       						=  f.read()
								self.assertTrue(os.path.isfile(os.path.join(_lowerCamelCase			,			'''.no_exist'''			,			_lowerCamelCase			,			'''conf'''  )  )  )

								snake_case__						:       Any       						=  cached_file(_lowerCamelCase			,			'''conf'''			,			_raise_exceptions_for_missing_entries=_lowerCamelCase  )
								self.assertIsNone(_lowerCamelCase  )

								snake_case__						:       Optional[int]       						=  cached_file(_lowerCamelCase			,			'''conf'''			,			local_files_only=_lowerCamelCase			,			_raise_exceptions_for_missing_entries=_lowerCamelCase  )
								self.assertIsNone(_lowerCamelCase  )

								snake_case__						:       Union[str, Any]       						=  mock.Mock()
								snake_case__						:       Dict       						=  5_0_0
								snake_case__						:       Optional[Any]       						=  {}
								snake_case__						:       str       						=  HTTPError
								snake_case__						:       int       						=  {}

								# Under the mock environment we get a 500 error when trying to reach the tokenizer.
								with mock.patch('''requests.Session.request'''			,			return_value=_lowerCamelCase  ) as mock_head:
										snake_case__						:       Union[str, Any]       						=  cached_file(_lowerCamelCase			,			'''conf'''			,			_raise_exceptions_for_connection_errors=_lowerCamelCase  )
										self.assertIsNone(_lowerCamelCase  )
										# This check we did call the fake head request
										mock_head.assert_called()







						def       __magic_name__							(							self					:      Optional[int]  ):



								'''simple docstring'''



								self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only'''			,			_lowerCamelCase  )  )
								self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only'''			,			_lowerCamelCase  )  )
								self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only'''			,			_lowerCamelCase  )  )







						def       __magic_name__							(							self					:      Tuple  ):



								'''simple docstring'''



								self.assertIsNone(get_file_from_repo('''bert-base-cased'''			,			'''ahah.txt'''  )  )

								# The function raises if the repository does not exist.
								with self.assertRaisesRegex(_lowerCamelCase			,			'''is not a valid model identifier'''  ):
										get_file_from_repo('''bert-base-case'''			,			_lowerCamelCase  )

								# The function raises if the revision does not exist.
								with self.assertRaisesRegex(_lowerCamelCase			,			'''is not a valid git identifier'''  ):
										get_file_from_repo('''bert-base-cased'''			,			_lowerCamelCase			,			revision='''ahaha'''  )

								snake_case__						:       Any       						=  get_file_from_repo('''bert-base-cased'''			,			_lowerCamelCase  )
								# The name is the cached name which is not very easy to test, so instead we load the content.
								snake_case__						:       Union[str, Any]       						=  json.loads(open(_lowerCamelCase			,			'''r'''  ).read()  )
								self.assertEqual(config['''hidden_size''']			,			7_6_8  )







						def       __magic_name__							(							self					:      List[str]  ):



								'''simple docstring'''



								with tempfile.TemporaryDirectory() as tmp_dir:
										snake_case__						:       Optional[Any]       						=  Path(_lowerCamelCase  ) / '''a.txt'''
										filename.touch()
										self.assertEqual(get_file_from_repo(_lowerCamelCase			,			'''a.txt'''  )			,			str(_lowerCamelCase  )  )

										self.assertIsNone(get_file_from_repo(_lowerCamelCase			,			'''b.txt'''  )  )






'''simple docstring'''


import warnings

from diffusers import StableDiffusionImgaImgPipeline  # noqa F401


warnings.warn(
    """The `image_to_image.py` script is outdated. Please use directly `from diffusers import"""
    """ StableDiffusionImg2ImgPipeline` instead."""
)



