





import itertools
import random
import unittest

import numpy as np

from transformers import WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaConfig, WavaVecaFeatureExtractor
from transformers.testing_utils import require_torch, slow

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


UpperCAmelCase     :     str							=	random.Random()
def 					_A	(		SCREAMING_SNAKE_CASE					:       List[Any]	,	SCREAMING_SNAKE_CASE					:       int=1.0	,	SCREAMING_SNAKE_CASE					:       Any=None	,	SCREAMING_SNAKE_CASE					:       Union[str, Any]=None						):





  """simple docstring"""



  if rng is None:
    a__     : int  =global_rng

  a__     : List[Any]  =[]
  for batch_idx in range(shape[0]						):
    values.append([]						)
    for _ in range(shape[1]						):
      values[-1].append(rng.random() * scale						)

  return values







class     __lowerCAmelCase	(   unittest.TestCase):







      def __init__(   self		,				lowerCAmelCase__		,				lowerCAmelCase__=7		,				lowerCAmelCase__=4_0_0		,				lowerCAmelCase__=2_0_0_0		,				lowerCAmelCase__=1		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__=1_6_0_0_0		,				lowerCAmelCase__=True		,				lowerCAmelCase__=True		,				)       ->						Union[str, Any]:



        '''simple docstring'''




        a__     : Any  =parent
        a__     : Any  =batch_size
        a__     : Optional[int]  =min_seq_length
        a__     : Optional[Any]  =max_seq_length
        a__     : List[Any]  =(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
        a__     : List[Any]  =feature_size
        a__     : Any  =padding_value
        a__     : List[Any]  =sampling_rate
        a__     : Tuple  =return_attention_mask
        a__     : List[str]  =do_normalize







      def        _lowercase  (   self							)       ->						Tuple:



        '''simple docstring'''




        return {
            "feature_size": self.feature_size,
            "padding_value": self.padding_value,
            "sampling_rate": self.sampling_rate,
            "return_attention_mask": self.return_attention_mask,
            "do_normalize": self.do_normalize,
        }







      def        _lowercase  (   self		,				lowerCAmelCase__=False		,				lowerCAmelCase__=False							)       ->						Dict:



        '''simple docstring'''




        def _flatten(lowerCAmelCase__							):
          return list(itertools.chain(*lowerCAmelCase__							)							)

        if equal_length:
          a__     : int  =floats_list((self.batch_size, self.max_seq_length)							)
        else:
          # make sure that inputs increase in size
          a__     : Tuple  =[
              _flatten(floats_list((x, self.feature_size)							)							)
              for x in range(self.min_seq_length		,				self.max_seq_length		,				self.seq_length_diff							)
          ]

        if numpify:
          a__     : Optional[Any]  =[np.asarray(lowerCAmelCase__							) for x in speech_inputs]

        return speech_inputs







class     __lowerCAmelCase	(   UpperCamelCase__						,    unittest.TestCase):
      _lowercase    : Optional[int]            =      WavaVecaFeatureExtractor







      def        _lowercase  (   self							)       ->						Optional[int]:



        '''simple docstring'''




        a__     : Union[str, Any]  =WavaVecaFeatureExtractionTester(self							)







      def        _lowercase  (   self		,				lowerCAmelCase__							)       ->						List[Any]:



        '''simple docstring'''




        self.assertTrue(np.all(np.mean(lowerCAmelCase__		,				axis=0							) < 1E-3							)							)
        self.assertTrue(np.all(np.abs(np.var(lowerCAmelCase__		,				axis=0							) - 1							) < 1E-3							)							)







      def        _lowercase  (   self							)       ->						int:



        '''simple docstring'''




        a__     : Any  =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
        # create three inputs of length 800, 1000, and 1200
        a__     : int  =[floats_list((1, x)							)[0] for x in range(8_0_0		,				1_4_0_0		,				2_0_0							)]
        a__     : Tuple  =[np.asarray(lowerCAmelCase__							) for speech_input in speech_inputs]

        # Test not batched input
        a__     : Any  =feat_extract(speech_inputs[0]		,				return_tensors="np"							).input_values
        a__     : Dict  =feat_extract(np_speech_inputs[0]		,				return_tensors="np"							).input_values
        self.assertTrue(np.allclose(lowerCAmelCase__		,				lowerCAmelCase__		,				atol=1E-3							)							)

        # Test batched
        a__     : str  =feat_extract(lowerCAmelCase__		,				return_tensors="np"							).input_values
        a__     : Optional[Any]  =feat_extract(lowerCAmelCase__		,				return_tensors="np"							).input_values
        for enc_seq_a, enc_seq_a in zip(lowerCAmelCase__		,				lowerCAmelCase__							):
          self.assertTrue(np.allclose(lowerCAmelCase__		,				lowerCAmelCase__		,				atol=1E-3							)							)

        # Test 2-D numpy arrays are batched.
        a__     : List[str]  =[floats_list((1, x)							)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
        a__     : Tuple  =np.asarray(lowerCAmelCase__							)
        a__     : Optional[int]  =feat_extract(lowerCAmelCase__		,				return_tensors="np"							).input_values
        a__     : List[str]  =feat_extract(lowerCAmelCase__		,				return_tensors="np"							).input_values
        for enc_seq_a, enc_seq_a in zip(lowerCAmelCase__		,				lowerCAmelCase__							):
          self.assertTrue(np.allclose(lowerCAmelCase__		,				lowerCAmelCase__		,				atol=1E-3							)							)







      def        _lowercase  (   self							)       ->						Dict:



        '''simple docstring'''




        a__     : Union[str, Any]  =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
        a__     : Optional[int]  =[floats_list((1, x)							)[0] for x in range(8_0_0		,				1_4_0_0		,				2_0_0							)]

        a__     : List[Any]  =["longest", "max_length", "do_not_pad"]
        a__     : Optional[int]  =[None, 1_6_0_0, None]
        for max_length, padding in zip(lowerCAmelCase__		,				lowerCAmelCase__							):
          a__     : Union[str, Any]  =feat_extract(lowerCAmelCase__		,				padding=lowerCAmelCase__		,				max_length=lowerCAmelCase__		,				return_tensors="np"							)
          a__     : Dict  =processed.input_values

          self._check_zero_mean_unit_variance(input_values[0][:8_0_0]							)
          self.assertTrue(input_values[0][8_0_0:].sum() < 1E-6							)
          self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0]							)
          self.assertTrue(input_values[0][1_0_0_0:].sum() < 1E-6							)
          self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0]							)







      def        _lowercase  (   self							)       ->						List[Any]:



        '''simple docstring'''




        a__     : Tuple  =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
        a__     : List[str]  =range(8_0_0		,				1_4_0_0		,				2_0_0							)
        a__     : List[str]  =[floats_list((1, x)							)[0] for x in lengths]

        a__     : Tuple  =["longest", "max_length", "do_not_pad"]
        a__     : Tuple  =[None, 1_6_0_0, None]

        for max_length, padding in zip(lowerCAmelCase__		,				lowerCAmelCase__							):
          a__     : Optional[Any]  =feat_extract(lowerCAmelCase__		,				max_length=lowerCAmelCase__		,				padding=lowerCAmelCase__							)
          a__     : str  =processed.input_values

          self._check_zero_mean_unit_variance(input_values[0][:8_0_0]							)
          self._check_zero_mean_unit_variance(input_values[1][:1_0_0_0]							)
          self._check_zero_mean_unit_variance(input_values[2][:1_2_0_0]							)







      def        _lowercase  (   self							)       ->						Dict:



        '''simple docstring'''




        a__     : Optional[int]  =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
        a__     : Union[str, Any]  =[floats_list((1, x)							)[0] for x in range(8_0_0		,				1_4_0_0		,				2_0_0							)]
        a__     : Any  =feat_extract(
            lowerCAmelCase__		,				truncation=lowerCAmelCase__		,				max_length=1_0_0_0		,				padding="max_length"		,				return_tensors="np"							)
        a__     : str  =processed.input_values

        self._check_zero_mean_unit_variance(input_values[0, :8_0_0]							)
        self._check_zero_mean_unit_variance(input_values[1]							)
        self._check_zero_mean_unit_variance(input_values[2]							)







      def        _lowercase  (   self							)       ->						int:



        '''simple docstring'''




        a__     : List[Any]  =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
        a__     : List[Any]  =[floats_list((1, x)							)[0] for x in range(8_0_0		,				1_4_0_0		,				2_0_0							)]
        a__     : str  =feat_extract(
            lowerCAmelCase__		,				truncation=lowerCAmelCase__		,				max_length=1_0_0_0		,				padding="longest"		,				return_tensors="np"							)
        a__     : Optional[int]  =processed.input_values

        self._check_zero_mean_unit_variance(input_values[0, :8_0_0]							)
        self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0]							)
        self._check_zero_mean_unit_variance(input_values[2]							)

        # make sure that if max_length < longest -> then pad to max_length
        self.assertTrue(input_values.shape == (3, 1_0_0_0)							)

        a__     : Any  =[floats_list((1, x)							)[0] for x in range(8_0_0		,				1_4_0_0		,				2_0_0							)]
        a__     : Dict  =feat_extract(
            lowerCAmelCase__		,				truncation=lowerCAmelCase__		,				max_length=2_0_0_0		,				padding="longest"		,				return_tensors="np"							)
        a__     : Tuple  =processed.input_values

        self._check_zero_mean_unit_variance(input_values[0, :8_0_0]							)
        self._check_zero_mean_unit_variance(input_values[1, :1_0_0_0]							)
        self._check_zero_mean_unit_variance(input_values[2]							)

        # make sure that if max_length > longest -> then pad to longest
        self.assertTrue(input_values.shape == (3, 1_2_0_0)							)







      @require_torch
      def        _lowercase  (   self							)       ->						Optional[int]:



        '''simple docstring'''




        import torch

        a__     : Dict  =self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()							)
        a__     : List[Any]  =np.random.rand(1_0_0							).astype(np.floataa							)
        a__     : Optional[int]  =np_speech_inputs.tolist()

        for inputs in [py_speech_inputs, np_speech_inputs]:
          a__     : Any  =feature_extractor.pad([{"input_values": inputs}]		,				return_tensors="np"							)
          self.assertTrue(np_processed.input_values.dtype == np.floataa							)
          a__     : List[Any]  =feature_extractor.pad([{"input_values": inputs}]		,				return_tensors="pt"							)
          self.assertTrue(pt_processed.input_values.dtype == torch.floataa							)







      @slow
      @require_torch
      def        _lowercase  (   self							)       ->						Dict:



        '''simple docstring'''




        for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
          a__     : Optional[Any]  =WavaVecaConfig.from_pretrained(lowerCAmelCase__							)
          a__     : Any  =WavaVecaFeatureExtractor.from_pretrained(lowerCAmelCase__							)

          # only "layer" feature extraction norm should make use of
          # attention_mask
          self.assertEqual(feat_extract.return_attention_mask		,				config.feat_extract_norm == "layer"							)













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.
UpperCAmelCase     :     List[Any]							=	{"""LayoutLMv2Config""", """LayoutLMv3Config"""}







@is_pipeline_test
class     __lowerCAmelCase	(   unittest.TestCase):
      _lowercase    : str            =      MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
      _lowercase    : Tuple            =      TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

      if model_mapping is not None:
            _lowercase    : Dict            =      {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
      if tf_model_mapping is not None:
            _lowercase    : int            =      {
                config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
            }







      @require_torch
      def        _lowercase  (   self							)       ->						Tuple:



        '''simple docstring'''




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

        a__     : Any  =text_classifier("This is great !"							)
        self.assertEqual(nested_simplify(lowerCAmelCase__							)		,				[{"label": "LABEL_0", "score": 0.5_04}]							)

        a__     : Any  =text_classifier("This is great !"		,				top_k=2							)
        self.assertEqual(
            nested_simplify(lowerCAmelCase__							)		,				[{"label": "LABEL_0", "score": 0.5_04}, {"label": "LABEL_1", "score": 0.4_96}]							)

        a__     : Tuple  =text_classifier(["This is great !", "This is bad"]		,				top_k=2							)
        self.assertEqual(
            nested_simplify(lowerCAmelCase__							)		,				[
                [{"label": "LABEL_0", "score": 0.5_04}, {"label": "LABEL_1", "score": 0.4_96}],
                [{"label": "LABEL_0", "score": 0.5_04}, {"label": "LABEL_1", "score": 0.4_96}],
            ]		,				)

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

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

        a__     : List[str]  =text_classifier("This is great !"		,				return_all_scores=lowerCAmelCase__							)
        self.assertEqual(
            nested_simplify(lowerCAmelCase__							)		,				[[{"label": "LABEL_0", "score": 0.5_04}, {"label": "LABEL_1", "score": 0.4_96}]]							)

        a__     : Optional[int]  =text_classifier(["This is great !", "Something else"]		,				return_all_scores=lowerCAmelCase__							)
        self.assertEqual(
            nested_simplify(lowerCAmelCase__							)		,				[
                [{"label": "LABEL_0", "score": 0.5_04}, {"label": "LABEL_1", "score": 0.4_96}],
                [{"label": "LABEL_0", "score": 0.5_04}, {"label": "LABEL_1", "score": 0.4_96}],
            ]		,				)

        a__     : int  =text_classifier(["This is great !", "Something else"]		,				return_all_scores=lowerCAmelCase__							)
        self.assertEqual(
            nested_simplify(lowerCAmelCase__							)		,				[
                {"label": "LABEL_0", "score": 0.5_04},
                {"label": "LABEL_0", "score": 0.5_04},
            ]		,				)







      @require_torch
      def        _lowercase  (   self							)       ->						List[Any]:



        '''simple docstring'''




        import torch

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

        a__     : Optional[Any]  =text_classifier("This is great !"							)
        self.assertEqual(nested_simplify(lowerCAmelCase__							)		,				[{"label": "LABEL_0", "score": 0.5_04}]							)







      @require_tf
      def        _lowercase  (   self							)       ->						Union[str, Any]:



        '''simple docstring'''




        a__     : Optional[int]  =pipeline(
            task="text-classification"		,				model="hf-internal-testing/tiny-random-distilbert"		,				framework="tf"							)

        a__     : Optional[Any]  =text_classifier("This is great !"							)
        self.assertEqual(nested_simplify(lowerCAmelCase__							)		,				[{"label": "LABEL_0", "score": 0.5_04}]							)







      @slow
      @require_torch
      def        _lowercase  (   self							)       ->						str:



        '''simple docstring'''




        a__     : Union[str, Any]  =pipeline("text-classification"							)

        a__     : Union[str, Any]  =text_classifier("This is great !"							)
        self.assertEqual(nested_simplify(lowerCAmelCase__							)		,				[{"label": "POSITIVE", "score": 1.0}]							)
        a__     : Optional[Any]  =text_classifier("This is bad !"							)
        self.assertEqual(nested_simplify(lowerCAmelCase__							)		,				[{"label": "NEGATIVE", "score": 1.0}]							)
        a__     : Dict  =text_classifier("Birds are a type of animal"							)
        self.assertEqual(nested_simplify(lowerCAmelCase__							)		,				[{"label": "POSITIVE", "score": 0.9_88}]							)







      @slow
      @require_tf
      def        _lowercase  (   self							)       ->						Optional[int]:



        '''simple docstring'''




        a__     : Tuple  =pipeline("text-classification"		,				framework="tf"							)

        a__     : str  =text_classifier("This is great !"							)
        self.assertEqual(nested_simplify(lowerCAmelCase__							)		,				[{"label": "POSITIVE", "score": 1.0}]							)
        a__     : str  =text_classifier("This is bad !"							)
        self.assertEqual(nested_simplify(lowerCAmelCase__							)		,				[{"label": "NEGATIVE", "score": 1.0}]							)
        a__     : Dict  =text_classifier("Birds are a type of animal"							)
        self.assertEqual(nested_simplify(lowerCAmelCase__							)		,				[{"label": "POSITIVE", "score": 0.9_88}]							)







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__							)       ->						Optional[int]:



        '''simple docstring'''




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







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__							)       ->						List[str]:



        '''simple docstring'''




        a__     : Tuple  =text_classifier.model
        # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
        a__     : List[Any]  ="HuggingFace is in"
        a__     : int  =text_classifier(lowerCAmelCase__							)

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

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

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

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






