

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 _a (       SCREAMING_SNAKE_CASE		:			int    ,					SCREAMING_SNAKE_CASE		:			Union[str, Any]=1.0    ,					SCREAMING_SNAKE_CASE		:			List[str]=None    ,					SCREAMING_SNAKE_CASE		:			List[str]=None		)    ->    Union[str, Any]:

 """simple docstring"""






 if rng is None:
  __lowerCAmelCase:   Any   	=      global_rng

 __lowerCAmelCase:   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 							A_ (		unittest.TestCase					):


     def __init__(							self			:		Tuple			,  UpperCAmelCase			:		List[Any]			,  UpperCAmelCase			:		int=7			,  UpperCAmelCase			:		List[str]=4_0_0			,  UpperCAmelCase			:		Any=2_0_0_0			,  UpperCAmelCase			:		str=2_4			,  UpperCAmelCase			:		Optional[int]=2_4			,  UpperCAmelCase			:		Dict=0.0			,  UpperCAmelCase			:		Optional[Any]=1_6_0_0_0			,  UpperCAmelCase			:		Any=True			,  UpperCAmelCase			:		Optional[int]=True			,  )							->	str:
      __lowerCAmelCase:   str   	=      parent
      __lowerCAmelCase:   str   	=      batch_size
      __lowerCAmelCase:   Optional[Any]   	=      min_seq_length
      __lowerCAmelCase:   List[str]   	=      max_seq_length
      __lowerCAmelCase:   int   	=      (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
      __lowerCAmelCase:   Optional[Any]   	=      feature_size
      __lowerCAmelCase:   Optional[int]   	=      num_mel_bins
      __lowerCAmelCase:   List[Any]   	=      padding_value
      __lowerCAmelCase:   List[str]   	=      sampling_rate
      __lowerCAmelCase:   Any   	=      return_attention_mask
      __lowerCAmelCase:   Union[str, Any]   	=      do_normalize


     def   UpperCAmelCase  (							self			:		List[Any]					)							->	Any:
      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			:		Any			,  UpperCAmelCase			:		Dict=False			,  UpperCAmelCase			:		Tuple=False					)							->	List[Any]:
      def _flatten(UpperCAmelCase			:		List[Any]					):
       return list(itertools.chain(*_lowerCamelCase					)					)

      if equal_length:
       __lowerCAmelCase:   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
       __lowerCAmelCase:   Dict   	=      [
           floats_list((x, self.feature_size)					)
           for x in range(self.min_seq_length			,  self.max_seq_length			,  self.seq_length_diff					)
       ]
      if numpify:
       __lowerCAmelCase:   int   	=      [np.asarray(_lowerCamelCase					) for x in speech_inputs]
      return speech_inputs







@require_torch
@require_torchaudio
class 							A_ (		a_							,	unittest.TestCase					):
     _lowercase			:     int								=	SpeechaTextFeatureExtractor if is_speech_available() else None


     def   UpperCAmelCase  (							self			:		Dict					)							->	Any:
      __lowerCAmelCase:   str   	=      SpeechaTextFeatureExtractionTester(self					)


     def   UpperCAmelCase  (							self			:		Union[str, Any]			,  UpperCAmelCase			:		List[Any]					)							->	Union[str, Any]:
      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[int]					)							->	Tuple:
      # Tests that all call wrap to encode_plus and batch_encode_plus
      __lowerCAmelCase:   List[str]   	=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
      # create three inputs of length 800, 1000, and 1200
      __lowerCAmelCase:   List[Any]   	=      [floats_list((1, x)					)[0] for x in range(8_0_0			,  1_4_0_0			,  2_0_0					)]
      __lowerCAmelCase:   Optional[int]   	=      [np.asarray(_lowerCamelCase					) for speech_input in speech_inputs]

      # Test feature size
      __lowerCAmelCase:   int   	=      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
      __lowerCAmelCase:   List[Any]   	=      feature_extractor(speech_inputs[0]			,  return_tensors='np'					).input_features
      __lowerCAmelCase:   Optional[int]   	=      feature_extractor(np_speech_inputs[0]			,  return_tensors='np'					).input_features
      self.assertTrue(np.allclose(_lowerCamelCase			,  _lowerCamelCase			,  atol=1E-3					)					)

      # Test batched
      __lowerCAmelCase:   Optional[Any]   	=      feature_extractor(_lowerCamelCase			,  return_tensors='np'					).input_features
      __lowerCAmelCase:   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					)					)

      # Test 2-D numpy arrays are batched.
      __lowerCAmelCase:   Any   	=      [floats_list((1, x)					)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
      __lowerCAmelCase:   int   	=      np.asarray(_lowerCamelCase					)
      __lowerCAmelCase:   Dict   	=      feature_extractor(_lowerCamelCase			,  return_tensors='np'					).input_features
      __lowerCAmelCase:   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					)					)


     def   UpperCAmelCase  (							self			:		List[Any]					)							->	Tuple:
      __lowerCAmelCase:   List[str]   	=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
      __lowerCAmelCase:   int   	=      [floats_list((1, x)					)[0] for x in range(8_0_0			,  1_4_0_0			,  2_0_0					)]

      __lowerCAmelCase:   List[str]   	=      ['longest', 'max_length', 'do_not_pad']
      __lowerCAmelCase:   Any   	=      [None, 1_6, None]
      for max_length, padding in zip(_lowerCamelCase			,  _lowerCamelCase					):
       __lowerCAmelCase:   Any   	=      feature_extractor(
           _lowerCamelCase			,  padding=_lowerCamelCase			,  max_length=_lowerCamelCase			,  return_attention_mask=_lowerCamelCase					)
       __lowerCAmelCase:   Tuple   	=      inputs.input_features
       __lowerCAmelCase:   str   	=      inputs.attention_mask
       __lowerCAmelCase:   str   	=      [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]					)							->	List[str]:
      __lowerCAmelCase:   str   	=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
      __lowerCAmelCase:   Any   	=      [floats_list((1, x)					)[0] for x in range(8_0_0			,  1_4_0_0			,  2_0_0					)]

      __lowerCAmelCase:   Optional[Any]   	=      ['longest', 'max_length', 'do_not_pad']
      __lowerCAmelCase:   Tuple   	=      [None, 1_6, None]
      for max_length, padding in zip(_lowerCamelCase			,  _lowerCamelCase					):
       __lowerCAmelCase:   Union[str, Any]   	=      feature_extractor(
           _lowerCamelCase			,  max_length=_lowerCamelCase			,  padding=_lowerCamelCase			,  return_tensors='np'			,  return_attention_mask=_lowerCamelCase					)
       __lowerCAmelCase:   Any   	=      inputs.input_features
       __lowerCAmelCase:   List[str]   	=      inputs.attention_mask
       __lowerCAmelCase:   Any   	=      [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			:		List[Any]					)							->	Tuple:
      __lowerCAmelCase:   int   	=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
      __lowerCAmelCase:   str   	=      [floats_list((1, x)					)[0] for x in range(8_0_0			,  1_4_0_0			,  2_0_0					)]
      __lowerCAmelCase:   Optional[Any]   	=      feature_extractor(
          _lowerCamelCase			,  padding='max_length'			,  max_length=4			,  truncation=_lowerCamelCase			,  return_tensors='np'			,  return_attention_mask=_lowerCamelCase			,  )
      __lowerCAmelCase:   Dict   	=      inputs.input_features
      __lowerCAmelCase:   Tuple   	=      inputs.attention_mask
      __lowerCAmelCase:   List[Any]   	=      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			:		int					)							->	Optional[Any]:
      __lowerCAmelCase:   List[str]   	=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
      __lowerCAmelCase:   Dict   	=      [floats_list((1, x)					)[0] for x in range(8_0_0			,  1_4_0_0			,  2_0_0					)]
      __lowerCAmelCase:   Any   	=      feature_extractor(
          _lowerCamelCase			,  padding='longest'			,  max_length=4			,  truncation=_lowerCamelCase			,  return_tensors='np'			,  return_attention_mask=_lowerCamelCase			,  )
      __lowerCAmelCase:   Optional[int]   	=      inputs.input_features
      __lowerCAmelCase:   str   	=      inputs.attention_mask
      __lowerCAmelCase:   Tuple   	=      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, 2_4)					)

      __lowerCAmelCase:   Union[str, Any]   	=      [floats_list((1, x)					)[0] for x in range(8_0_0			,  1_4_0_0			,  2_0_0					)]
      __lowerCAmelCase:   Dict   	=      feature_extractor(
          _lowerCamelCase			,  padding='longest'			,  max_length=1_6			,  truncation=_lowerCamelCase			,  return_tensors='np'			,  return_attention_mask=_lowerCamelCase			,  )
      __lowerCAmelCase:   str   	=      inputs.input_features
      __lowerCAmelCase:   List[Any]   	=      inputs.attention_mask
      __lowerCAmelCase:   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, 2_4)					)


     def   UpperCAmelCase  (							self			:		Optional[Any]					)							->	List[Any]:
      import torch

      __lowerCAmelCase:   Dict   	=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
      __lowerCAmelCase:   List[str]   	=      np.random.rand(1_0_0			,  3_2					).astype(np.floataa					)
      __lowerCAmelCase:   List[Any]   	=      np_speech_inputs.tolist()

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


     def   UpperCAmelCase  (							self			:		Dict			,  UpperCAmelCase			:		List[str]					)							->	Optional[Any]:
      from datasets import load_dataset

      __lowerCAmelCase:   Tuple   	=      load_dataset('hf-internal-testing/librispeech_asr_dummy'			,  'clean'			,  split='validation'					)
      # automatic decoding with librispeech
      __lowerCAmelCase:   Dict   	=      ds.sort('id'					).select(range(_lowerCamelCase					)					)[:num_samples]['audio']

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


     def   UpperCAmelCase  (							self			:		Any					)							->	str:
      # fmt: off
      __lowerCAmelCase:   Dict   	=      np.array([
          -1.5745, -1.7713, -1.7020, -1.6069, -1.2250, -1.1105, -0.9072, -0.8241,
          -1.2310, -0.8098, -0.3320, -0.4101, -0.7985, -0.4996, -0.8213, -0.9128,
          -1.0420, -1.1286, -1.0440, -0.7999, -0.8405, -1.2275, -1.5443, -1.4625,
      ]					)
      # fmt: on

      __lowerCAmelCase:   Any   	=      self._load_datasamples(1					)
      __lowerCAmelCase:   Dict   	=      self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
      __lowerCAmelCase:   Optional[int]   	=      feature_extractor(_lowerCamelCase			,  return_tensors='pt'					).input_features
      self.assertEquals(input_features.shape			,  (1, 5_8_4, 2_4)					)
      self.assertTrue(np.allclose(input_features[0, 0, :3_0]			,  _lowerCamelCase			,  atol=1E-4					)					)













"""simple docstring"""



import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
    from PIL import Image
else:

    class        a     :







        @staticmethod
        def 			UpperCamelCase_      (			*_lowerCamelCase							,		**_lowerCamelCase       ):
          pass

@is_pipeline_test
@require_torch
@require_vision
class        a     (   unittest.TestCase	):
    UpperCAmelCase_ :     Union[str, Any]	     =MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       pipeline('visual-question-answering'							,		model='hf-internal-testing/tiny-vilt-random-vqa'       )
      lowercase							    =       [
          {
              'image': Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       ),
              'question': 'How many cats are there?',
          },
          {
              'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
              'question': 'How many cats are there?',
          },
      ]
      return vqa_pipeline, examples







    def 			UpperCamelCase_      (			self							,		_lowerCamelCase							,		_lowerCamelCase       ):
      lowercase							    =       vqa_pipeline(_lowerCamelCase							,		top_k=1       )
      self.assertEqual(
          _lowerCamelCase							,		[
              [{'score': ANY(_lowerCamelCase       ), 'answer': ANY(_lowerCamelCase       )}],
              [{'score': ANY(_lowerCamelCase       ), 'answer': ANY(_lowerCamelCase       )}],
          ]							,		)







    @require_torch
    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       pipeline('visual-question-answering'							,		model='hf-internal-testing/tiny-vilt-random-vqa'       )
      lowercase							    =       './tests/fixtures/tests_samples/COCO/000000039769.png'
      lowercase							    =       'How many cats are there?'

      lowercase							    =       vqa_pipeline(image=_lowerCamelCase							,		question='How many cats are there?'							,		top_k=2       )
      self.assertEqual(
          _lowerCamelCase							,		[{'score': ANY(_lowerCamelCase       ), 'answer': ANY(_lowerCamelCase       )}, {'score': ANY(_lowerCamelCase       ), 'answer': ANY(_lowerCamelCase       )}]       )

      lowercase							    =       vqa_pipeline({'image': image, 'question': question}							,		top_k=2       )
      self.assertEqual(
          _lowerCamelCase							,		[{'score': ANY(_lowerCamelCase       ), 'answer': ANY(_lowerCamelCase       )}, {'score': ANY(_lowerCamelCase       ), 'answer': ANY(_lowerCamelCase       )}]       )







    @slow
    @require_torch
    def 			UpperCamelCase_      (			self       ):
      lowercase							    =       pipeline('visual-question-answering'							,		model='dandelin/vilt-b32-finetuned-vqa'       )
      lowercase							    =       './tests/fixtures/tests_samples/COCO/000000039769.png'
      lowercase							    =       'How many cats are there?'

      lowercase							    =       vqa_pipeline(image=_lowerCamelCase							,		question=_lowerCamelCase							,		top_k=2       )
      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[{'score': 0.8_7_9_9, 'answer': '2'}, {'score': 0.2_9_6, 'answer': '1'}]       )

      lowercase							    =       vqa_pipeline({'image': image, 'question': question}							,		top_k=2       )
      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[{'score': 0.8_7_9_9, 'answer': '2'}, {'score': 0.2_9_6, 'answer': '1'}]       )

      lowercase							    =       vqa_pipeline(
          [{'image': image, 'question': question}, {'image': image, 'question': question}]							,		top_k=2       )
      self.assertEqual(
          nested_simplify(_lowerCamelCase							,		decimals=4       )							,		[[{'score': 0.8_7_9_9, 'answer': '2'}, {'score': 0.2_9_6, 'answer': '1'}]] * 2							,		)







    @require_tf
    @unittest.skip('Visual question answering not implemented in TF'       )
    def 			UpperCamelCase_      (			self       ):
      pass


