
from dataclasses import dataclass, field
from typing import Optional

from transformers import AutoConfig, AutoImageProcessor, AutoTokenizer, FlaxVisionEncoderDecoderModel, HfArgumentParser





@dataclass
class     __UpperCamelCase :






     """simple docstring"""







     lowerCAmelCase_							   =			field(
         metadata={'''help''': '''The output directory where the model will be written.'''} ,						)
     lowerCAmelCase_							   =			field(
         metadata={
             '''help''': (
                 '''The encoder model checkpoint for weights initialization.'''
                 '''Don\'t set if you want to train an encoder model from scratch.'''
             )
         } ,						)
     lowerCAmelCase_							   =			field(
         metadata={
             '''help''': (
                 '''The decoder model checkpoint for weights initialization.'''
                 '''Don\'t set if you want to train a decoder model from scratch.'''
             )
         } ,						)
     lowerCAmelCase_							   =			field(
         default=lowerCAmelCase__ ,						metadata={'''help''': '''Pretrained encoder config name or path if not the same as encoder_model_name'''}       )
     lowerCAmelCase_							   =			field(
         default=lowerCAmelCase__ ,						metadata={'''help''': '''Pretrained decoder config name or path if not the same as decoder_model_name'''}       )






def 							a__					(							):





  """simple docstring"""
  __SCREAMING_SNAKE_CASE      :	List[str]					=		HfArgumentParser((ModelArguments,)							)
  ((__SCREAMING_SNAKE_CASE),		)      :	List[str]					=		parser.parse_args_into_dataclasses()

  # Load pretrained model and tokenizer

  # Use explicit specified encoder config
  if model_args.encoder_config_name:
    __SCREAMING_SNAKE_CASE      :	List[str]					=		AutoConfig.from_pretrained(model_args.encoder_config_name							)
  # Use pretrained encoder model's config
  else:
    __SCREAMING_SNAKE_CASE      :	Tuple					=		AutoConfig.from_pretrained(model_args.encoder_model_name_or_path							)

  # Use explicit specified decoder config
  if model_args.decoder_config_name:
    __SCREAMING_SNAKE_CASE      :	List[Any]					=		AutoConfig.from_pretrained(model_args.decoder_config_name							)
  # Use pretrained decoder model's config
  else:
    __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		AutoConfig.from_pretrained(model_args.decoder_model_name_or_path							)

  # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed
  __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		True
  __SCREAMING_SNAKE_CASE      :	int					=		True

  __SCREAMING_SNAKE_CASE      :	Any					=		FlaxVisionEncoderDecoderModel.from_encoder_decoder_pretrained(
      encoder_pretrained_model_name_or_path=model_args.encoder_model_name_or_path       ,			decoder_pretrained_model_name_or_path=model_args.decoder_model_name_or_path       ,			encoder_config=snake_case       ,			decoder_config=snake_case       ,			)

  # GPT2 only has bos/eos tokens but not decoder_start/pad tokens
  __SCREAMING_SNAKE_CASE      :	str					=		decoder_config.decoder_start_token_id
  __SCREAMING_SNAKE_CASE      :	List[Any]					=		decoder_config.pad_token_id
  if decoder_start_token_id is None:
    __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		decoder_config.bos_token_id
  if pad_token_id is None:
    __SCREAMING_SNAKE_CASE      :	str					=		decoder_config.eos_token_id

  # This is necessary to make Flax's generate() work
  __SCREAMING_SNAKE_CASE      :	int					=		decoder_config.eos_token_id
  __SCREAMING_SNAKE_CASE      :	Tuple					=		decoder_start_token_id
  __SCREAMING_SNAKE_CASE      :	Optional[int]					=		pad_token_id

  __SCREAMING_SNAKE_CASE      :	Any					=		AutoImageProcessor.from_pretrained(model_args.encoder_model_name_or_path							)

  __SCREAMING_SNAKE_CASE      :	Any					=		AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path							)
  __SCREAMING_SNAKE_CASE      :	Optional[int]					=		tokenizer.convert_ids_to_tokens(model.config.pad_token_id							)

  model.save_pretrained(model_args.output_dir							)
  image_processor.save_pretrained(model_args.output_dir							)
  tokenizer.save_pretrained(model_args.output_dir							)


if __name__ == "__main__":
   main()


"""simple docstring"""






import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
from .feature_extraction_wavaveca import WavaVecaFeatureExtractor
from .tokenization_wavaveca import WavaVecaCTCTokenizer

class 					__a     ( __a       ):



							'''simple docstring'''







							_lowerCamelCase					:			List[Any]          =   """Wav2Vec2FeatureExtractor"""
							_lowerCamelCase					:			Any          =   """AutoTokenizer"""
							def __init__(     self  ,    _lowerCamelCase  ,    _lowerCamelCase       )			->							List[str]:




										'''simple docstring'''
										super().__init__(_lowerCamelCase  ,    _lowerCamelCase       )
										__lowercase       =       self.feature_extractor
										__lowercase       =       False
							@classmethod
							def   SCREAMING_SNAKE_CASE						(     cls  ,    _lowerCamelCase  ,    **_lowerCamelCase       )			->							Any:




										'''simple docstring'''
										try:
													return super().from_pretrained(_lowerCamelCase  ,    **_lowerCamelCase       )
										except OSError:
													warnings.warn(
													    f'''Loading a tokenizer inside {cls.__name__} from a config that does not'''
													    " include a `tokenizer_class` attribute is deprecated and will be "
													    "removed in v5. Please add `'tokenizer_class': 'Wav2Vec2CTCTokenizer'`"
													    " attribute to either your `config.json` or `tokenizer_config.json` "
													    "file to suppress this warning: "  ,    _lowerCamelCase  ,    )

													__lowercase       =       WavaVecaFeatureExtractor.from_pretrained(_lowerCamelCase  ,    **_lowerCamelCase       )
													__lowercase       =       WavaVecaCTCTokenizer.from_pretrained(_lowerCamelCase  ,    **_lowerCamelCase       )

													return cls(feature_extractor=_lowerCamelCase  ,    tokenizer=_lowerCamelCase       )
							def __call__(     self  ,    *_lowerCamelCase  ,    **_lowerCamelCase       )			->							List[Any]:




										'''simple docstring'''
										# For backward compatibility
										if self._in_target_context_manager:
													return self.current_processor(*_lowerCamelCase  ,    **_lowerCamelCase       )

										if "raw_speech" in kwargs:
													warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead."       )
													__lowercase       =       kwargs.pop("raw_speech"       )
										else:
													__lowercase       =       kwargs.pop("audio"  ,    _lowerCamelCase       )
										__lowercase       =       kwargs.pop("sampling_rate"  ,    _lowerCamelCase       )
										__lowercase       =       kwargs.pop("text"  ,    _lowerCamelCase       )
										if len(_lowerCamelCase       ) > 0:
													__lowercase       =       args[0]
													__lowercase       =       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:
													__lowercase       =       self.feature_extractor(_lowerCamelCase  ,    *_lowerCamelCase  ,    sampling_rate=_lowerCamelCase  ,    **_lowerCamelCase       )
										if text is not None:
													__lowercase       =       self.tokenizer(_lowerCamelCase  ,    **_lowerCamelCase       )

										if text is None:
													return inputs
										elif audio is None:
													return encodings
										else:
													__lowercase       =       encodings["input_ids"]
													return inputs
							def   SCREAMING_SNAKE_CASE						(     self  ,    *_lowerCamelCase  ,    **_lowerCamelCase       )			->							List[str]:




										'''simple docstring'''
										# For backward compatibility
										if self._in_target_context_manager:
													return self.current_processor.pad(*_lowerCamelCase  ,    **_lowerCamelCase       )

										__lowercase       =       kwargs.pop("input_features"  ,    _lowerCamelCase       )
										__lowercase       =       kwargs.pop("labels"  ,    _lowerCamelCase       )
										if len(_lowerCamelCase       ) > 0:
													__lowercase       =       args[0]
													__lowercase       =       args[1:]

										if input_features is not None:
													__lowercase       =       self.feature_extractor.pad(_lowerCamelCase  ,    *_lowerCamelCase  ,    **_lowerCamelCase       )
										if labels is not None:
													__lowercase       =       self.tokenizer.pad(_lowerCamelCase  ,    **_lowerCamelCase       )

										if labels is None:
													return input_features
										elif input_features is None:
													return labels
										else:
													__lowercase       =       labels["input_ids"]
													return input_features
							def   SCREAMING_SNAKE_CASE						(     self  ,    *_lowerCamelCase  ,    **_lowerCamelCase       )			->							Optional[int]:




										'''simple docstring'''
										return self.tokenizer.batch_decode(*_lowerCamelCase  ,    **_lowerCamelCase       )
							def   SCREAMING_SNAKE_CASE						(     self  ,    *_lowerCamelCase  ,    **_lowerCamelCase       )			->							Dict:




										'''simple docstring'''
										return self.tokenizer.decode(*_lowerCamelCase  ,    **_lowerCamelCase       )





							@contextmanager
							def   SCREAMING_SNAKE_CASE						(     self       )			->							str:




										'''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."       )
										__lowercase       =       True
										__lowercase       =       self.tokenizer
										yield
										__lowercase       =       self.feature_extractor
										__lowercase       =       False





