

'''simple docstring'''




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


_a				: List[str] 							=						get_tests_dir('fixtures/test_sentencepiece_bpe_char.model')



@require_torch
class        lowercase_	(		unittest.TestCase ):

  '''simple docstring'''





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


    """simple docstring"""
    UpperCAmelCase													=	tempfile.mkdtemp()

    UpperCAmelCase													=	SpeechTaTokenizer(a_     )
    tokenizer.save_pretrained(self.tmpdirname     )

    UpperCAmelCase													=	{
        'feature_size': 1,
        'padding_value': 0.0,
        'sampling_rate': 1_6_0_0_0,
        'do_normalize': False,
        'num_mel_bins': 8_0,
        'hop_length': 1_6,
        'win_length': 6_4,
        'win_function': 'hann_window',
        'fmin': 8_0,
        'fmax': 7_6_0_0,
        'mel_floor': 1E-10,
        'reduction_factor': 2,
        'return_attention_mask': True,
    }

    UpperCAmelCase													=	os.path.join(self.tmpdirname   , a_     )
    with open(self.feature_extraction_file   , 'w'   , encoding='utf-8'     ) as fp:
      fp.write(json.dumps(a_     ) + '\n'     )
  def 						snake_case_			(     self   , **a_     )							->				Union[str, Any]:


    """simple docstring"""
    return SpeechTaTokenizer.from_pretrained(self.tmpdirname   , **a_     )
  def 						snake_case_			(     self   , **a_     )							->				List[Any]:


    """simple docstring"""
    return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname   , **a_     )
  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    shutil.rmtree(self.tmpdirname     )
  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    UpperCAmelCase													=	self.get_tokenizer()
    UpperCAmelCase													=	self.get_feature_extractor()

    UpperCAmelCase													=	SpeechTaProcessor(tokenizer=a_   , feature_extractor=a_     )

    processor.save_pretrained(self.tmpdirname     )
    UpperCAmelCase													=	SpeechTaProcessor.from_pretrained(self.tmpdirname     )

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

    self.assertEqual(processor.feature_extractor.to_json_string()   , feature_extractor.to_json_string()     )
    self.assertIsInstance(processor.feature_extractor   , a_     )
  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    UpperCAmelCase													=	SpeechTaProcessor(tokenizer=self.get_tokenizer()   , feature_extractor=self.get_feature_extractor()     )
    processor.save_pretrained(self.tmpdirname     )

    UpperCAmelCase													=	self.get_tokenizer(bos_token='(BOS)'   , eos_token='(EOS)'     )
    UpperCAmelCase													=	self.get_feature_extractor(do_normalize=a_   , padding_value=1.0     )

    UpperCAmelCase													=	SpeechTaProcessor.from_pretrained(
        self.tmpdirname   , bos_token='(BOS)'   , eos_token='(EOS)'   , do_normalize=a_   , padding_value=1.0     )

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

    self.assertEqual(processor.feature_extractor.to_json_string()   , feature_extractor_add_kwargs.to_json_string()     )
    self.assertIsInstance(processor.feature_extractor   , a_     )
  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    UpperCAmelCase													=	self.get_feature_extractor()
    UpperCAmelCase													=	self.get_tokenizer()

    UpperCAmelCase													=	SpeechTaProcessor(tokenizer=a_   , feature_extractor=a_     )

    UpperCAmelCase													=	floats_list((3, 1_0_0_0)     )

    UpperCAmelCase													=	feature_extractor(audio=a_   , return_tensors='np'     )
    UpperCAmelCase													=	processor(audio=a_   , 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     )							->				Any:


    """simple docstring"""
    UpperCAmelCase													=	self.get_feature_extractor()
    UpperCAmelCase													=	self.get_tokenizer()

    UpperCAmelCase													=	SpeechTaProcessor(tokenizer=a_   , feature_extractor=a_     )

    UpperCAmelCase													=	floats_list((3, 1_0_0_0)     )

    UpperCAmelCase													=	feature_extractor(audio_target=a_   , return_tensors='np'     )
    UpperCAmelCase													=	processor(audio_target=a_   , 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     )							->				Any:


    """simple docstring"""
    UpperCAmelCase													=	self.get_feature_extractor()
    UpperCAmelCase													=	self.get_tokenizer()

    UpperCAmelCase													=	SpeechTaProcessor(tokenizer=a_   , feature_extractor=a_     )

    UpperCAmelCase													=	'This is a test string'

    UpperCAmelCase													=	processor(text=a_     )
    UpperCAmelCase													=	tokenizer(a_     )

    for key in encoded_tok.keys():
      self.assertListEqual(encoded_tok[key]   , encoded_processor[key]     )
  def 						snake_case_			(     self     )							->				Union[str, Any]:


    """simple docstring"""
    UpperCAmelCase													=	self.get_feature_extractor()
    UpperCAmelCase													=	self.get_tokenizer()

    UpperCAmelCase													=	SpeechTaProcessor(tokenizer=a_   , feature_extractor=a_     )

    UpperCAmelCase													=	'This is a test string'

    UpperCAmelCase													=	processor(text_target=a_     )
    UpperCAmelCase													=	tokenizer(a_     )

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


    """simple docstring"""
    UpperCAmelCase													=	self.get_feature_extractor()
    UpperCAmelCase													=	self.get_tokenizer()

    UpperCAmelCase													=	SpeechTaProcessor(tokenizer=a_   , feature_extractor=a_     )

    UpperCAmelCase													=	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

    UpperCAmelCase													=	processor.batch_decode(a_     )
    UpperCAmelCase													=	tokenizer.batch_decode(a_     )

    self.assertListEqual(a_   , a_     )





  def 						snake_case_			(     self     )							->				int:


    """simple docstring"""
    UpperCAmelCase													=	self.get_feature_extractor()
    UpperCAmelCase													=	self.get_tokenizer()

    UpperCAmelCase													=	SpeechTaProcessor(tokenizer=a_   , feature_extractor=a_     )

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





'''simple docstring'''




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 MobileNetVaImageProcessor



class        lowercase_	(		unittest.TestCase ):

  '''simple docstring'''





  def __init__(     self   , a_   , a_=7   , a_=3   , a_=1_8   , a_=3_0   , a_=4_0_0   , a_=True   , a_=None   , a_=True   , a_=None   , )							->				List[str]:


    """simple docstring"""
    UpperCAmelCase													=	size if size is not None else {'shortest_edge': 2_0}
    UpperCAmelCase													=	crop_size if crop_size is not None else {'height': 1_8, 'width': 1_8}
    UpperCAmelCase													=	parent
    UpperCAmelCase													=	batch_size
    UpperCAmelCase													=	num_channels
    UpperCAmelCase													=	image_size
    UpperCAmelCase													=	min_resolution
    UpperCAmelCase													=	max_resolution
    UpperCAmelCase													=	do_resize
    UpperCAmelCase													=	size
    UpperCAmelCase													=	do_center_crop
    UpperCAmelCase													=	crop_size





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


    """simple docstring"""
    return {
        "do_resize": self.do_resize,
        "size": self.size,
        "do_center_crop": self.do_center_crop,
        "crop_size": self.crop_size,
    }




@require_torch
@require_vision
class        lowercase_	(		a ,       unittest.TestCase ):

  '''simple docstring'''





  __lowerCAmelCase					: Union[str, Any]      =   MobileNetVaImageProcessor if is_vision_available() else None
  def 						snake_case_			(     self     )							->				Dict:


    """simple docstring"""
    UpperCAmelCase													=	MobileNetVaImageProcessingTester(self     )
  @property
  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    return self.image_processor_tester.prepare_image_processor_dict()
  def 						snake_case_			(     self     )							->				Optional[int]:


    """simple docstring"""
    UpperCAmelCase													=	self.image_processing_class(**self.image_processor_dict     )
    self.assertTrue(hasattr(a_   , 'do_resize'     )     )
    self.assertTrue(hasattr(a_   , 'size'     )     )
    self.assertTrue(hasattr(a_   , 'do_center_crop'     )     )
    self.assertTrue(hasattr(a_   , 'center_crop'     )     )
  def 						snake_case_			(     self     )							->				str:


    """simple docstring"""
    UpperCAmelCase													=	self.image_processing_class.from_dict(self.image_processor_dict     )
    self.assertEqual(image_processor.size   , {'shortest_edge': 2_0}     )
    self.assertEqual(image_processor.crop_size   , {'height': 1_8, 'width': 1_8}     )

    UpperCAmelCase													=	self.image_processing_class.from_dict(self.image_processor_dict   , size=4_2   , crop_size=8_4     )
    self.assertEqual(image_processor.size   , {'shortest_edge': 4_2}     )
    self.assertEqual(image_processor.crop_size   , {'height': 8_4, 'width': 8_4}     )
  def 						snake_case_			(     self     )							->				List[str]:


    """simple docstring"""
    pass
  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    # Initialize image_processing
    UpperCAmelCase													=	self.image_processing_class(**self.image_processor_dict     )
    # create random PIL images
    UpperCAmelCase													=	prepare_image_inputs(self.image_processor_tester   , equal_resolution=a_     )
    for image in image_inputs:
      self.assertIsInstance(a_   , Image.Image     )

    # Test not batched input
    UpperCAmelCase													=	image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )

    # Test batched
    UpperCAmelCase													=	image_processing(a_   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )
  def 						snake_case_			(     self     )							->				str:


    """simple docstring"""
    # Initialize image_processing
    UpperCAmelCase													=	self.image_processing_class(**self.image_processor_dict     )
    # create random numpy tensors
    UpperCAmelCase													=	prepare_image_inputs(self.image_processor_tester   , equal_resolution=a_   , numpify=a_     )
    for image in image_inputs:
      self.assertIsInstance(a_   , np.ndarray     )

    # Test not batched input
    UpperCAmelCase													=	image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )

    # Test batched
    UpperCAmelCase													=	image_processing(a_   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )





  def 						snake_case_			(     self     )							->				List[Any]:


    """simple docstring"""
    # Initialize image_processing
    UpperCAmelCase													=	self.image_processing_class(**self.image_processor_dict     )
    # create random PyTorch tensors
    UpperCAmelCase													=	prepare_image_inputs(self.image_processor_tester   , equal_resolution=a_   , torchify=a_     )
    for image in image_inputs:
      self.assertIsInstance(a_   , torch.Tensor     )

    # Test not batched input
    UpperCAmelCase													=	image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            1,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )

    # Test batched
    UpperCAmelCase													=	image_processing(a_   , return_tensors='pt'     ).pixel_values
    self.assertEqual(
        encoded_images.shape   , (
            self.image_processor_tester.batch_size,
            self.image_processor_tester.num_channels,
            self.image_processor_tester.crop_size['height'],
            self.image_processor_tester.crop_size['width'],
        )   , )


