


import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin






class   _lowerCamelCase (					UpperCamelCase			):




       """simple docstring"""
       snake_case      =    "MCTCTFeatureExtractor"
       snake_case      =    "AutoTokenizer"



       def __init__(      self				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)->Tuple:




         '''simple docstring'''





         super().__init__(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)
         A_      :				Optional[Any]		     =					self.feature_extractor
         A_      :				int		     =					False



       def __call__(      self				,       *_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)->List[str]:




         '''simple docstring'''





         if self._in_target_context_manager:
           return self.current_processor(*_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)

         if "raw_speech" in kwargs:
           warnings.warn('''Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'''		)
           A_      :				Union[str, Any]		     =					kwargs.pop('''raw_speech'''		)
         else:
           A_      :				int		     =					kwargs.pop('''audio'''				,       _SCREAMING_SNAKE_CASE		)
         A_      :				str		     =					kwargs.pop('''sampling_rate'''				,       _SCREAMING_SNAKE_CASE		)
         A_      :				Union[str, Any]		     =					kwargs.pop('''text'''				,       _SCREAMING_SNAKE_CASE		)
         if len(_SCREAMING_SNAKE_CASE		) > 0:
           A_      :				Optional[int]		     =					args[0]
           A_      :				List[str]		     =					args[1:]

         if audio is None and text is None:
           raise ValueError('''You need to specify either an `audio` or `text` input to process.'''		)

         if audio is not None:
           A_      :				List[str]		     =					self.feature_extractor(_SCREAMING_SNAKE_CASE				,       *_SCREAMING_SNAKE_CASE				,       sampling_rate=_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)
         if text is not None:
           A_      :				Union[str, Any]		     =					self.tokenizer(_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)

         if text is None:
           return inputs
         elif audio is None:
           return encodings
         else:
           A_      :				str		     =					encodings['''input_ids''']
           return inputs



       def        _snake_case       (      self				,       *_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)->Tuple:




         '''simple docstring'''





         return self.tokenizer.batch_decode(*_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)



       def        _snake_case       (      self				,       *_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)->Optional[Any]:




         '''simple docstring'''





         if self._in_target_context_manager:
           return self.current_processor.pad(*_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)

         A_      :				List[Any]		     =					kwargs.pop('''input_features'''				,       _SCREAMING_SNAKE_CASE		)
         A_      :				Tuple		     =					kwargs.pop('''labels'''				,       _SCREAMING_SNAKE_CASE		)
         if len(_SCREAMING_SNAKE_CASE		) > 0:
           A_      :				List[str]		     =					args[0]
           A_      :				str		     =					args[1:]

         if input_features is not None:
           A_      :				Dict		     =					self.feature_extractor.pad(_SCREAMING_SNAKE_CASE				,       *_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)
         if labels is not None:
           A_      :				Dict		     =					self.tokenizer.pad(_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)

         if labels is None:
           return input_features
         elif input_features is None:
           return labels
         else:
           A_      :				Any		     =					labels['''input_ids''']
           return input_features



       def        _snake_case       (      self				,       *_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)->str:




         '''simple docstring'''





         return self.tokenizer.decode(*_SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)







       @contextmanager
       def        _snake_case       (      self		)->Any:




         '''simple docstring'''





         warnings.warn(
             '''`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '''
             '''labels by using the argument `text` of the regular `__call__` method (either in the same call as '''
             '''your audio inputs, or in a separate call.'''		)
         A_      :				str		     =					True
         A_      :				Union[str, Any]		     =					self.tokenizer
         yield
         A_      :				Optional[Any]		     =					self.feature_extractor
         A_      :				Tuple		     =					False








import copy
import tempfile
import unittest

from huggingface_hub import HfFolder, delete_repo
from parameterized import parameterized
from requests.exceptions import HTTPError

from transformers import AutoConfig, GenerationConfig
from transformers.testing_utils import TOKEN, USER, is_staging_test






class   _lowerCamelCase (					unittest.TestCase			):




       """simple docstring"""



       @parameterized.expand([(None,), ('''foo.json''',)]		)
       def        _snake_case       (      self				,       _SCREAMING_SNAKE_CASE		)->Tuple:




         '''simple docstring'''





         A_      :				Dict		     =					GenerationConfig(
             do_sample=_SCREAMING_SNAKE_CASE				,       temperature=0.7				,       length_penalty=1.0				,       bad_words_ids=[[1, 2, 3], [4, 5]]				,       )
         with tempfile.TemporaryDirectory() as tmp_dir:
           config.save_pretrained(_SCREAMING_SNAKE_CASE				,       config_name=_SCREAMING_SNAKE_CASE		)
           A_      :				Tuple		     =					GenerationConfig.from_pretrained(_SCREAMING_SNAKE_CASE				,       config_name=_SCREAMING_SNAKE_CASE		)

         # Checks parameters that were specified
         self.assertEqual(loaded_config.do_sample				,       _SCREAMING_SNAKE_CASE		)
         self.assertEqual(loaded_config.temperature				,       0.7		)
         self.assertEqual(loaded_config.length_penalty				,       1.0		)
         self.assertEqual(loaded_config.bad_words_ids				,       [[1, 2, 3], [4, 5]]		)

         # Checks parameters that were not specified (defaults)
         self.assertEqual(loaded_config.top_k				,       50		)
         self.assertEqual(loaded_config.max_length				,       20		)
         self.assertEqual(loaded_config.max_time				,       _SCREAMING_SNAKE_CASE		)



       def        _snake_case       (      self		)->Dict:




         '''simple docstring'''





         A_      :				str		     =					AutoConfig.from_pretrained('''gpt2'''		)
         A_      :				int		     =					GenerationConfig.from_model_config(_SCREAMING_SNAKE_CASE		)
         A_      :				Dict		     =					GenerationConfig()

         # The generation config has loaded a few non-default parameters from the model config
         self.assertNotEqual(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)

         # One of those parameters is eos_token_id -- check if it matches
         self.assertNotEqual(generation_config_from_model.eos_token_id				,       default_generation_config.eos_token_id		)
         self.assertEqual(generation_config_from_model.eos_token_id				,       model_config.eos_token_id		)



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




         '''simple docstring'''





         A_      :				List[Any]		     =					GenerationConfig()
         A_      :				Optional[Any]		     =					{
             '''max_new_tokens''': 1024,
             '''foo''': '''bar''',
         }
         A_      :				Dict		     =					copy.deepcopy(_SCREAMING_SNAKE_CASE		)
         A_      :				Optional[int]		     =					generation_config.update(**_SCREAMING_SNAKE_CASE		)

         # update_kwargs was not modified (no side effects)
         self.assertEqual(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)

         # update_kwargs was used to update the config on valid attributes
         self.assertEqual(generation_config.max_new_tokens				,       1024		)

         # `.update()` returns a dictionary of unused kwargs
         self.assertEqual(_SCREAMING_SNAKE_CASE				,       {'''foo''': '''bar'''}		)



       def        _snake_case       (      self		)->str:




         '''simple docstring'''





         A_      :				List[str]		     =					GenerationConfig()
         A_      :				int		     =					'''bar'''

         with tempfile.TemporaryDirectory('''test-generation-config'''		) as tmp_dir:
           generation_config.save_pretrained(_SCREAMING_SNAKE_CASE		)

           A_      :				Optional[Any]		     =					GenerationConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
         # update_kwargs was used to update the config on valid attributes
         self.assertEqual(new_config.foo				,       '''bar'''		)

         A_      :				Optional[int]		     =					GenerationConfig.from_model_config(_SCREAMING_SNAKE_CASE		)
         assert not hasattr(_SCREAMING_SNAKE_CASE				,       '''foo'''		)  # no new kwargs should be initialized if from config







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




         '''simple docstring'''





         A_      :				str		     =					GenerationConfig()
         self.assertEqual(default_config.temperature				,       1.0		)
         self.assertEqual(default_config.do_sample				,       _SCREAMING_SNAKE_CASE		)
         self.assertEqual(default_config.num_beams				,       1		)

         A_      :				str		     =					GenerationConfig(
             do_sample=_SCREAMING_SNAKE_CASE				,       temperature=0.7				,       length_penalty=1.0				,       bad_words_ids=[[1, 2, 3], [4, 5]]				,       )
         self.assertEqual(config.temperature				,       0.7		)
         self.assertEqual(config.do_sample				,       _SCREAMING_SNAKE_CASE		)
         self.assertEqual(config.num_beams				,       1		)

         with tempfile.TemporaryDirectory() as tmp_dir:
           config.save_pretrained(_SCREAMING_SNAKE_CASE		)
           A_      :				Dict		     =					GenerationConfig.from_pretrained(_SCREAMING_SNAKE_CASE				,       temperature=1.0		)

         self.assertEqual(loaded_config.temperature				,       1.0		)
         self.assertEqual(loaded_config.do_sample				,       _SCREAMING_SNAKE_CASE		)
         self.assertEqual(loaded_config.num_beams				,       1		)  # default value






@is_staging_test
class   _lowerCamelCase (					unittest.TestCase			):




       """simple docstring"""



       @classmethod
       def        _snake_case       (      cls		)->Optional[int]:




         '''simple docstring'''





         A_      :				str		     =					TOKEN
         HfFolder.save_token(_SCREAMING_SNAKE_CASE		)



       @classmethod
       def        _snake_case       (      cls		)->List[Any]:




         '''simple docstring'''





         try:
           delete_repo(token=cls._token				,       repo_id='''test-generation-config'''		)
         except HTTPError:
           pass

         try:
           delete_repo(token=cls._token				,       repo_id='''valid_org/test-generation-config-org'''		)
         except HTTPError:
           pass



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




         '''simple docstring'''





         A_      :				List[Any]		     =					GenerationConfig(
             do_sample=_SCREAMING_SNAKE_CASE				,       temperature=0.7				,       length_penalty=1.0				,       )
         config.push_to_hub('''test-generation-config'''				,       use_auth_token=self._token		)

         A_      :				Optional[int]		     =					GenerationConfig.from_pretrained(F'''{USER}/test-generation-config'''		)
         for k, v in config.to_dict().items():
           if k != "transformers_version":
             self.assertEqual(_SCREAMING_SNAKE_CASE				,       getattr(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)		)

        # Reset repo
         delete_repo(token=self._token				,       repo_id='''test-generation-config'''		)

         # Push to hub via save_pretrained
         with tempfile.TemporaryDirectory() as tmp_dir:
           config.save_pretrained(
               _SCREAMING_SNAKE_CASE				,       repo_id='''test-generation-config'''				,       push_to_hub=_SCREAMING_SNAKE_CASE				,       use_auth_token=self._token		)

         A_      :				Optional[Any]		     =					GenerationConfig.from_pretrained(F'''{USER}/test-generation-config'''		)
         for k, v in config.to_dict().items():
           if k != "transformers_version":
             self.assertEqual(_SCREAMING_SNAKE_CASE				,       getattr(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)		)







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




         '''simple docstring'''





         A_      :				Tuple		     =					GenerationConfig(
             do_sample=_SCREAMING_SNAKE_CASE				,       temperature=0.7				,       length_penalty=1.0				,       )
         config.push_to_hub('''valid_org/test-generation-config-org'''				,       use_auth_token=self._token		)

         A_      :				Union[str, Any]		     =					GenerationConfig.from_pretrained('''valid_org/test-generation-config-org'''		)
         for k, v in config.to_dict().items():
           if k != "transformers_version":
             self.assertEqual(_SCREAMING_SNAKE_CASE				,       getattr(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)		)

        # Reset repo
         delete_repo(token=self._token				,       repo_id='''valid_org/test-generation-config-org'''		)

         # Push to hub via save_pretrained
         with tempfile.TemporaryDirectory() as tmp_dir:
           config.save_pretrained(
               _SCREAMING_SNAKE_CASE				,       repo_id='''valid_org/test-generation-config-org'''				,       push_to_hub=_SCREAMING_SNAKE_CASE				,       use_auth_token=self._token		)

         A_      :				Dict		     =					GenerationConfig.from_pretrained('''valid_org/test-generation-config-org'''		)
         for k, v in config.to_dict().items():
           if k != "transformers_version":
             self.assertEqual(_SCREAMING_SNAKE_CASE				,       getattr(_SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE		)		)




