






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.
lowerCamelCase	:							Optional[Any]				={'''LayoutLMv2Config''', '''LayoutLMv3Config'''}





@is_pipeline_test
class 			__a  (    unittest.TestCase       ):
 _lowerCAmelCase       :       Tuple        = MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
 _lowerCAmelCase       :       Dict        = TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

 if model_mapping is not None:
  _lowerCAmelCase       :       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:
  _lowerCAmelCase       :       Tuple        = {
      config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
  }







 @require_torch
 def 					__lowercase					(		self					:   Dict						):


      '''simple docstring'''






      UpperCamelCase__			:   Dict       				=  pipeline(
          task="text-classification"							,   model="hf-internal-testing/tiny-random-distilbert"							,   framework="pt"						)

      UpperCamelCase__			:   List[Any]       				=  text_classifier("This is great !"						)
      self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE						)							,   [{"label": "LABEL_0", "score": 0.5_0_4}]						)

      UpperCamelCase__			:   int       				=  text_classifier("This is great !"							,   top_k=2						)
      self.assertEqual(
          nested_simplify(SCREAMING_SNAKE_CASE						)							,   [{"label": "LABEL_0", "score": 0.5_0_4}, {"label": "LABEL_1", "score": 0.4_9_6}]						)

      UpperCamelCase__			:   List[Any]       				=  text_classifier(["This is great !", "This is bad"]							,   top_k=2						)
      self.assertEqual(
          nested_simplify(SCREAMING_SNAKE_CASE						)							,   [
              [{"label": "LABEL_0", "score": 0.5_0_4}, {"label": "LABEL_1", "score": 0.4_9_6}],
              [{"label": "LABEL_0", "score": 0.5_0_4}, {"label": "LABEL_1", "score": 0.4_9_6}],
          ]							,   )

      UpperCamelCase__			:   List[Any]       				=  text_classifier("This is great !"							,   top_k=1						)
      self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE						)							,   [{"label": "LABEL_0", "score": 0.5_0_4}]						)

      # Legacy behavior
      UpperCamelCase__			:   List[Any]       				=  text_classifier("This is great !"							,   return_all_scores=SCREAMING_SNAKE_CASE						)
      self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE						)							,   [{"label": "LABEL_0", "score": 0.5_0_4}]						)

      UpperCamelCase__			:   Dict       				=  text_classifier("This is great !"							,   return_all_scores=SCREAMING_SNAKE_CASE						)
      self.assertEqual(
          nested_simplify(SCREAMING_SNAKE_CASE						)							,   [[{"label": "LABEL_0", "score": 0.5_0_4}, {"label": "LABEL_1", "score": 0.4_9_6}]]						)

      UpperCamelCase__			:   Any       				=  text_classifier(["This is great !", "Something else"]							,   return_all_scores=SCREAMING_SNAKE_CASE						)
      self.assertEqual(
          nested_simplify(SCREAMING_SNAKE_CASE						)							,   [
              [{"label": "LABEL_0", "score": 0.5_0_4}, {"label": "LABEL_1", "score": 0.4_9_6}],
              [{"label": "LABEL_0", "score": 0.5_0_4}, {"label": "LABEL_1", "score": 0.4_9_6}],
          ]							,   )

      UpperCamelCase__			:   List[Any]       				=  text_classifier(["This is great !", "Something else"]							,   return_all_scores=SCREAMING_SNAKE_CASE						)
      self.assertEqual(
          nested_simplify(SCREAMING_SNAKE_CASE						)							,   [
              {"label": "LABEL_0", "score": 0.5_0_4},
              {"label": "LABEL_0", "score": 0.5_0_4},
          ]							,   )







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


      '''simple docstring'''






      import torch

      UpperCamelCase__			:   Union[str, Any]       				=  pipeline(
          task="text-classification"							,   model="hf-internal-testing/tiny-random-distilbert"							,   framework="pt"							,   device=torch.device("cpu"						)							,   )

      UpperCamelCase__			:   Optional[int]       				=  text_classifier("This is great !"						)
      self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE						)							,   [{"label": "LABEL_0", "score": 0.5_0_4}]						)







 @require_tf
 def 					__lowercase					(		self					:   Any						):


      '''simple docstring'''






      UpperCamelCase__			:   str       				=  pipeline(
          task="text-classification"							,   model="hf-internal-testing/tiny-random-distilbert"							,   framework="tf"						)

      UpperCamelCase__			:   List[str]       				=  text_classifier("This is great !"						)
      self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE						)							,   [{"label": "LABEL_0", "score": 0.5_0_4}]						)







 @slow
 @require_torch
 def 					__lowercase					(		self					:   List[str]						):


      '''simple docstring'''






      UpperCamelCase__			:   str       				=  pipeline("text-classification"						)

      UpperCamelCase__			:   Optional[int]       				=  text_classifier("This is great !"						)
      self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE						)							,   [{"label": "POSITIVE", "score": 1.0}]						)
      UpperCamelCase__			:   str       				=  text_classifier("This is bad !"						)
      self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE						)							,   [{"label": "NEGATIVE", "score": 1.0}]						)
      UpperCamelCase__			:   List[Any]       				=  text_classifier("Birds are a type of animal"						)
      self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE						)							,   [{"label": "POSITIVE", "score": 0.9_8_8}]						)







 @slow
 @require_tf
 def 					__lowercase					(		self					:   Any						):


      '''simple docstring'''






      UpperCamelCase__			:   str       				=  pipeline("text-classification"							,   framework="tf"						)

      UpperCamelCase__			:   List[str]       				=  text_classifier("This is great !"						)
      self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE						)							,   [{"label": "POSITIVE", "score": 1.0}]						)
      UpperCamelCase__			:   int       				=  text_classifier("This is bad !"						)
      self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE						)							,   [{"label": "NEGATIVE", "score": 1.0}]						)
      UpperCamelCase__			:   str       				=  text_classifier("Birds are a type of animal"						)
      self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE						)							,   [{"label": "POSITIVE", "score": 0.9_8_8}]						)







 def 					__lowercase					(		self					:   Tuple							,   SCREAMING_SNAKE_CASE					:   Dict							,   SCREAMING_SNAKE_CASE					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   List[str]						):


      '''simple docstring'''






      UpperCamelCase__			:   Any       				=  TextClassificationPipeline(model=SCREAMING_SNAKE_CASE							,   tokenizer=SCREAMING_SNAKE_CASE						)
      return text_classifier, ["HuggingFace is in", "This is another test"]







 def 					__lowercase					(		self					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   Optional[Any]							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]						):


      '''simple docstring'''






      UpperCamelCase__			:   Dict       				=  text_classifier.model
      # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
      UpperCamelCase__			:   Optional[Any]       				=  "HuggingFace is in"
      UpperCamelCase__			:   List[str]       				=  text_classifier(SCREAMING_SNAKE_CASE						)

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

      UpperCamelCase__			:   Union[str, Any]       				=  ["HuggingFace is in ", "Paris is in France"]
      UpperCamelCase__			:   Optional[Any]       				=  text_classifier(SCREAMING_SNAKE_CASE						)
      self.assertEqual(
          nested_simplify(SCREAMING_SNAKE_CASE						)							,   [{"label": ANY(SCREAMING_SNAKE_CASE						), "score": ANY(SCREAMING_SNAKE_CASE						)}, {"label": ANY(SCREAMING_SNAKE_CASE						), "score": ANY(SCREAMING_SNAKE_CASE						)}]							,   )
      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
      UpperCamelCase__			:   Tuple       				=  text_classifier(SCREAMING_SNAKE_CASE							,   top_k=SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   Dict       				=  len(model.config.idalabel.values()						)
      self.assertEqual(
          nested_simplify(SCREAMING_SNAKE_CASE						)							,   [[{"label": ANY(SCREAMING_SNAKE_CASE						), "score": ANY(SCREAMING_SNAKE_CASE						)}] * N, [{"label": ANY(SCREAMING_SNAKE_CASE						), "score": ANY(SCREAMING_SNAKE_CASE						)}] * N]							,   )

      UpperCamelCase__			:   Union[str, Any]       				=  {"text": "HuggingFace is in ", "text_pair": "Paris is in France"}
      UpperCamelCase__			:   int       				=  text_classifier(SCREAMING_SNAKE_CASE						)
      self.assertEqual(
          nested_simplify(SCREAMING_SNAKE_CASE						)							,   {"label": ANY(SCREAMING_SNAKE_CASE						), "score": ANY(SCREAMING_SNAKE_CASE						)}							,   )
      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.
      UpperCamelCase__			:   List[Any]       				=  [["HuggingFace is in ", "Paris is in France"]]
      with self.assertRaises(SCREAMING_SNAKE_CASE						):
           text_classifier(SCREAMING_SNAKE_CASE						)

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







from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase	:							Optional[int]				=logging.get_logger(__name__)

lowerCamelCase	:							Dict				={
    '''facebook/s2t-wav2vec2-large-en-de''': (
        '''https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json'''
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}





class 			__a  (    A__       ):
 _lowerCAmelCase       :       Tuple        = '''speech_to_text_2'''
 _lowerCAmelCase       :       Dict        = ['''past_key_values''']
 _lowerCAmelCase       :       Any        = {'''num_attention_heads''': '''decoder_attention_heads''', '''hidden_size''': '''d_model'''}







 def __init__(		self					:   List[str]							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=1_00_00							,   SCREAMING_SNAKE_CASE					:   List[Any]=6							,   SCREAMING_SNAKE_CASE					:   List[Any]=20_48							,   SCREAMING_SNAKE_CASE					:   Dict=4							,   SCREAMING_SNAKE_CASE					:   List[Any]=0.0							,   SCREAMING_SNAKE_CASE					:   Optional[int]=True							,   SCREAMING_SNAKE_CASE					:   List[Any]="relu"							,   SCREAMING_SNAKE_CASE					:   Tuple=2_56							,   SCREAMING_SNAKE_CASE					:   List[str]=0.1							,   SCREAMING_SNAKE_CASE					:   Tuple=0.0							,   SCREAMING_SNAKE_CASE					:   Any=0.0							,   SCREAMING_SNAKE_CASE					:   int=0.0_2							,   SCREAMING_SNAKE_CASE					:   Dict=2							,   SCREAMING_SNAKE_CASE					:   List[str]=True							,   SCREAMING_SNAKE_CASE					:   Any=1							,   SCREAMING_SNAKE_CASE					:   List[Any]=0							,   SCREAMING_SNAKE_CASE					:   Any=2							,   SCREAMING_SNAKE_CASE					:   str=10_24							,   **SCREAMING_SNAKE_CASE					:   int							,   ):


      '''simple docstring'''






      UpperCamelCase__			:   int       				=  vocab_size
      UpperCamelCase__			:   Optional[Any]       				=  d_model
      UpperCamelCase__			:   Optional[Any]       				=  decoder_ffn_dim
      UpperCamelCase__			:   str       				=  decoder_layers
      UpperCamelCase__			:   Any       				=  decoder_attention_heads
      UpperCamelCase__			:   List[str]       				=  dropout
      UpperCamelCase__			:   int       				=  attention_dropout
      UpperCamelCase__			:   Optional[int]       				=  activation_dropout
      UpperCamelCase__			:   Union[str, Any]       				=  activation_function
      UpperCamelCase__			:   Tuple       				=  init_std
      UpperCamelCase__			:   Optional[int]       				=  decoder_layerdrop
      UpperCamelCase__			:   Dict       				=  use_cache
      UpperCamelCase__			:   str       				=  decoder_layers
      UpperCamelCase__			:   Union[str, Any]       				=  scale_embedding  # scale factor will be sqrt(d_model) if True
      UpperCamelCase__			:   Optional[Any]       				=  max_target_positions

      super().__init__(
          pad_token_id=SCREAMING_SNAKE_CASE							,   bos_token_id=SCREAMING_SNAKE_CASE							,   eos_token_id=SCREAMING_SNAKE_CASE							,   decoder_start_token_id=SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE							,   )