



import itertools
import os
import random
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

from transformers import is_speech_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_speech_available():
  from transformers import WhisperFeatureExtractor

if is_torch_available():
  import torch

snake_case    =	random.Random()



def       lowercase       (   SCREAMING_SNAKE_CASE__       :	List[str]				,       SCREAMING_SNAKE_CASE__       :	List[str]=1.0				,       SCREAMING_SNAKE_CASE__       :	Optional[int]=None				,       SCREAMING_SNAKE_CASE__       :	Optional[int]=None				)  ->     Optional[int]:
 if rng is None:
  _snake_case    :  List[str]					=					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							:			Tuple				,		lowerCAmelCase							:			Optional[int]=7				,		lowerCAmelCase							:			int=400				,		lowerCAmelCase							:			Union[str, Any]=2000				,		lowerCAmelCase							:			Optional[Any]=10				,		lowerCAmelCase							:			Optional[Any]=160				,		lowerCAmelCase							:			Union[str, Any]=8				,		lowerCAmelCase							:			Dict=0.0				,		lowerCAmelCase							:			List[str]=4000				,		lowerCAmelCase							:			Dict=False				,		lowerCAmelCase							:			List[str]=True				,		)				->				List[Any]:

    """simple docstring"""







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

   def      UpperCamelCase_  (					self							:			str)				->				Tuple:

    """simple docstring"""







    return {
        "feature_size": self.feature_size,
        "hop_length": self.hop_length,
        "chunk_length": self.chunk_length,
        "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							:			Optional[int]=False				,		lowerCAmelCase							:			Optional[Any]=False)				->				List[str]:

    """simple docstring"""







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

    if equal_length:
     _snake_case    :  List[Any]					=					[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    :  Any					=					[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_					: str          =		WhisperFeatureExtractor if is_speech_available() else None

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

    """simple docstring"""







    _snake_case    :  Union[str, Any]					=					WhisperFeatureExtractionTester(self)

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

    """simple docstring"""







    _snake_case    :  Optional[Any]					=					self.feature_extraction_class(**self.feat_extract_dict)

    with tempfile.TemporaryDirectory() as tmpdirname:
     _snake_case    :  Optional[Any]					=					feat_extract_first.save_pretrained(lowerCAmelCase)[0]
     check_json_file_has_correct_format(lowerCAmelCase)
     _snake_case    :  List[Any]					=					self.feature_extraction_class.from_pretrained(lowerCAmelCase)

    _snake_case    :  Optional[Any]					=					feat_extract_first.to_dict()
    _snake_case    :  Tuple					=					feat_extract_second.to_dict()
    _snake_case    :  Dict					=					feat_extract_first.mel_filters
    _snake_case    :  Optional[Any]					=					feat_extract_second.mel_filters
    self.assertTrue(np.allclose(lowerCAmelCase				,		lowerCAmelCase))
    self.assertEqual(lowerCAmelCase				,		lowerCAmelCase)

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

    """simple docstring"""







    _snake_case    :  Any					=					self.feature_extraction_class(**self.feat_extract_dict)

    with tempfile.TemporaryDirectory() as tmpdirname:
     _snake_case    :  List[str]					=					os.path.join(lowerCAmelCase				,		"""feat_extract.json""")
     feat_extract_first.to_json_file(lowerCAmelCase)
     _snake_case    :  List[str]					=					self.feature_extraction_class.from_json_file(lowerCAmelCase)

    _snake_case    :  Optional[int]					=					feat_extract_first.to_dict()
    _snake_case    :  List[str]					=					feat_extract_second.to_dict()
    _snake_case    :  Optional[Any]					=					feat_extract_first.mel_filters
    _snake_case    :  Tuple					=					feat_extract_second.mel_filters
    self.assertTrue(np.allclose(lowerCAmelCase				,		lowerCAmelCase))
    self.assertEqual(lowerCAmelCase				,		lowerCAmelCase)

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

    """simple docstring"""







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

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

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

    # Test batched
    _snake_case    :  Tuple					=					feature_extractor(lowerCAmelCase				,		return_tensors="""np""").input_features
    _snake_case    :  List[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    :  Optional[int]					=					[floats_list((1, x))[0] for x in (800, 800, 800)]
    _snake_case    :  str					=					np.asarray(lowerCAmelCase)
    _snake_case    :  Dict					=					feature_extractor(lowerCAmelCase				,		return_tensors="""np""").input_features
    _snake_case    :  Optional[int]					=					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 truncation required
    _snake_case    :  Optional[Any]					=					[floats_list((1, x))[0] for x in range(200				,		(feature_extractor.n_samples + 500)				,		200)]
    _snake_case    :  Dict					=					[np.asarray(lowerCAmelCase) for speech_input in speech_inputs]

    _snake_case    :  int					=					[x[: feature_extractor.n_samples] for x in speech_inputs]
    _snake_case    :  List[str]					=					[np.asarray(lowerCAmelCase) for speech_input in speech_inputs_truncated]

    _snake_case    :  Optional[int]					=					feature_extractor(lowerCAmelCase				,		return_tensors="""np""").input_features
    _snake_case    :  Union[str, 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))

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

    """simple docstring"""







    import torch

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

    for inputs in [py_speech_inputs, np_speech_inputs]:
     _snake_case    :  List[str]					=					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							:			Optional[int]				,		lowerCAmelCase							:			Optional[Any])				->				Any:

    """simple docstring"""







    _snake_case    :  Optional[Any]					=					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							:			Union[str, Any])				->				Optional[Any]:

    """simple docstring"""







    _snake_case    :  str					=					torch.tensor(
        [
            0.1_193, -0.0_946, -0.1_098, -0.0_196, 0.0_225, -0.0_690, -0.1_736, 0.0_951,
            0.0_971, -0.0_817, -0.0_702, 0.0_162, 0.0_260, 0.0_017, -0.0_192, -0.1_678,
            0.0_709, -0.1_867, -0.0_655, -0.0_274, -0.0_234, -0.1_884, -0.0_516, -0.0_554,
            -0.0_274, -0.1_425, -0.1_423, 0.0_837, 0.0_377, -0.0_854
        ])
    # fmt: on

    _snake_case    :  List[str]					=					self._load_datasamples(1)
    _snake_case    :  Union[str, Any]					=					WhisperFeatureExtractor()
    _snake_case    :  Optional[int]					=					feature_extractor(lowerCAmelCase				,		return_tensors="""pt""").input_features
    self.assertEqual(input_features.shape				,		(1, 80, 3000))
    self.assertTrue(torch.allclose(input_features[0, 0, :30]				,		lowerCAmelCase				,		atol=1E-4))


   def      UpperCamelCase_  (					self							:			int)				->				Any:

    """simple docstring"""







    _snake_case    :  Tuple					=					self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
    _snake_case    :  List[str]					=					self._load_datasamples(1)[0]
    _snake_case    :  int					=					((audio - audio.min()) / (audio.max() - audio.min())) * 6_5535  # Rescale to [0, 65535] to show issue
    _snake_case    :  List[Any]					=					feat_extract.zero_mean_unit_var_norm([audio]				,		attention_mask=lowerCAmelCase)[0]

    self.assertTrue(np.all(np.mean(lowerCAmelCase) < 1E-3))
    self.assertTrue(np.all(np.abs(np.var(lowerCAmelCase) - 1) < 1E-3))





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


a__    =	{
    """configuration_canine""": ["""CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP""", """CanineConfig"""],
    """tokenization_canine""": ["""CanineTokenizer"""],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  a__    =	[
      """CANINE_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """CanineForMultipleChoice""",
      """CanineForQuestionAnswering""",
      """CanineForSequenceClassification""",
      """CanineForTokenClassification""",
      """CanineLayer""",
      """CanineModel""",
      """CaninePreTrainedModel""",
      """load_tf_weights_in_canine""",
  ]


if TYPE_CHECKING:
  from .configuration_canine import CANINE_PRETRAINED_CONFIG_ARCHIVE_MAP, CanineConfig
  from .tokenization_canine import CanineTokenizer

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_canine import (
        CANINE_PRETRAINED_MODEL_ARCHIVE_LIST,
        CanineForMultipleChoice,
        CanineForQuestionAnswering,
        CanineForSequenceClassification,
        CanineForTokenClassification,
        CanineLayer,
        CanineModel,
        CaninePreTrainedModel,
        load_tf_weights_in_canine,
    )


else:
  import sys

  a__    =	_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)
