





"""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


lowerCamelCase_					:						Union[str, Any]      = get_tests_dir('fixtures/test_sentencepiece_bpe_char.model')

@require_torch
class 			_UpperCAmelCase  (       unittest.TestCase		):



      '''simple docstring'''

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	Tuple    						=  tempfile.mkdtemp()

        A_  :	Dict    						=  SpeechTaTokenizer(snake_case_  )
        tokenizer.save_pretrained(self.tmpdirname  )

        A_  :	List[str]    						=  {
            '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,
        }

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

      def  lowerCamelCase_      (					self ,				**snake_case_  ):

        """simple docstring"""

        return SpeechTaTokenizer.from_pretrained(self.tmpdirname ,				**snake_case_  )

      def  lowerCamelCase_      (					self ,				**snake_case_  ):

        """simple docstring"""

        return SpeechTaFeatureExtractor.from_pretrained(self.tmpdirname ,				**snake_case_  )

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        shutil.rmtree(self.tmpdirname  )

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	Optional[int]    						=  self.get_tokenizer()
        A_  :	Tuple    						=  self.get_feature_extractor()

        A_  :	Union[str, Any]    						=  SpeechTaProcessor(tokenizer=snake_case_ ,				feature_extractor=snake_case_  )

        processor.save_pretrained(self.tmpdirname  )
        A_  :	Any    						=  SpeechTaProcessor.from_pretrained(self.tmpdirname  )

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

        self.assertEqual(processor.feature_extractor.to_json_string() ,				feature_extractor.to_json_string()  )
        self.assertIsInstance(processor.feature_extractor ,				snake_case_  )

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	Optional[Any]    						=  SpeechTaProcessor(tokenizer=self.get_tokenizer() ,				feature_extractor=self.get_feature_extractor()  )
        processor.save_pretrained(self.tmpdirname  )

        A_  :	str    						=  self.get_tokenizer(bos_token='(BOS)' ,				eos_token='(EOS)'  )
        A_  :	Optional[Any]    						=  self.get_feature_extractor(do_normalize=snake_case_ ,				padding_value=1.0  )

        A_  :	Optional[int]    						=  SpeechTaProcessor.from_pretrained(
            self.tmpdirname ,				bos_token='(BOS)' ,				eos_token='(EOS)' ,				do_normalize=snake_case_ ,				padding_value=1.0  )

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

        self.assertEqual(processor.feature_extractor.to_json_string() ,				feature_extractor_add_kwargs.to_json_string()  )
        self.assertIsInstance(processor.feature_extractor ,				snake_case_  )

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	str    						=  self.get_feature_extractor()
        A_  :	Optional[int]    						=  self.get_tokenizer()

        A_  :	Optional[Any]    						=  SpeechTaProcessor(tokenizer=snake_case_ ,				feature_extractor=snake_case_  )

        A_  :	Dict    						=  floats_list((3, 1_0_0_0)  )

        A_  :	List[Any]    						=  feature_extractor(audio=snake_case_ ,				return_tensors='np'  )
        A_  :	Union[str, Any]    						=  processor(audio=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  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	Union[str, Any]    						=  self.get_feature_extractor()
        A_  :	Optional[Any]    						=  self.get_tokenizer()

        A_  :	Any    						=  SpeechTaProcessor(tokenizer=snake_case_ ,				feature_extractor=snake_case_  )

        A_  :	Optional[int]    						=  floats_list((3, 1_0_0_0)  )

        A_  :	List[str]    						=  feature_extractor(audio_target=snake_case_ ,				return_tensors='np'  )
        A_  :	Optional[Any]    						=  processor(audio_target=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  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	Tuple    						=  self.get_feature_extractor()
        A_  :	List[str]    						=  self.get_tokenizer()

        A_  :	Optional[Any]    						=  SpeechTaProcessor(tokenizer=snake_case_ ,				feature_extractor=snake_case_  )

        A_  :	Optional[int]    						=  'This is a test string'

        A_  :	int    						=  processor(text=snake_case_  )
        A_  :	Optional[Any]    						=  tokenizer(snake_case_  )

        for key in encoded_tok.keys():
          self.assertListEqual(encoded_tok[key] ,				encoded_processor[key]  )

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	Dict    						=  self.get_feature_extractor()
        A_  :	Union[str, Any]    						=  self.get_tokenizer()

        A_  :	List[str]    						=  SpeechTaProcessor(tokenizer=snake_case_ ,				feature_extractor=snake_case_  )

        A_  :	Tuple    						=  'This is a test string'

        A_  :	Optional[int]    						=  processor(text_target=snake_case_  )
        A_  :	Union[str, Any]    						=  tokenizer(snake_case_  )

        for key in encoded_tok.keys():
          self.assertListEqual(encoded_tok[key] ,				encoded_processor[key]  )

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	List[Any]    						=  self.get_feature_extractor()
        A_  :	Optional[Any]    						=  self.get_tokenizer()

        A_  :	Optional[Any]    						=  SpeechTaProcessor(tokenizer=snake_case_ ,				feature_extractor=snake_case_  )

        A_  :	Dict    						=  [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

        A_  :	Union[str, Any]    						=  processor.batch_decode(snake_case_  )
        A_  :	Optional[int]    						=  tokenizer.batch_decode(snake_case_  )

        self.assertListEqual(snake_case_ ,				snake_case_  )

      def  lowerCamelCase_      (					self  ):

        """simple docstring"""

        A_  :	Dict    						=  self.get_feature_extractor()
        A_  :	Optional[Any]    						=  self.get_tokenizer()

        A_  :	List[Any]    						=  SpeechTaProcessor(tokenizer=snake_case_ ,				feature_extractor=snake_case_  )

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






"""simple docstring"""




# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
    is_vision_available,
)


lowerCamelCase_					:						Any      = {
    'configuration_tvlt': ['TVLT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TvltConfig'],
    'feature_extraction_tvlt': ['TvltFeatureExtractor'],
    'processing_tvlt': ['TvltProcessor'],
}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowerCamelCase_					:						Union[str, Any]      = [
       'TVLT_PRETRAINED_MODEL_ARCHIVE_LIST',
       'TvltModel',
       'TvltForPreTraining',
       'TvltForAudioVisualClassification',
       'TvltPreTrainedModel',
   ]

try:
   if not is_vision_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   lowerCamelCase_					:						List[Any]      = ['TvltImageProcessor']


if TYPE_CHECKING:
   from .configuration_tvlt import TVLT_PRETRAINED_CONFIG_ARCHIVE_MAP, TvltConfig
   from .processing_tvlt import TvltProcessor
   from .feature_extraction_tvlt import TvltFeatureExtractor

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tvlt import (
          TVLT_PRETRAINED_MODEL_ARCHIVE_LIST,
          TvltForAudioVisualClassification,
          TvltForPreTraining,
          TvltModel,
          TvltPreTrainedModel,
      )

   try:
      if not is_vision_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .image_processing_tvlt import TvltImageProcessor


else:
   import sys

   lowerCamelCase_					:						Optional[int]      = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)