





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_    =    '''hf-internal-testing/tiny-random-bert'''
lowerCAmelCase_    =    os.path.join(TRANSFORMERS_CACHE, '''models--hf-internal-testing--tiny-random-bert''')
lowerCAmelCase_    =    '''9b8c223d42b2188cb49d29af482996f9d0f3e5a6'''



class  __lowerCAmelCase	(      unittest.TestCase					):






			def       lowerCamelCase  (self   )					->       int:


							'''simple docstring'''



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

							# File is cached at the same place the second time.
							snake_case_						:   Optional[Any]           =  cached_file(__magic_name__ ,							__magic_name__   )
							self.assertEqual(__magic_name__ ,							__magic_name__   )

							# Using a specific revision to test the full commit hash.
							snake_case_						:   str           =  cached_file(__magic_name__ ,							__magic_name__ ,							revision='''9b8c223'''   )
							self.assertEqual(__magic_name__ ,							os.path.join(__magic_name__ ,							'''snapshots''' ,							__magic_name__ ,							__magic_name__   )   )






			def       lowerCamelCase  (self   )					->       Optional[Any]:


							'''simple docstring'''



							with self.assertRaisesRegex(__magic_name__ ,							'''is not a valid model identifier'''   ):
											snake_case_						:   str           =  cached_file('''tiny-random-bert''' ,							__magic_name__   )

							with self.assertRaisesRegex(__magic_name__ ,							'''is not a valid git identifier'''   ):
											snake_case_						:   Optional[Any]           =  cached_file(__magic_name__ ,							__magic_name__ ,							revision='''aaaa'''   )

							with self.assertRaisesRegex(__magic_name__ ,							'''does not appear to have a file named'''   ):
											snake_case_						:   List[str]           =  cached_file(__magic_name__ ,							'''conf'''   )






			def       lowerCamelCase  (self   )					->       Optional[Any]:


							'''simple docstring'''



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

							with open(os.path.join(__magic_name__ ,							'''refs''' ,							'''main'''   )   ) as f:
											snake_case_						:   Tuple           =  f.read()
							self.assertTrue(os.path.isfile(os.path.join(__magic_name__ ,							'''.no_exist''' ,							__magic_name__ ,							'''conf'''   )   )   )

							snake_case_						:   List[Any]           =  cached_file(__magic_name__ ,							'''conf''' ,							_raise_exceptions_for_missing_entries=__magic_name__   )
							self.assertIsNone(__magic_name__   )

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

							snake_case_						:   Optional[int]           =  mock.Mock()
							snake_case_						:   Optional[int]           =  500
							snake_case_						:   str           =  {}
							snake_case_						:   str           =  HTTPError
							snake_case_						:   str           =  {}

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






			def       lowerCamelCase  (self   )					->       Optional[Any]:


							'''simple docstring'''



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






			def       lowerCamelCase  (self   )					->       Union[str, Any]:


							'''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(__magic_name__ ,							'''is not a valid model identifier'''   ):
											get_file_from_repo('''bert-base-case''' ,							__magic_name__   )

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

							snake_case_						:   List[Any]           =  get_file_from_repo('''bert-base-cased''' ,							__magic_name__   )
							# The name is the cached name which is not very easy to test, so instead we load the content.
							snake_case_						:   int           =  json.loads(open(__magic_name__ ,							'''r'''   ).read()   )
							self.assertEqual(config['''hidden_size'''] ,							768   )






			def       lowerCamelCase  (self   )					->       List[str]:


							'''simple docstring'''



							with tempfile.TemporaryDirectory() as tmp_dir:
											snake_case_						:   Tuple           =  Path(__magic_name__   ) / '''a.txt'''
											filename.touch()
											self.assertEqual(get_file_from_repo(__magic_name__ ,							'''a.txt'''   ) ,							str(__magic_name__   )   )

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







import numpy as np
from matplotlib import pyplot as plt
from sklearn.datasets import load_iris
from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
from xgboost import XGBClassifier






def 					lowerCamelCase_						(    _UpperCamelCase			)		->						tuple:






				"""simple docstring"""


				return (data["data"], data["target"])






def 					lowerCamelCase_						(    _UpperCamelCase			,    _UpperCamelCase			)		->						XGBClassifier:






				"""simple docstring"""


				snake_case_						:   Optional[Any]           =  XGBClassifier()
				classifier.fit(_UpperCamelCase			,    _UpperCamelCase			)
				return classifier






def 					lowerCamelCase_						(    )		->						None:






				"""simple docstring"""


				snake_case_						:   Optional[Any]           =  load_iris()
				snake_case_      ,							snake_case_						:   str           =  data_handling(_UpperCamelCase			)
				snake_case_      ,							snake_case_      ,							snake_case_      ,							snake_case_						:   Dict           =  train_test_split(
				    _UpperCamelCase			,    _UpperCamelCase			,    test_size=0.25			)

				snake_case_						:   List[str]           =  iris['''target_names''']

				# Create an XGBoost Classifier from the training data
				snake_case_						:   int           =  xgboost(_UpperCamelCase			,    _UpperCamelCase			)

				# Display the confusion matrix of the classifier with both training and test sets
				ConfusionMatrixDisplay.from_estimator(
				    _UpperCamelCase			,    _UpperCamelCase			,    _UpperCamelCase			,    display_labels=_UpperCamelCase			,    cmap='''Blues'''			,    normalize='''true'''			,    )
				plt.title('''Normalized Confusion Matrix - IRIS Dataset'''			)
				plt.show()


if __name__ == "__main__":
	import doctest

	doctest.testmod(verbose=True)
	main()
