


"""simple docstring"""







import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoImageProcessor, ViTImageProcessor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / '''utils'''))

from test_module.custom_image_processing import CustomImageProcessor  # noqa E402


a__       :							str        =  get_tests_dir('''fixtures''')




class 				UpperCamelCase_			(				unittest.TestCase):

	"""simple docstring"""





	def 						UpperCAmelCase_   (				self    :       Dict     ) ->			Optional[Any]:
						# A mock response for an HTTP head request to emulate server down
						__SCREAMING_SNAKE_CASE		     =       mock.Mock()
						__SCREAMING_SNAKE_CASE		     =       5_0_0
						__SCREAMING_SNAKE_CASE		     =       {}
						__SCREAMING_SNAKE_CASE		     =       HTTPError
						__SCREAMING_SNAKE_CASE		     =       {}

						# Download this model to make sure it's in the cache.
						__SCREAMING_SNAKE_CASE		     =       ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit"     )
						# Under the mock environment we get a 500 error when trying to reach the model.
						with mock.patch("requests.Session.request"    ,    return_value=UpperCAmelCase__     ) as mock_head:
											__SCREAMING_SNAKE_CASE		     =       ViTImageProcessor.from_pretrained("hf-internal-testing/tiny-random-vit"     )
											# This check we did call the fake head request
											mock_head.assert_called()




	def 						UpperCAmelCase_   (				self    :       Any     ) ->			Optional[Any]:
						# This test is for deprecated behavior and can be removed in v5
						__SCREAMING_SNAKE_CASE		     =       ViTImageProcessor.from_pretrained(
						    "https://huggingface.co/hf-internal-testing/tiny-random-vit/resolve/main/preprocessor_config.json"     )




	def 						UpperCAmelCase_   (				self    :       Optional[int]     ) ->			int:
						with self.assertRaises(UpperCAmelCase__     ):
											# config is in subfolder, the following should not work without specifying the subfolder
											__SCREAMING_SNAKE_CASE		     =       AutoImageProcessor.from_pretrained("hf-internal-testing/stable-diffusion-all-variants"     )

						__SCREAMING_SNAKE_CASE		     =       AutoImageProcessor.from_pretrained(
						    "hf-internal-testing/stable-diffusion-all-variants"    ,    subfolder="feature_extractor"     )

						self.assertIsNotNone(UpperCAmelCase__     )




@is_staging_test
class 				UpperCamelCase_			(				unittest.TestCase):

	"""simple docstring"""





	@classmethod
	def 						UpperCAmelCase_   (				cls    :       Tuple     ) ->			List[str]:
						__SCREAMING_SNAKE_CASE		     =       TOKEN
						HfFolder.save_token(UpperCAmelCase__     )




	@classmethod
	def 						UpperCAmelCase_   (				cls    :       int     ) ->			Any:
						try:
											delete_repo(token=cls._token    ,    repo_id="test-image-processor"     )
						except HTTPError:
											pass

						try:
											delete_repo(token=cls._token    ,    repo_id="valid_org/test-image-processor-org"     )
						except HTTPError:
											pass

						try:
											delete_repo(token=cls._token    ,    repo_id="test-dynamic-image-processor"     )
						except HTTPError:
											pass




	def 						UpperCAmelCase_   (				self    :       Optional[int]     ) ->			Tuple:
						__SCREAMING_SNAKE_CASE		     =       ViTImageProcessor.from_pretrained(UpperCAmelCase__     )
						image_processor.push_to_hub("test-image-processor"    ,    use_auth_token=self._token     )

						__SCREAMING_SNAKE_CASE		     =       ViTImageProcessor.from_pretrained(F"""{USER}/test-image-processor"""     )
						for k, v in image_processor.__dict__.items():
											self.assertEqual(UpperCAmelCase__    ,    getattr(UpperCAmelCase__    ,    UpperCAmelCase__     )     )

						# Reset repo
						delete_repo(token=self._token    ,    repo_id="test-image-processor"     )

						# Push to hub via save_pretrained
						with tempfile.TemporaryDirectory() as tmp_dir:
											image_processor.save_pretrained(
											    UpperCAmelCase__    ,    repo_id="test-image-processor"    ,    push_to_hub=UpperCAmelCase__    ,    use_auth_token=self._token     )

						__SCREAMING_SNAKE_CASE		     =       ViTImageProcessor.from_pretrained(F"""{USER}/test-image-processor"""     )
						for k, v in image_processor.__dict__.items():
											self.assertEqual(UpperCAmelCase__    ,    getattr(UpperCAmelCase__    ,    UpperCAmelCase__     )     )




	def 						UpperCAmelCase_   (				self    :       Union[str, Any]     ) ->			Union[str, Any]:
						__SCREAMING_SNAKE_CASE		     =       ViTImageProcessor.from_pretrained(UpperCAmelCase__     )
						image_processor.push_to_hub("valid_org/test-image-processor"    ,    use_auth_token=self._token     )

						__SCREAMING_SNAKE_CASE		     =       ViTImageProcessor.from_pretrained("valid_org/test-image-processor"     )
						for k, v in image_processor.__dict__.items():
											self.assertEqual(UpperCAmelCase__    ,    getattr(UpperCAmelCase__    ,    UpperCAmelCase__     )     )

						# Reset repo
						delete_repo(token=self._token    ,    repo_id="valid_org/test-image-processor"     )

						# Push to hub via save_pretrained
						with tempfile.TemporaryDirectory() as tmp_dir:
											image_processor.save_pretrained(
											    UpperCAmelCase__    ,    repo_id="valid_org/test-image-processor-org"    ,    push_to_hub=UpperCAmelCase__    ,    use_auth_token=self._token     )

						__SCREAMING_SNAKE_CASE		     =       ViTImageProcessor.from_pretrained("valid_org/test-image-processor-org"     )
						for k, v in image_processor.__dict__.items():
											self.assertEqual(UpperCAmelCase__    ,    getattr(UpperCAmelCase__    ,    UpperCAmelCase__     )     )




	def 						UpperCAmelCase_   (				self    :       Tuple     ) ->			Optional[int]:
						CustomImageProcessor.register_for_auto_class()
						__SCREAMING_SNAKE_CASE		     =       CustomImageProcessor.from_pretrained(UpperCAmelCase__     )

						image_processor.push_to_hub("test-dynamic-image-processor"    ,    use_auth_token=self._token     )

						# This has added the proper auto_map field to the config
						self.assertDictEqual(
						    image_processor.auto_map    ,    {"AutoImageProcessor": "custom_image_processing.CustomImageProcessor"}    ,    )

						__SCREAMING_SNAKE_CASE		     =       AutoImageProcessor.from_pretrained(
						    F"""{USER}/test-dynamic-image-processor"""    ,    trust_remote_code=UpperCAmelCase__     )
						# Can't make an isinstance check because the new_image_processor is from the CustomImageProcessor class of a dynamic module
						self.assertEqual(new_image_processor.__class__.__name__    ,    "CustomImageProcessor"     )




"""simple docstring"""







import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TextClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
a__       :							Dict        =  {'''LayoutLMv2Config''', '''LayoutLMv3Config'''}




@is_pipeline_test
class 				UpperCamelCase_			(				unittest.TestCase):

	"""simple docstring"""





	snake_case__       :						str					      =    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
	snake_case__       :						Tuple					      =    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

	if model_mapping is not None:
		snake_case__       :						List[Any]					      =    {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
	if tf_model_mapping is not None:
		snake_case__       :						Dict					      =    {
		    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
		}




	@require_torch
	def 						UpperCAmelCase_   (				self    :       str     ) ->			Tuple:
						__SCREAMING_SNAKE_CASE		     =       pipeline(
						    task="text-classification"    ,    model="hf-internal-testing/tiny-random-distilbert"    ,    framework="pt"     )

						__SCREAMING_SNAKE_CASE		     =       text_classifier("This is great !"     )
						self.assertEqual(nested_simplify(UpperCAmelCase__     )    ,    [{"label": "LABEL_0", "score": 0.504}]     )

						__SCREAMING_SNAKE_CASE		     =       text_classifier("This is great !"    ,    top_k=2     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__     )    ,    [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}]     )

						__SCREAMING_SNAKE_CASE		     =       text_classifier(["This is great !", "This is bad"]    ,    top_k=2     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__     )    ,    [
						        [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
						        [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
						    ]    ,    )

						__SCREAMING_SNAKE_CASE		     =       text_classifier("This is great !"    ,    top_k=1     )
						self.assertEqual(nested_simplify(UpperCAmelCase__     )    ,    [{"label": "LABEL_0", "score": 0.504}]     )

						# Legacy behavior
						__SCREAMING_SNAKE_CASE		     =       text_classifier("This is great !"    ,    return_all_scores=UpperCAmelCase__     )
						self.assertEqual(nested_simplify(UpperCAmelCase__     )    ,    [{"label": "LABEL_0", "score": 0.504}]     )

						__SCREAMING_SNAKE_CASE		     =       text_classifier("This is great !"    ,    return_all_scores=UpperCAmelCase__     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__     )    ,    [[{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}]]     )

						__SCREAMING_SNAKE_CASE		     =       text_classifier(["This is great !", "Something else"]    ,    return_all_scores=UpperCAmelCase__     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__     )    ,    [
						        [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
						        [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
						    ]    ,    )

						__SCREAMING_SNAKE_CASE		     =       text_classifier(["This is great !", "Something else"]    ,    return_all_scores=UpperCAmelCase__     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__     )    ,    [
						        {"label": "LABEL_0", "score": 0.504},
						        {"label": "LABEL_0", "score": 0.504},
						    ]    ,    )




	@require_torch
	def 						UpperCAmelCase_   (				self    :       Union[str, Any]     ) ->			Optional[int]:
						import torch

						__SCREAMING_SNAKE_CASE		     =       pipeline(
						    task="text-classification"    ,    model="hf-internal-testing/tiny-random-distilbert"    ,    framework="pt"    ,    device=torch.device("cpu"     )    ,    )

						__SCREAMING_SNAKE_CASE		     =       text_classifier("This is great !"     )
						self.assertEqual(nested_simplify(UpperCAmelCase__     )    ,    [{"label": "LABEL_0", "score": 0.504}]     )




	@require_tf
	def 						UpperCAmelCase_   (				self    :       Dict     ) ->			Optional[int]:
						__SCREAMING_SNAKE_CASE		     =       pipeline(
						    task="text-classification"    ,    model="hf-internal-testing/tiny-random-distilbert"    ,    framework="tf"     )

						__SCREAMING_SNAKE_CASE		     =       text_classifier("This is great !"     )
						self.assertEqual(nested_simplify(UpperCAmelCase__     )    ,    [{"label": "LABEL_0", "score": 0.504}]     )




	@slow
	@require_torch
	def 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			int:
						__SCREAMING_SNAKE_CASE		     =       pipeline("text-classification"     )

						__SCREAMING_SNAKE_CASE		     =       text_classifier("This is great !"     )
						self.assertEqual(nested_simplify(UpperCAmelCase__     )    ,    [{"label": "POSITIVE", "score": 1.0}]     )
						__SCREAMING_SNAKE_CASE		     =       text_classifier("This is bad !"     )
						self.assertEqual(nested_simplify(UpperCAmelCase__     )    ,    [{"label": "NEGATIVE", "score": 1.0}]     )
						__SCREAMING_SNAKE_CASE		     =       text_classifier("Birds are a type of animal"     )
						self.assertEqual(nested_simplify(UpperCAmelCase__     )    ,    [{"label": "POSITIVE", "score": 0.988}]     )




	@slow
	@require_tf
	def 						UpperCAmelCase_   (				self    :       List[str]     ) ->			List[str]:
						__SCREAMING_SNAKE_CASE		     =       pipeline("text-classification"    ,    framework="tf"     )

						__SCREAMING_SNAKE_CASE		     =       text_classifier("This is great !"     )
						self.assertEqual(nested_simplify(UpperCAmelCase__     )    ,    [{"label": "POSITIVE", "score": 1.0}]     )
						__SCREAMING_SNAKE_CASE		     =       text_classifier("This is bad !"     )
						self.assertEqual(nested_simplify(UpperCAmelCase__     )    ,    [{"label": "NEGATIVE", "score": 1.0}]     )
						__SCREAMING_SNAKE_CASE		     =       text_classifier("Birds are a type of animal"     )
						self.assertEqual(nested_simplify(UpperCAmelCase__     )    ,    [{"label": "POSITIVE", "score": 0.988}]     )




	def 						UpperCAmelCase_   (				self    :       Any    ,    UpperCAmelCase__    :       str    ,    UpperCAmelCase__    :       Any    ,    UpperCAmelCase__    :       Tuple     ) ->			Dict:
						__SCREAMING_SNAKE_CASE		     =       TextClassificationPipeline(model=UpperCAmelCase__    ,    tokenizer=UpperCAmelCase__     )
						return text_classifier, ["HuggingFace is in", "This is another test"]




	def 						UpperCAmelCase_   (				self    :       str    ,    UpperCAmelCase__    :       Optional[int]    ,    UpperCAmelCase__    :       Optional[int]     ) ->			Dict:
						__SCREAMING_SNAKE_CASE		     =       text_classifier.model
						# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
						__SCREAMING_SNAKE_CASE		     =       "HuggingFace is in"
						__SCREAMING_SNAKE_CASE		     =       text_classifier(UpperCAmelCase__     )

						self.assertEqual(nested_simplify(UpperCAmelCase__     )    ,    [{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}]     )
						self.assertTrue(outputs[0]["label"] in model.config.idalabel.values()     )

						__SCREAMING_SNAKE_CASE		     =       ["HuggingFace is in ", "Paris is in France"]
						__SCREAMING_SNAKE_CASE		     =       text_classifier(UpperCAmelCase__     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__     )    ,    [{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}, {"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}]    ,    )
						self.assertTrue(outputs[0]["label"] in model.config.idalabel.values()     )
						self.assertTrue(outputs[1]["label"] in model.config.idalabel.values()     )

						# Forcing to get all results with `top_k=None`
						# This is NOT the legacy format
						__SCREAMING_SNAKE_CASE		     =       text_classifier(UpperCAmelCase__    ,    top_k=UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       len(model.config.idalabel.values()     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__     )    ,    [[{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}] * N, [{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}] * N]    ,    )

						__SCREAMING_SNAKE_CASE		     =       {"text": "HuggingFace is in ", "text_pair": "Paris is in France"}
						__SCREAMING_SNAKE_CASE		     =       text_classifier(UpperCAmelCase__     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__     )    ,    {"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}    ,    )
						self.assertTrue(outputs["label"] in model.config.idalabel.values()     )

						# This might be used a text pair, but tokenizer + pipe interaction
						# makes it hard to understand that it's not using the pair properly
						# https://github.com/huggingface/transformers/issues/17305
						# We disabled this usage instead as it was outputting wrong outputs.
						__SCREAMING_SNAKE_CASE		     =       [["HuggingFace is in ", "Paris is in France"]]
						with self.assertRaises(UpperCAmelCase__     ):
											text_classifier(UpperCAmelCase__     )

						# This used to be valid for doing text pairs
						# We're keeping it working because of backward compatibility
						__SCREAMING_SNAKE_CASE		     =       text_classifier([[["HuggingFace is in ", "Paris is in France"]]]     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__     )    ,    [{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}]    ,    )
						self.assertTrue(outputs[0]["label"] in model.config.idalabel.values()     )
