


'''simple docstring'''
from dataclasses import dataclass, field
from typing import Optional

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

@dataclass
class 						_lowerCamelCase					:




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


def 			_lowercase       (				):
 __A  :		Dict       =			HfArgumentParser((ModelArguments,)			)
 (__A)  :		Optional[Any]       =			parser.parse_args_into_dataclasses()

 # Load pretrained model and tokenizer

 # Use explicit specified encoder config
 if model_args.encoder_config_name:
  __A  :		Dict       =			AutoConfig.from_pretrained(model_args.encoder_config_name			)
 # Use pretrained encoder model's config
 else:
  __A  :		Optional[Any]       =			AutoConfig.from_pretrained(model_args.encoder_model_name_or_path			)

 # Use explicit specified decoder config
 if model_args.decoder_config_name:
  __A  :		Dict       =			AutoConfig.from_pretrained(model_args.decoder_config_name			)
 # Use pretrained decoder model's config
 else:
  __A  :		Tuple       =			AutoConfig.from_pretrained(model_args.decoder_model_name_or_path			)

 # necessary for `from_encoder_decoder_pretrained` when `decoder_config` is passed
 __A  :		Dict       =			True
 __A  :		int       =			True

 __A  :		str       =			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=UpperCamelCase__,				decoder_config=UpperCamelCase__,				)

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

 # This is necessary to make Flax's generate() work
 __A  :		Optional[int]       =			decoder_config.eos_token_id
 __A  :		str       =			decoder_start_token_id
 __A  :		Union[str, Any]       =			pad_token_id

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

 __A  :		List[str]       =			AutoTokenizer.from_pretrained(model_args.decoder_model_name_or_path			)
 __A  :		Union[str, Any]       =			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 unittest

from datasets import load_dataset

from transformers.pipelines import pipeline
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_torch, slow

@is_pipeline_test
@require_torch
class 						_lowerCamelCase					(    unittest.TestCase				):




 '''simple docstring'''


 @require_torch
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Optional[int]       =			pipeline(
      task='zero-shot-audio-classification'      ,      model='hf-internal-testing/tiny-clap-htsat-unfused'						)
  __A  :		str       =			load_dataset('ashraq/esc50'						)
  __A  :		Union[str, Any]       =			dataset['train']['audio'][-1]['array']
  __A  :		List[str]       =			audio_classifier(__lowercase      ,      candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']						)
  self.assertEqual(
      nested_simplify(__lowercase						)      ,      [{'score': 0.5_0_1, 'label': 'Sound of a dog'}, {'score': 0.4_9_9, 'label': 'Sound of vaccum cleaner'}]      ,      )


 @unittest.skip('No models are available in TF'						)
 def  snake_case__   (      self						):

  """simple docstring"""







  pass


 @slow
 @require_torch
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Dict       =			pipeline(
      task='zero-shot-audio-classification'      ,      model='laion/clap-htsat-unfused'      ,      )
  # This is an audio of a dog
  __A  :		Optional[Any]       =			load_dataset('ashraq/esc50'						)
  __A  :		List[str]       =			dataset['train']['audio'][-1]['array']
  __A  :		Union[str, Any]       =			audio_classifier(__lowercase      ,      candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']						)

  self.assertEqual(
      nested_simplify(__lowercase						)      ,      [
          {'score': 0.9_9_9, 'label': 'Sound of a dog'},
          {'score': 0.0_0_1, 'label': 'Sound of vaccum cleaner'},
      ]      ,      )

  __A  :		Any       =			audio_classifier([audio] * 5      ,      candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']						)
  self.assertEqual(
      nested_simplify(__lowercase						)      ,      [
          [
              {'score': 0.9_9_9, 'label': 'Sound of a dog'},
              {'score': 0.0_0_1, 'label': 'Sound of vaccum cleaner'},
          ],
      ]
      * 5      ,      )
  __A  :		str       =			audio_classifier(
      [audio] * 5      ,      candidate_labels=['Sound of a dog', 'Sound of vaccum cleaner']      ,      batch_size=5						)
  self.assertEqual(
      nested_simplify(__lowercase						)      ,      [
          [
              {'score': 0.9_9_9, 'label': 'Sound of a dog'},
              {'score': 0.0_0_1, 'label': 'Sound of vaccum cleaner'},
          ],
      ]
      * 5      ,      )







 @unittest.skip('No models are available in TF'						)
 def  snake_case__   (      self						):

  """simple docstring"""







  pass






