

"""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_							:			Tuple											= {"LayoutLMv2Config", "LayoutLMv3Config"}





@is_pipeline_test
class    lowerCamelCase						(unittest.TestCase			):
			lowerCamelCase__   :		Dict							=							MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
			lowerCamelCase__   :		Any							=							TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

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

			@require_torch
			def 					SCREAMING_SNAKE_CASE				(					self				:						Dict       )   ->    List[Any]:
						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 					SCREAMING_SNAKE_CASE				(					self				:						List[Any]       )   ->    Optional[Any]:
						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 					SCREAMING_SNAKE_CASE				(					self				:						int       )   ->    Tuple:
						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 					SCREAMING_SNAKE_CASE				(					self				:						Optional[Any]       )   ->    List[Any]:
						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 					SCREAMING_SNAKE_CASE				(					self				:						Any       )   ->    Optional[Any]:
						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 					SCREAMING_SNAKE_CASE				(					self				:						int    ,				__UpperCAmelCase				:						Union[str, Any]    ,				__UpperCAmelCase				:						Tuple    ,				__UpperCAmelCase				:						Union[str, Any]       )   ->    Any:
						SCREAMING_SNAKE_CASE__								=      TextClassificationPipeline(model=__UpperCAmelCase    ,				tokenizer=__UpperCAmelCase       )
						return text_classifier, ["HuggingFace is in", "This is another test"]





			def 					SCREAMING_SNAKE_CASE				(					self				:						Any    ,				__UpperCAmelCase				:						int    ,				__UpperCAmelCase				:						Any       )   ->    Optional[Any]:
						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()       )





"""simple docstring"""




import pytest
import requests

from datasets.utils.file_utils import http_head

from .utils import OfflineSimulationMode, RequestWouldHangIndefinitelyError, offline

@pytest.mark.integration
def 	A       (     ):




			'''simple docstring'''


			with offline(OfflineSimulationMode.CONNECTION_TIMES_OUT    ):
						with pytest.raises(snake_case__    ):
									requests.request("""GET"""		,				"""https://huggingface.co"""    )
						with pytest.raises(requests.exceptions.ConnectTimeout    ):
									requests.request("""GET"""		,				"""https://huggingface.co"""		,				timeout=1.0    )

@pytest.mark.integration
def 	A       (     ):




			'''simple docstring'''


			with offline(OfflineSimulationMode.CONNECTION_FAILS    ):
						with pytest.raises(requests.exceptions.ConnectionError    ):
									requests.request("""GET"""		,				"""https://huggingface.co"""    )




def 	A       (     ):




			'''simple docstring'''


			with offline(OfflineSimulationMode.HF_DATASETS_OFFLINE_SET_TO_1    ):
						with pytest.raises(snake_case__    ):
									http_head("""https://huggingface.co"""    )


