





"""simple docstring"""





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

SCREAMING_SNAKE_CASE__					=   random.Random()


def 			UpperCAmelCase__							(  SCREAMING_SNAKE_CASE     :						Any							,						SCREAMING_SNAKE_CASE     :						int=1.0							,						SCREAMING_SNAKE_CASE     :						Tuple=None							,						SCREAMING_SNAKE_CASE     :						Dict=None			):







      '''simple docstring'''




      if rng is None:
            lowerCAmelCase  						=       global_rng

      lowerCAmelCase  						=       []
      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 		lowercase					(					unittest.TestCase					):





      def __init__(       self      ,						lowercase      ,						lowercase=7      ,						lowercase=400      ,						lowercase=2_000      ,						lowercase=10      ,						lowercase=160      ,						lowercase=8      ,						lowercase=0.0      ,						lowercase=4_000      ,						lowercase=False      ,						lowercase=True      ,						)		->     List[str]:
            lowerCAmelCase  						=       parent
            lowerCAmelCase  						=       batch_size
            lowerCAmelCase  						=       min_seq_length
            lowerCAmelCase  						=       max_seq_length
            lowerCAmelCase  						=       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
            lowerCAmelCase  						=       padding_value
            lowerCAmelCase  						=       sampling_rate
            lowerCAmelCase  						=       return_attention_mask
            lowerCAmelCase  						=       do_normalize
            lowerCAmelCase  						=       feature_size
            lowerCAmelCase  						=       chunk_length
            lowerCAmelCase  						=       hop_length





      def     _snake_case			(       self )		->     Optional[Any]:
            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     _snake_case			(       self      ,						lowercase=False      ,						lowercase=False )		->     Optional[Any]:





            def _flatten(lowercase ):
                  return list(itertools.chain(*A_ ) )

            if equal_length:
                  lowerCAmelCase  						=       [floats_list((self.max_seq_length, self.feature_size) ) for _ in range(self.batch_size )]
            else:
                  # make sure that inputs increase in size
                  lowerCAmelCase  						=       [
                      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  						=       [np.asarray(A_ ) for x in speech_inputs]
            return speech_inputs







@require_torch
@require_torchaudio
class 		lowercase					(					_UpperCAmelCase     ,  unittest.TestCase					):
      _SCREAMING_SNAKE_CASE					=      WhisperFeatureExtractor if is_speech_available() else None





      def     _snake_case			(       self )		->     Optional[int]:
            lowerCAmelCase  						=       WhisperFeatureExtractionTester(self )





      def     _snake_case			(       self )		->     int:
            lowerCAmelCase  						=       self.feature_extraction_class(**self.feat_extract_dict )

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

            lowerCAmelCase  						=       feat_extract_first.to_dict()
            lowerCAmelCase  						=       feat_extract_second.to_dict()
            lowerCAmelCase  						=       feat_extract_first.mel_filters
            lowerCAmelCase  						=       feat_extract_second.mel_filters
            self.assertTrue(np.allclose(A_      ,						A_ ) )
            self.assertEqual(A_      ,						A_ )





      def     _snake_case			(       self )		->     str:
            lowerCAmelCase  						=       self.feature_extraction_class(**self.feat_extract_dict )

            with tempfile.TemporaryDirectory() as tmpdirname:
                  lowerCAmelCase  						=       os.path.join(A_      ,						"""feat_extract.json""" )
                  feat_extract_first.to_json_file(A_ )
                  lowerCAmelCase  						=       self.feature_extraction_class.from_json_file(A_ )

            lowerCAmelCase  						=       feat_extract_first.to_dict()
            lowerCAmelCase  						=       feat_extract_second.to_dict()
            lowerCAmelCase  						=       feat_extract_first.mel_filters
            lowerCAmelCase  						=       feat_extract_second.mel_filters
            self.assertTrue(np.allclose(A_      ,						A_ ) )
            self.assertEqual(A_      ,						A_ )





      def     _snake_case			(       self )		->     List[Any]:
            lowerCAmelCase  						=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
            # create three inputs of length 800, 1000, and 1200
            lowerCAmelCase  						=       [floats_list((1, x) )[0] for x in range(800      ,						1_400      ,						200 )]
            lowerCAmelCase  						=       [np.asarray(A_ ) for speech_input in speech_inputs]

            # Test feature size
            lowerCAmelCase  						=       feature_extractor(A_      ,						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
            lowerCAmelCase  						=       feature_extractor(speech_inputs[0]      ,						return_tensors="""np""" ).input_features
            lowerCAmelCase  						=       feature_extractor(np_speech_inputs[0]      ,						return_tensors="""np""" ).input_features
            self.assertTrue(np.allclose(A_      ,						A_      ,						atol=1e-3 ) )

            # Test batched
            lowerCAmelCase  						=       feature_extractor(A_      ,						return_tensors="""np""" ).input_features
            lowerCAmelCase  						=       feature_extractor(A_      ,						return_tensors="""np""" ).input_features
            for enc_seq_a, enc_seq_a in zip(A_      ,						A_ ):
                  self.assertTrue(np.allclose(A_      ,						A_      ,						atol=1e-3 ) )

            # Test 2-D numpy arrays are batched.
            lowerCAmelCase  						=       [floats_list((1, x) )[0] for x in (800, 800, 800)]
            lowerCAmelCase  						=       np.asarray(A_ )
            lowerCAmelCase  						=       feature_extractor(A_      ,						return_tensors="""np""" ).input_features
            lowerCAmelCase  						=       feature_extractor(A_      ,						return_tensors="""np""" ).input_features
            for enc_seq_a, enc_seq_a in zip(A_      ,						A_ ):
                  self.assertTrue(np.allclose(A_      ,						A_      ,						atol=1e-3 ) )

            # Test truncation required
            lowerCAmelCase  						=       [floats_list((1, x) )[0] for x in range(200      ,						(feature_extractor.n_samples + 500)      ,						200 )]
            lowerCAmelCase  						=       [np.asarray(A_ ) for speech_input in speech_inputs]

            lowerCAmelCase  						=       [x[: feature_extractor.n_samples] for x in speech_inputs]
            lowerCAmelCase  						=       [np.asarray(A_ ) for speech_input in speech_inputs_truncated]

            lowerCAmelCase  						=       feature_extractor(A_      ,						return_tensors="""np""" ).input_features
            lowerCAmelCase  						=       feature_extractor(A_      ,						return_tensors="""np""" ).input_features
            for enc_seq_a, enc_seq_a in zip(A_      ,						A_ ):
                  self.assertTrue(np.allclose(A_      ,						A_      ,						atol=1e-3 ) )





      def     _snake_case			(       self )		->     Dict:
            import torch

            lowerCAmelCase  						=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
            lowerCAmelCase  						=       np.random.rand(100      ,						32 ).astype(np.floataa )
            lowerCAmelCase  						=       np_speech_inputs.tolist()

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





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

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





      def     _snake_case			(       self )		->     int:
            lowerCAmelCase  						=       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

            lowerCAmelCase  						=       self._load_datasamples(1 )
            lowerCAmelCase  						=       WhisperFeatureExtractor()
            lowerCAmelCase  						=       feature_extractor(A_      ,						return_tensors="""pt""" ).input_features
            self.assertEqual(input_features.shape      ,						(1, 80, 3_000) )
            self.assertTrue(torch.allclose(input_features[0, 0, :30]      ,						A_      ,						atol=1e-4 ) )





      def     _snake_case			(       self )		->     Any:
            lowerCAmelCase  						=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
            lowerCAmelCase  						=       self._load_datasamples(1 )[0]
            lowerCAmelCase  						=       ((audio - audio.min()) / (audio.max() - audio.min())) * 65_535  # Rescale to [0, 65535] to show issue
            lowerCAmelCase  						=       feat_extract.zero_mean_unit_var_norm([audio]      ,						attention_mask=A_ )[0]

            self.assertTrue(np.all(np.mean(A_ ) < 1e-3 ) )
            self.assertTrue(np.all(np.abs(np.var(A_ ) - 1 ) < 1e-3 ) )









import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_torch, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_torch_available, is_vision_available


if is_torch_available():
 import torch


if is_vision_available():
 from PIL import Image

 from transformers import MgpstrProcessor, ViTImageProcessor







@require_torch
@require_vision
class   __lowercase      (unittest.TestCase ):
 _UpperCamelCase			  =  ViTImageProcessor if is_vision_available() else None
 @property
 def 	UpperCamelCase__  (							self					)					->str:



       '''simple docstring'''





       return self.image_processor_tester.prepare_image_processor_dict()
 def 	UpperCamelCase__  (							self					)					->str:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					(3, 32, 128)
       __lowerCAmelCase					:						List[str]					=					tempfile.mkdtemp()

       # fmt: off
       __lowerCAmelCase					:						List[str]					=					['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z''']
       # fmt: on
       __lowerCAmelCase					:						Optional[int]					=					dict(zip(A_      ,							range(len(A_					)					)					)					)

       __lowerCAmelCase					:						Tuple					=					os.path.join(self.tmpdirname      ,							VOCAB_FILES_NAMES['''vocab_file''']					)
       with open(self.vocab_file      ,							'''w'''      ,							encoding='''utf-8'''					) as fp:
             fp.write(json.dumps(A_					) + '''\n'''					)

       __lowerCAmelCase					:						Union[str, Any]					=					{
           '''do_normalize''': False,
           '''do_resize''': True,
           '''image_processor_type''': '''ViTImageProcessor''',
           '''resample''': 3,
           '''size''': {'''height''': 32, '''width''': 128},
       }
       __lowerCAmelCase					:						Optional[Any]					=					os.path.join(self.tmpdirname      ,							A_					)
       with open(self.image_processor_file      ,							'''w'''      ,							encoding='''utf-8'''					) as fp:
             json.dump(A_      ,							A_					)
 def 	UpperCamelCase__  (							self      ,							**A_					)					->Tuple:



       '''simple docstring'''





       return MgpstrTokenizer.from_pretrained(self.tmpdirname      ,							**A_					)
 def 	UpperCamelCase__  (							self      ,							**A_					)					->Tuple:



       '''simple docstring'''





       return ViTImageProcessor.from_pretrained(self.tmpdirname      ,							**A_					)
 def 	UpperCamelCase__  (							self					)					->Optional[int]:



       '''simple docstring'''





       shutil.rmtree(self.tmpdirname					)
 def 	UpperCamelCase__  (							self					)					->Optional[Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					np.random.randint(255      ,							size=(3, 30, 400)      ,							dtype=np.uinta					)

       __lowerCAmelCase					:						str					=					Image.fromarray(np.moveaxis(A_      ,							0      ,							-1					)					)

       return image_input
 def 	UpperCamelCase__  (							self					)					->Any:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					self.get_tokenizer()
       __lowerCAmelCase					:						List[Any]					=					self.get_image_processor()

       __lowerCAmelCase					:						List[Any]					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)
       processor.save_pretrained(self.tmpdirname					)
       __lowerCAmelCase					:						Union[str, Any]					=					MgpstrProcessor.from_pretrained(self.tmpdirname      ,							use_fast=A_					)

       self.assertEqual(processor.char_tokenizer.get_vocab()      ,							tokenizer.get_vocab()					)
       self.assertIsInstance(processor.char_tokenizer      ,							A_					)

       self.assertEqual(processor.image_processor.to_json_string()      ,							image_processor.to_json_string()					)
       self.assertIsInstance(processor.image_processor      ,							A_					)
 def 	UpperCamelCase__  (							self					)					->Optional[int]:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					self.get_tokenizer()
       __lowerCAmelCase					:						Union[str, Any]					=					self.get_image_processor()

       __lowerCAmelCase					:						List[Any]					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)
       processor.save_pretrained(self.tmpdirname					)

       __lowerCAmelCase					:						List[Any]					=					self.get_tokenizer(bos_token='''(BOS)'''      ,							eos_token='''(EOS)'''					)
       __lowerCAmelCase					:						int					=					self.get_image_processor(do_normalize=A_      ,							padding_value=1.0					)

       __lowerCAmelCase					:						int					=					MgpstrProcessor.from_pretrained(
           self.tmpdirname      ,							bos_token='''(BOS)'''      ,							eos_token='''(EOS)'''      ,							do_normalize=A_      ,							padding_value=1.0					)

       self.assertEqual(processor.char_tokenizer.get_vocab()      ,							tokenizer_add_kwargs.get_vocab()					)
       self.assertIsInstance(processor.char_tokenizer      ,							A_					)

       self.assertEqual(processor.image_processor.to_json_string()      ,							image_processor_add_kwargs.to_json_string()					)
       self.assertIsInstance(processor.image_processor      ,							A_					)
 def 	UpperCamelCase__  (							self					)					->List[str]:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					self.get_image_processor()
       __lowerCAmelCase					:						Optional[Any]					=					self.get_tokenizer()

       __lowerCAmelCase					:						int					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)

       __lowerCAmelCase					:						Optional[int]					=					self.prepare_image_inputs()

       __lowerCAmelCase					:						Optional[Any]					=					image_processor(A_      ,							return_tensors='''np'''					)
       __lowerCAmelCase					:						Tuple					=					processor(images=A_      ,							return_tensors='''np'''					)

       for key in input_image_proc.keys():
             self.assertAlmostEqual(input_image_proc[key].sum()      ,							input_processor[key].sum()      ,							delta=1e-2					)
 def 	UpperCamelCase__  (							self					)					->str:



       '''simple docstring'''





       __lowerCAmelCase					:						str					=					self.get_image_processor()
       __lowerCAmelCase					:						Union[str, Any]					=					self.get_tokenizer()

       __lowerCAmelCase					:						Optional[Any]					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)

       __lowerCAmelCase					:						Any					=					'''test'''

       __lowerCAmelCase					:						Dict					=					processor(text=A_					)

       __lowerCAmelCase					:						str					=					tokenizer(A_					)
       for key in encoded_tok.keys():
             self.assertListEqual(encoded_tok[key]      ,							encoded_processor[key]					)
 def 	UpperCamelCase__  (							self					)					->Optional[int]:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					self.get_image_processor()
       __lowerCAmelCase					:						Any					=					self.get_tokenizer()

       __lowerCAmelCase					:						str					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)

       __lowerCAmelCase					:						List[Any]					=					'''test'''
       __lowerCAmelCase					:						int					=					self.prepare_image_inputs()

       __lowerCAmelCase					:						int					=					processor(text=A_      ,							images=A_					)

       self.assertListEqual(list(inputs.keys()					)      ,							['''pixel_values''', '''labels''']					)

       # test if it raises when no input is passed
       with pytest.raises(A_					):
             processor()
 def 	UpperCamelCase__  (							self					)					->Union[str, Any]:



       '''simple docstring'''





       __lowerCAmelCase					:						List[Any]					=					self.get_image_processor()
       __lowerCAmelCase					:						int					=					self.get_tokenizer()

       __lowerCAmelCase					:						Any					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)

       __lowerCAmelCase					:						List[str]					=					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9], [3, 4, 3, 1, 1, 8, 9]]

       __lowerCAmelCase					:						Optional[int]					=					processor.char_decode(A_					)
       __lowerCAmelCase					:						Tuple					=					tokenizer.batch_decode(A_					)
       __lowerCAmelCase					:						Any					=					[seq.replace(''' '''      ,							''''''					) for seq in decoded_tok]

       self.assertListEqual(A_      ,							A_					)
 def 	UpperCamelCase__  (							self					)					->Any:



       '''simple docstring'''





       __lowerCAmelCase					:						str					=					self.get_image_processor()
       __lowerCAmelCase					:						Any					=					self.get_tokenizer()

       __lowerCAmelCase					:						int					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)

       __lowerCAmelCase					:						Union[str, Any]					=					None
       __lowerCAmelCase					:						Optional[Any]					=					self.prepare_image_inputs()

       __lowerCAmelCase					:						List[Any]					=					processor(text=A_      ,							images=A_					)

       self.assertListEqual(list(inputs.keys()					)      ,							processor.model_input_names					)





 def 	UpperCamelCase__  (							self					)					->List[str]:



       '''simple docstring'''





       __lowerCAmelCase					:						List[str]					=					self.get_image_processor()
       __lowerCAmelCase					:						List[str]					=					self.get_tokenizer()

       __lowerCAmelCase					:						Any					=					MgpstrProcessor(tokenizer=A_      ,							image_processor=A_					)

       __lowerCAmelCase					:						List[Any]					=					torch.randn(1      ,							27      ,							38					)
       __lowerCAmelCase					:						Optional[int]					=					torch.randn(1      ,							27      ,							5_0257					)
       __lowerCAmelCase					:						Optional[Any]					=					torch.randn(1      ,							27      ,							3_0522					)

       __lowerCAmelCase					:						List[str]					=					processor.batch_decode([char_input, bpe_input, wp_input]					)

       self.assertListEqual(list(results.keys()					)      ,							['''generated_text''', '''scores''', '''char_preds''', '''bpe_preds''', '''wp_preds''']					)


