



import json
import os
import shutil
import tempfile
import unittest

from transformers import is_speech_available, is_torch_available
from transformers.models.speechta import SpeechTaTokenizer
from transformers.testing_utils import get_tests_dir, require_torch
from transformers.utils import FEATURE_EXTRACTOR_NAME


if is_speech_available() and is_torch_available():
    from transformers import SpeechTaFeatureExtractor, SpeechTaProcessor

    from .test_feature_extraction_speechta import floats_list


__UpperCAmelCase      = get_tests_dir('''fixtures/test_sentencepiece_bpe_char.model''')



@require_torch
class        lowerCAmelCase_	(						unittest.TestCase							):
       def       snake_case_ (    self )  ->	int:
          UpperCamelCase					:						List[Any]					=			tempfile.mkdtemp()

          UpperCamelCase					:						str					=			SpeechTaTokenizer(SCREAMING_SNAKE_CASE_ )
          tokenizer.save_pretrained(self.tmpdirname )

          UpperCamelCase					:						Union[str, Any]					=			{
              'feature_size': 1,
              'padding_value': 0.0,
              'sampling_rate': 1_6000,
              'do_normalize': False,
              'num_mel_bins': 80,
              'hop_length': 16,
              'win_length': 64,
              'win_function': 'hann_window',
              'fmin': 80,
              'fmax': 7600,
              'mel_floor': 1e-10,
              'reduction_factor': 2,
              'return_attention_mask': True,
          }

          UpperCamelCase					:						List[Any]					=			os.path.join(self.tmpdirname,    SCREAMING_SNAKE_CASE_ )
          with open(self.feature_extraction_file,    'w',    encoding='utf-8' ) as fp:
             fp.write(json.dumps(SCREAMING_SNAKE_CASE_ ) + '\n' )
       def       snake_case_ (    self,    **SCREAMING_SNAKE_CASE_ )  ->	str:
          return SpeechTaTokenizer.from_pretrained(self.tmpdirname,    **SCREAMING_SNAKE_CASE_ )
       def       snake_case_ (    self,    **SCREAMING_SNAKE_CASE_ )  ->	List[Any]:
          return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname,    **SCREAMING_SNAKE_CASE_ )
       def       snake_case_ (    self )  ->	int:
          shutil.rmtree(self.tmpdirname )
       def       snake_case_ (    self )  ->	Dict:
          UpperCamelCase					:						Optional[int]					=			self.get_tokenizer()
          UpperCamelCase					:						List[Any]					=			self.get_feature_extractor()

          UpperCamelCase					:						str					=			SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_,    feature_extractor=SCREAMING_SNAKE_CASE_ )

          processor.save_pretrained(self.tmpdirname )
          UpperCamelCase					:						Union[str, Any]					=			SpeechTaProcessor.from_pretrained(self.tmpdirname )

          self.assertEqual(processor.tokenizer.get_vocab(),    tokenizer.get_vocab() )
          self.assertIsInstance(processor.tokenizer,    SCREAMING_SNAKE_CASE_ )

          self.assertEqual(processor.feature_extractor.to_json_string(),    feature_extractor.to_json_string() )
          self.assertIsInstance(processor.feature_extractor,    SCREAMING_SNAKE_CASE_ )
       def       snake_case_ (    self )  ->	str:
          UpperCamelCase					:						int					=			SpeechTaProcessor(tokenizer=self.get_tokenizer(),    feature_extractor=self.get_feature_extractor() )
          processor.save_pretrained(self.tmpdirname )

          UpperCamelCase					:						List[Any]					=			self.get_tokenizer(bos_token='(BOS)',    eos_token='(EOS)' )
          UpperCamelCase					:						Tuple					=			self.get_feature_extractor(do_normalize=SCREAMING_SNAKE_CASE_,    padding_value=1.0 )

          UpperCamelCase					:						str					=			SpeechTaProcessor.from_pretrained(
              self.tmpdirname,    bos_token='(BOS)',    eos_token='(EOS)',    do_normalize=SCREAMING_SNAKE_CASE_,    padding_value=1.0 )

          self.assertEqual(processor.tokenizer.get_vocab(),    tokenizer_add_kwargs.get_vocab() )
          self.assertIsInstance(processor.tokenizer,    SCREAMING_SNAKE_CASE_ )

          self.assertEqual(processor.feature_extractor.to_json_string(),    feature_extractor_add_kwargs.to_json_string() )
          self.assertIsInstance(processor.feature_extractor,    SCREAMING_SNAKE_CASE_ )
       def       snake_case_ (    self )  ->	str:
          UpperCamelCase					:						Optional[Any]					=			self.get_feature_extractor()
          UpperCamelCase					:						Dict					=			self.get_tokenizer()

          UpperCamelCase					:						int					=			SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_,    feature_extractor=SCREAMING_SNAKE_CASE_ )

          UpperCamelCase					:						Union[str, Any]					=			floats_list((3, 1000) )

          UpperCamelCase					:						Tuple					=			feature_extractor(audio=SCREAMING_SNAKE_CASE_,    return_tensors='np' )
          UpperCamelCase					:						Union[str, Any]					=			processor(audio=SCREAMING_SNAKE_CASE_,    return_tensors='np' )

          for key in input_feat_extract.keys():
             self.assertAlmostEqual(input_feat_extract[key].sum(),    input_processor[key].sum(),    delta=1e-2 )
       def       snake_case_ (    self )  ->	int:
          UpperCamelCase					:						Optional[int]					=			self.get_feature_extractor()
          UpperCamelCase					:						int					=			self.get_tokenizer()

          UpperCamelCase					:						Any					=			SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_,    feature_extractor=SCREAMING_SNAKE_CASE_ )

          UpperCamelCase					:						Optional[Any]					=			floats_list((3, 1000) )

          UpperCamelCase					:						Dict					=			feature_extractor(audio_target=SCREAMING_SNAKE_CASE_,    return_tensors='np' )
          UpperCamelCase					:						Any					=			processor(audio_target=SCREAMING_SNAKE_CASE_,    return_tensors='np' )

          for key in input_feat_extract.keys():
             self.assertAlmostEqual(input_feat_extract[key].sum(),    input_processor[key].sum(),    delta=1e-2 )
       def       snake_case_ (    self )  ->	int:
          UpperCamelCase					:						List[str]					=			self.get_feature_extractor()
          UpperCamelCase					:						List[str]					=			self.get_tokenizer()

          UpperCamelCase					:						Union[str, Any]					=			SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_,    feature_extractor=SCREAMING_SNAKE_CASE_ )

          UpperCamelCase					:						int					=			'This is a test string'

          UpperCamelCase					:						List[Any]					=			processor(text=SCREAMING_SNAKE_CASE_ )
          UpperCamelCase					:						Dict					=			tokenizer(SCREAMING_SNAKE_CASE_ )

          for key in encoded_tok.keys():
             self.assertListEqual(encoded_tok[key],    encoded_processor[key] )
       def       snake_case_ (    self )  ->	Optional[Any]:
          UpperCamelCase					:						Dict					=			self.get_feature_extractor()
          UpperCamelCase					:						Tuple					=			self.get_tokenizer()

          UpperCamelCase					:						Optional[int]					=			SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_,    feature_extractor=SCREAMING_SNAKE_CASE_ )

          UpperCamelCase					:						List[Any]					=			'This is a test string'

          UpperCamelCase					:						int					=			processor(text_target=SCREAMING_SNAKE_CASE_ )
          UpperCamelCase					:						List[Any]					=			tokenizer(SCREAMING_SNAKE_CASE_ )

          for key in encoded_tok.keys():
             self.assertListEqual(encoded_tok[key],    encoded_processor[key] )
       def       snake_case_ (    self )  ->	Any:
          UpperCamelCase					:						List[Any]					=			self.get_feature_extractor()
          UpperCamelCase					:						List[Any]					=			self.get_tokenizer()

          UpperCamelCase					:						Union[str, Any]					=			SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_,    feature_extractor=SCREAMING_SNAKE_CASE_ )

          UpperCamelCase					:						Optional[int]					=			[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

          UpperCamelCase					:						str					=			processor.batch_decode(SCREAMING_SNAKE_CASE_ )
          UpperCamelCase					:						str					=			tokenizer.batch_decode(SCREAMING_SNAKE_CASE_ )

          self.assertListEqual(SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_ )






       def       snake_case_ (    self )  ->	Dict:
          UpperCamelCase					:						Dict					=			self.get_feature_extractor()
          UpperCamelCase					:						Optional[int]					=			self.get_tokenizer()

          UpperCamelCase					:						str					=			SpeechTaProcessor(tokenizer=SCREAMING_SNAKE_CASE_,    feature_extractor=SCREAMING_SNAKE_CASE_ )

          self.assertListEqual(
              processor.model_input_names,    feature_extractor.model_input_names,    msg='`processor` and `feature_extractor` model input names do not match',    )








import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import ViltImageProcessor



class        lowerCAmelCase_	(						unittest.TestCase							):
       def __init__(    self,    SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_=7,    SCREAMING_SNAKE_CASE_=3,    SCREAMING_SNAKE_CASE_=18,    SCREAMING_SNAKE_CASE_=30,    SCREAMING_SNAKE_CASE_=400,    SCREAMING_SNAKE_CASE_=True,    SCREAMING_SNAKE_CASE_=None,    SCREAMING_SNAKE_CASE_=2,    SCREAMING_SNAKE_CASE_=True,    SCREAMING_SNAKE_CASE_=[0.5, 0.5, 0.5],    SCREAMING_SNAKE_CASE_=[0.5, 0.5, 0.5],    )  ->	List[Any]:
          UpperCamelCase					:						Optional[int]					=			size if size is not None else {'shortest_edge': 30}
          UpperCamelCase					:						Dict					=			parent
          UpperCamelCase					:						str					=			batch_size
          UpperCamelCase					:						str					=			num_channels
          UpperCamelCase					:						Optional[int]					=			image_size
          UpperCamelCase					:						List[Any]					=			min_resolution
          UpperCamelCase					:						Tuple					=			max_resolution
          UpperCamelCase					:						Tuple					=			do_resize
          UpperCamelCase					:						Any					=			size
          UpperCamelCase					:						int					=			size_divisor
          UpperCamelCase					:						str					=			do_normalize
          UpperCamelCase					:						Optional[int]					=			image_mean
          UpperCamelCase					:						List[str]					=			image_std
       def       snake_case_ (    self )  ->	List[Any]:
          return {
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "do_normalize": self.do_normalize,
              "do_resize": self.do_resize,
              "size": self.size,
              "size_divisor": self.size_divisor,
          }






       def       snake_case_ (    self,    SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_=False )  ->	Union[str, Any]:
          if not batched:
             UpperCamelCase					:						Optional[Any]					=			self.size['shortest_edge']
             UpperCamelCase					:						int					=			image_inputs[0]
             if isinstance(SCREAMING_SNAKE_CASE_,    Image.Image ):
                UpperCamelCase			,  UpperCamelCase					:						List[Any]					=			image.size
             else:
                UpperCamelCase			,  UpperCamelCase					:						List[Any]					=			image.shape[1], image.shape[2]
             UpperCamelCase					:						Any					=			size / min(SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_ )
             if h < w:
                UpperCamelCase			,  UpperCamelCase					:						Optional[int]					=			size, scale * w
             else:
                UpperCamelCase			,  UpperCamelCase					:						List[str]					=			scale * h, size

             UpperCamelCase					:						int					=			int((1333 / 800) * size )
             if max(SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_ ) > max_size:
                UpperCamelCase					:						Any					=			max_size / max(SCREAMING_SNAKE_CASE_,    SCREAMING_SNAKE_CASE_ )
                UpperCamelCase					:						List[Any]					=			newh * scale
                UpperCamelCase					:						Optional[int]					=			neww * scale

             UpperCamelCase			,  UpperCamelCase					:						List[Any]					=			int(newh + 0.5 ), int(neww + 0.5 )
             UpperCamelCase			,  UpperCamelCase					:						int					=			(
                 newh // self.size_divisor * self.size_divisor,
                 neww // self.size_divisor * self.size_divisor,
             )

          else:
             UpperCamelCase					:						Dict					=			[]
             for image in image_inputs:
                UpperCamelCase			,  UpperCamelCase					:						Union[str, Any]					=			self.get_expected_values([image] )
                expected_values.append((expected_height, expected_width) )
             UpperCamelCase					:						List[str]					=			max(SCREAMING_SNAKE_CASE_,    key=lambda SCREAMING_SNAKE_CASE_ : item[0] )[0]
             UpperCamelCase					:						List[str]					=			max(SCREAMING_SNAKE_CASE_,    key=lambda SCREAMING_SNAKE_CASE_ : item[1] )[1]

          return expected_height, expected_width






@require_torch
@require_vision
class        lowerCAmelCase_	(						a__ ,					unittest.TestCase							):
       UpperCAmelCase__       :      List[Any]       =						ViltImageProcessor if is_vision_available() else None
       def       snake_case_ (    self )  ->	Union[str, Any]:
          UpperCamelCase					:						Dict					=			ViltImageProcessingTester(self )
       @property
       def       snake_case_ (    self )  ->	Dict:
          return self.image_processor_tester.prepare_image_processor_dict()
       def       snake_case_ (    self )  ->	Union[str, Any]:
          UpperCamelCase					:						List[str]					=			self.image_processing_class(**self.image_processor_dict )
          self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    'image_mean' ) )
          self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    'image_std' ) )
          self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    'do_normalize' ) )
          self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    'do_resize' ) )
          self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    'size' ) )
          self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_,    'size_divisor' ) )
       def       snake_case_ (    self )  ->	List[Any]:
          UpperCamelCase					:						Union[str, Any]					=			self.image_processing_class.from_dict(self.image_processor_dict )
          self.assertEqual(image_processor.size,    {'shortest_edge': 30} )

          UpperCamelCase					:						Union[str, Any]					=			self.image_processing_class.from_dict(self.image_processor_dict,    size=42 )
          self.assertEqual(image_processor.size,    {'shortest_edge': 42} )
       def       snake_case_ (    self )  ->	Dict:
          pass
       def       snake_case_ (    self )  ->	str:
          # Initialize image_processing
          UpperCamelCase					:						int					=			self.image_processing_class(**self.image_processor_dict )
          # create random PIL images
          UpperCamelCase					:						Any					=			prepare_image_inputs(self.image_processor_tester,    equal_resolution=SCREAMING_SNAKE_CASE_ )
          for image in image_inputs:
             self.assertIsInstance(SCREAMING_SNAKE_CASE_,    Image.Image )

          # Test not batched input
          UpperCamelCase					:						int					=			image_processing(image_inputs[0],    return_tensors='pt' ).pixel_values

          UpperCamelCase			,  UpperCamelCase					:						Dict					=			self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_ )
          self.assertEqual(
              encoded_images.shape,    (1, self.image_processor_tester.num_channels, expected_height, expected_width),    )

          # Test batched
          UpperCamelCase					:						str					=			image_processing(SCREAMING_SNAKE_CASE_,    return_tensors='pt' ).pixel_values

          UpperCamelCase			,  UpperCamelCase					:						Tuple					=			self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_,    batched=SCREAMING_SNAKE_CASE_ )
          self.assertEqual(
              encoded_images.shape,    (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  expected_height,
                  expected_width,
              ),    )
       def       snake_case_ (    self )  ->	List[str]:
          # Initialize image_processing
          UpperCamelCase					:						Optional[int]					=			self.image_processing_class(**self.image_processor_dict )
          # create random numpy tensors
          UpperCamelCase					:						Optional[Any]					=			prepare_image_inputs(self.image_processor_tester,    equal_resolution=SCREAMING_SNAKE_CASE_,    numpify=SCREAMING_SNAKE_CASE_ )
          for image in image_inputs:
             self.assertIsInstance(SCREAMING_SNAKE_CASE_,    np.ndarray )

          # Test not batched input
          UpperCamelCase					:						Optional[Any]					=			image_processing(image_inputs[0],    return_tensors='pt' ).pixel_values

          UpperCamelCase			,  UpperCamelCase					:						str					=			self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_ )
          self.assertEqual(
              encoded_images.shape,    (1, self.image_processor_tester.num_channels, expected_height, expected_width),    )

          # Test batched
          UpperCamelCase					:						Dict					=			image_processing(SCREAMING_SNAKE_CASE_,    return_tensors='pt' ).pixel_values

          UpperCamelCase			,  UpperCamelCase					:						Optional[Any]					=			self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_,    batched=SCREAMING_SNAKE_CASE_ )
          self.assertEqual(
              encoded_images.shape,    (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  expected_height,
                  expected_width,
              ),    )






       def       snake_case_ (    self )  ->	Optional[Any]:
          # Initialize image_processing
          UpperCamelCase					:						Optional[int]					=			self.image_processing_class(**self.image_processor_dict )
          # create random PyTorch tensors
          UpperCamelCase					:						Dict					=			prepare_image_inputs(self.image_processor_tester,    equal_resolution=SCREAMING_SNAKE_CASE_,    torchify=SCREAMING_SNAKE_CASE_ )
          for image in image_inputs:
             self.assertIsInstance(SCREAMING_SNAKE_CASE_,    torch.Tensor )

          # Test not batched input
          UpperCamelCase					:						List[str]					=			image_processing(image_inputs[0],    return_tensors='pt' ).pixel_values

          UpperCamelCase			,  UpperCamelCase					:						Union[str, Any]					=			self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_ )
          self.assertEqual(
              encoded_images.shape,    (1, self.image_processor_tester.num_channels, expected_height, expected_width),    )

          # Test batched
          UpperCamelCase					:						List[Any]					=			image_processing(SCREAMING_SNAKE_CASE_,    return_tensors='pt' ).pixel_values

          UpperCamelCase			,  UpperCamelCase					:						Dict					=			self.image_processor_tester.get_expected_values(SCREAMING_SNAKE_CASE_,    batched=SCREAMING_SNAKE_CASE_ )
          self.assertEqual(
              encoded_images.shape,    (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  expected_height,
                  expected_width,
              ),    )



