



import itertools
import random
import unittest

import numpy as np

from transformers import is_speech_available
from transformers.testing_utils import require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
  from transformers import SpeechaTextFeatureExtractor

a__    =	random.Random()



def       lowercase       (   SCREAMING_SNAKE_CASE__       :	str				,       SCREAMING_SNAKE_CASE__       :	Any=1.0				,       SCREAMING_SNAKE_CASE__       :	Dict=None				,       SCREAMING_SNAKE_CASE__       :	int=None				)  ->     Any:
 if rng is None:
  _snake_case    :  Dict					=					global_rng

 _snake_case    :  Optional[int]					=					[]
 for batch_idx in range(shape[0]				):
  values.append([]				)
  for _ in range(shape[1]				):
   values[-1].append(rng.random() * scale				)

 return values







@require_torch
@require_torchaudio
class 		snake_case						(	unittest.TestCase  ):



   '''simple docstring'''






   def __init__(					self							:			List[Any]				,		lowerCAmelCase							:			Union[str, Any]				,		lowerCAmelCase							:			Optional[int]=7				,		lowerCAmelCase							:			str=400				,		lowerCAmelCase							:			str=2000				,		lowerCAmelCase							:			int=24				,		lowerCAmelCase							:			Optional[int]=24				,		lowerCAmelCase							:			Optional[Any]=0.0				,		lowerCAmelCase							:			Any=1_6000				,		lowerCAmelCase							:			Optional[Any]=True				,		lowerCAmelCase							:			Dict=True				,		)				->				int:

    """simple docstring"""







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

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

    """simple docstring"""







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


   def      UpperCamelCase_  (					self							:			Tuple				,		lowerCAmelCase							:			List[str]=False				,		lowerCAmelCase							:			str=False)				->				Optional[int]:

    """simple docstring"""







    def _flatten(lowerCAmelCase							:			Tuple):
     return list(itertools.chain(*lowerCAmelCase))

    if equal_length:
     _snake_case    :  int					=					[floats_list((self.max_seq_length, self.feature_size)) for _ in range(self.batch_size)]
    else:
     # make sure that inputs increase in size
     _snake_case    :  Union[str, Any]					=					[
         floats_list((x, self.feature_size))
         for x in range(self.min_seq_length				,		self.max_seq_length				,		self.seq_length_diff)
     ]
    if numpify:
     _snake_case    :  List[str]					=					[np.asarray(lowerCAmelCase) for x in speech_inputs]
    return speech_inputs







@require_torch
@require_torchaudio
class 		snake_case						(	SCREAMING_SNAKE_CASE_					,unittest.TestCase  ):



   '''simple docstring'''






   snake_case_					: List[str]          =		SpeechaTextFeatureExtractor if is_speech_available() else None

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

    """simple docstring"""







    _snake_case    :  List[Any]					=					SpeechaTextFeatureExtractionTester(self)

   def      UpperCamelCase_  (					self							:			List[str]				,		lowerCAmelCase							:			Any)				->				int:

    """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      UpperCamelCase_  (					self							:			Optional[Any])				->				Dict:

    """simple docstring"""







    _snake_case    :  str					=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
    # create three inputs of length 800, 1000, and 1200
    _snake_case    :  Union[str, Any]					=					[floats_list((1, x))[0] for x in range(800				,		1400				,		200)]
    _snake_case    :  Optional[int]					=					[np.asarray(lowerCAmelCase) for speech_input in speech_inputs]

    # Test feature size
    _snake_case    :  Union[str, Any]					=					feature_extractor(lowerCAmelCase				,		padding=lowerCAmelCase				,		return_tensors="""np""").input_features
    self.assertTrue(input_features.ndim == 3)
    self.assertTrue(input_features.shape[-1] == feature_extractor.feature_size)

    # Test not batched input
    _snake_case    :  List[str]					=					feature_extractor(speech_inputs[0]				,		return_tensors="""np""").input_features
    _snake_case    :  List[Any]					=					feature_extractor(np_speech_inputs[0]				,		return_tensors="""np""").input_features
    self.assertTrue(np.allclose(lowerCAmelCase				,		lowerCAmelCase				,		atol=1E-3))

    # Test batched
    _snake_case    :  List[Any]					=					feature_extractor(lowerCAmelCase				,		return_tensors="""np""").input_features
    _snake_case    :  Optional[Any]					=					feature_extractor(lowerCAmelCase				,		return_tensors="""np""").input_features
    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.
    _snake_case    :  Any					=					[floats_list((1, x))[0] for x in (800, 800, 800)]
    _snake_case    :  List[Any]					=					np.asarray(lowerCAmelCase)
    _snake_case    :  List[str]					=					feature_extractor(lowerCAmelCase				,		return_tensors="""np""").input_features
    _snake_case    :  str					=					feature_extractor(lowerCAmelCase				,		return_tensors="""np""").input_features
    for enc_seq_a, enc_seq_a in zip(lowerCAmelCase				,		lowerCAmelCase):
     self.assertTrue(np.allclose(lowerCAmelCase				,		lowerCAmelCase				,		atol=1E-3))

   def      UpperCamelCase_  (					self							:			List[str])				->				Any:

    """simple docstring"""







    _snake_case    :  Dict					=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
    _snake_case    :  Dict					=					[floats_list((1, x))[0] for x in range(800				,		1400				,		200)]

    _snake_case    :  Dict					=					["""longest""", """max_length""", """do_not_pad"""]
    _snake_case    :  int					=					[None, 16, None]
    for max_length, padding in zip(lowerCAmelCase				,		lowerCAmelCase):
     _snake_case    :  int					=					feature_extractor(
         lowerCAmelCase				,		padding=lowerCAmelCase				,		max_length=lowerCAmelCase				,		return_attention_mask=lowerCAmelCase)
     _snake_case    :  List[Any]					=					inputs.input_features
     _snake_case    :  str					=					inputs.attention_mask
     _snake_case    :  Union[str, Any]					=					[np.sum(lowerCAmelCase) for x in attention_mask]

     self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]])
     self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]])
     self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]])

   def      UpperCamelCase_  (					self							:			Optional[int])				->				Optional[Any]:

    """simple docstring"""







    _snake_case    :  Any					=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
    _snake_case    :  Optional[Any]					=					[floats_list((1, x))[0] for x in range(800				,		1400				,		200)]

    _snake_case    :  int					=					["""longest""", """max_length""", """do_not_pad"""]
    _snake_case    :  int					=					[None, 16, None]
    for max_length, padding in zip(lowerCAmelCase				,		lowerCAmelCase):
     _snake_case    :  Optional[int]					=					feature_extractor(
         lowerCAmelCase				,		max_length=lowerCAmelCase				,		padding=lowerCAmelCase				,		return_tensors="""np"""				,		return_attention_mask=lowerCAmelCase)
     _snake_case    :  int					=					inputs.input_features
     _snake_case    :  Tuple					=					inputs.attention_mask
     _snake_case    :  int					=					[np.sum(lowerCAmelCase) for x in attention_mask]

     self._check_zero_mean_unit_variance(input_features[0][: fbank_feat_lengths[0]])
     self.assertTrue(input_features[0][fbank_feat_lengths[0] :].sum() < 1E-6)
     self._check_zero_mean_unit_variance(input_features[1][: fbank_feat_lengths[1]])
     self.assertTrue(input_features[0][fbank_feat_lengths[1] :].sum() < 1E-6)
     self._check_zero_mean_unit_variance(input_features[2][: fbank_feat_lengths[2]])

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

    """simple docstring"""







    _snake_case    :  Dict					=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
    _snake_case    :  Optional[int]					=					[floats_list((1, x))[0] for x in range(800				,		1400				,		200)]
    _snake_case    :  Dict					=					feature_extractor(
        lowerCAmelCase				,		padding="""max_length"""				,		max_length=4				,		truncation=lowerCAmelCase				,		return_tensors="""np"""				,		return_attention_mask=lowerCAmelCase				,		)
    _snake_case    :  List[str]					=					inputs.input_features
    _snake_case    :  List[Any]					=					inputs.attention_mask
    _snake_case    :  Dict					=					np.sum(attention_mask == 1				,		axis=1)

    self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]])
    self._check_zero_mean_unit_variance(input_features[1])
    self._check_zero_mean_unit_variance(input_features[2])

   def      UpperCamelCase_  (					self							:			Any)				->				Any:

    """simple docstring"""







    _snake_case    :  Optional[int]					=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
    _snake_case    :  Union[str, Any]					=					[floats_list((1, x))[0] for x in range(800				,		1400				,		200)]
    _snake_case    :  Optional[int]					=					feature_extractor(
        lowerCAmelCase				,		padding="""longest"""				,		max_length=4				,		truncation=lowerCAmelCase				,		return_tensors="""np"""				,		return_attention_mask=lowerCAmelCase				,		)
    _snake_case    :  Optional[Any]					=					inputs.input_features
    _snake_case    :  Union[str, Any]					=					inputs.attention_mask
    _snake_case    :  Optional[int]					=					np.sum(attention_mask == 1				,		axis=1)

    self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]])
    self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]])
    self._check_zero_mean_unit_variance(input_features[2])

    # make sure that if max_length < longest -> then pad to max_length
    self.assertEqual(input_features.shape				,		(3, 4, 24))

    _snake_case    :  str					=					[floats_list((1, x))[0] for x in range(800				,		1400				,		200)]
    _snake_case    :  Tuple					=					feature_extractor(
        lowerCAmelCase				,		padding="""longest"""				,		max_length=16				,		truncation=lowerCAmelCase				,		return_tensors="""np"""				,		return_attention_mask=lowerCAmelCase				,		)
    _snake_case    :  int					=					inputs.input_features
    _snake_case    :  Any					=					inputs.attention_mask
    _snake_case    :  List[str]					=					np.sum(attention_mask == 1				,		axis=1)

    self._check_zero_mean_unit_variance(input_features[0, : fbank_feat_lengths[0]])
    self._check_zero_mean_unit_variance(input_features[1, : fbank_feat_lengths[1]])
    self._check_zero_mean_unit_variance(input_features[2])

    # make sure that if max_length < longest -> then pad to max_length
    self.assertEqual(input_features.shape				,		(3, 6, 24))

   def      UpperCamelCase_  (					self							:			List[Any])				->				str:

    """simple docstring"""







    import torch

    _snake_case    :  str					=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
    _snake_case    :  Tuple					=					np.random.rand(100				,		32).astype(np.floataa)
    _snake_case    :  List[str]					=					np_speech_inputs.tolist()

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

   def      UpperCamelCase_  (					self							:			Tuple				,		lowerCAmelCase							:			int)				->				Any:

    """simple docstring"""







    from datasets import load_dataset

    _snake_case    :  List[str]					=					load_dataset("""hf-internal-testing/librispeech_asr_dummy"""				,		"""clean"""				,		split="""validation""")
    # automatic decoding with librispeech
    _snake_case    :  Any					=					ds.sort("""id""").select(range(lowerCAmelCase))[:num_samples]["""audio"""]

    return [x["array"] for x in speech_samples]


   def      UpperCamelCase_  (					self							:			Dict)				->				str:

    """simple docstring"""







    _snake_case    :  str					=					np.array([
        -1.5_745, -1.7_713, -1.7_020, -1.6_069, -1.2_250, -1.1_105, -0.9_072, -0.8_241,
        -1.2_310, -0.8_098, -0.3_320, -0.4_101, -0.7_985, -0.4_996, -0.8_213, -0.9_128,
        -1.0_420, -1.1_286, -1.0_440, -0.7_999, -0.8_405, -1.2_275, -1.5_443, -1.4_625,
    ])
    # fmt: on

    _snake_case    :  int					=					self._load_datasamples(1)
    _snake_case    :  List[str]					=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
    _snake_case    :  int					=					feature_extractor(lowerCAmelCase				,		return_tensors="""pt""").input_features
    self.assertEquals(input_features.shape				,		(1, 584, 24))
    self.assertTrue(np.allclose(input_features[0, 0, :30]				,		lowerCAmelCase				,		atol=1E-4))






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding







class 		snake_case						(	SCREAMING_SNAKE_CASE_  ):



   '''simple docstring'''






   snake_case_					: List[str]          =		["""image_processor""", """tokenizer"""]
   snake_case_					: str          =		"""ChineseCLIPImageProcessor"""
   snake_case_					: Tuple          =		("""BertTokenizer""", """BertTokenizerFast""")

   def __init__(					self							:			Any				,		lowerCAmelCase							:			Dict=None				,		lowerCAmelCase							:			List[Any]=None				,		**lowerCAmelCase							:			str)				->				Optional[Any]:

    """simple docstring"""







    _snake_case    :  List[str]					=					None
    if "feature_extractor" in kwargs:
     warnings.warn(
         """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
         """ instead."""				,		lowerCAmelCase				,		)
     _snake_case    :  Tuple					=					kwargs.pop("""feature_extractor""")

    _snake_case    :  Dict					=					image_processor if image_processor is not None else feature_extractor
    if image_processor is None:
     raise ValueError("""You need to specify an `image_processor`.""")
    if tokenizer is None:
     raise ValueError("""You need to specify a `tokenizer`.""")

    super().__init__(lowerCAmelCase				,		lowerCAmelCase)
    _snake_case    :  Optional[int]					=					self.image_processor

   def __call__(					self							:			List[Any]				,		lowerCAmelCase							:			str=None				,		lowerCAmelCase							:			Optional[int]=None				,		lowerCAmelCase							:			List[Any]=None				,		**lowerCAmelCase							:			Optional[int])				->				Optional[Any]:

    """simple docstring"""







    if text is None and images is None:
     raise ValueError("""You have to specify either text or images. Both cannot be none.""")

    if text is not None:
     _snake_case    :  Dict					=					self.tokenizer(lowerCAmelCase				,		return_tensors=lowerCAmelCase				,		**lowerCAmelCase)

    if images is not None:
     _snake_case    :  Any					=					self.image_processor(lowerCAmelCase				,		return_tensors=lowerCAmelCase				,		**lowerCAmelCase)

    if text is not None and images is not None:
     _snake_case    :  Optional[Any]					=					image_features.pixel_values
     return encoding
    elif text is not None:
     return encoding
    else:
     return BatchEncoding(data=dict(**lowerCAmelCase)				,		tensor_type=lowerCAmelCase)

   def      UpperCamelCase_  (					self							:			Dict				,		*lowerCAmelCase							:			Tuple				,		**lowerCAmelCase							:			Any)				->				Optional[Any]:

    """simple docstring"""







    return self.tokenizer.batch_decode(*lowerCAmelCase				,		**lowerCAmelCase)

   def      UpperCamelCase_  (					self							:			Tuple				,		*lowerCAmelCase							:			Tuple				,		**lowerCAmelCase							:			int)				->				str:

    """simple docstring"""







    return self.tokenizer.decode(*lowerCAmelCase				,		**lowerCAmelCase)

   @property
   def      UpperCamelCase_  (					self							:			Union[str, Any])				->				List[str]:

    """simple docstring"""







    _snake_case    :  Dict					=					self.tokenizer.model_input_names
    _snake_case    :  int					=					self.image_processor.model_input_names
    return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names))


   @property
   def      UpperCamelCase_  (					self							:			str)				->				Tuple:

    """simple docstring"""







    warnings.warn(
        """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."""				,		lowerCAmelCase				,		)
    return self.image_processor_class
