


import argparse

import fairseq
import torch
from torch import nn

from transformers import (
    MBartaaTokenizer,
    MBartConfig,
    MBartForCausalLM,
    SpeechEncoderDecoderConfig,
    SpeechEncoderDecoderModel,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaModel,
    logging,
)


logging.set_verbosity_info()
UpperCAmelCase_      : Dict  					=       logging.get_logger(__name__)

UpperCAmelCase_      : int  					=       {
    'post_extract_proj': 'feature_projection.projection',
    'encoder.pos_conv.0': 'encoder.pos_conv_embed.conv',
    'self_attn.k_proj': 'encoder.layers.*.attention.k_proj',
    'self_attn.v_proj': 'encoder.layers.*.attention.v_proj',
    'self_attn.q_proj': 'encoder.layers.*.attention.q_proj',
    'self_attn.out_proj': 'encoder.layers.*.attention.out_proj',
    'self_attn_layer_norm': 'encoder.layers.*.layer_norm',
    'fc1': 'encoder.layers.*.feed_forward.intermediate_dense',
    'fc2': 'encoder.layers.*.feed_forward.output_dense',
    'final_layer_norm': 'encoder.layers.*.final_layer_norm',
    'encoder.layer_norm': 'encoder.layer_norm',
    'w2v_model.layer_norm': 'feature_projection.layer_norm',
    'quantizer.weight_proj': 'quantizer.weight_proj',
    'quantizer.vars': 'quantizer.codevectors',
    'project_q': 'project_q',
    'final_proj': 'project_hid',
    'w2v_encoder.proj': 'lm_head',
    'mask_emb': 'masked_spec_embed',
}
UpperCAmelCase_      : Union[str, Any]  					=       [
    'lm_head',
    'quantizer.weight_proj',
    'quantizer.codevectors',
    'project_q',
    'project_hid',
]







def        SCREAMING_SNAKE_CASE_						(			__A :			str  ,    __A :			Optional[Any]  ,    __A :			Optional[Any]  ,    __A :			int  ,    __A :			Dict				)	->		List[Any]:

       """simple docstring"""

       for attribute in key.split('.'				):
              a_					:		Optional[Any]							=  getattr(__A  ,    __A				)

       if weight_type is not None:
              a_					:		Optional[int]							=  getattr(__A  ,    __A				).shape
       else:
              a_					:		Any							=  hf_pointer.shape

       assert hf_shape == value.shape, (
           F"""Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be"""
           F""" {value.shape} for {full_name}"""
       )

       if weight_type == "weight":
              a_					:		str							=  value
       elif weight_type == "weight_g":
              a_					:		Tuple							=  value
       elif weight_type == "weight_v":
              a_					:		Optional[int]							=  value
       elif weight_type == "bias":
              a_					:		Union[str, Any]							=  value
       else:
              a_					:		Tuple							=  value

       logger.info(F"""{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}."""				)







def        SCREAMING_SNAKE_CASE_						(			__A :			List[str]  ,    __A :			List[Any]				)	->		Any:

       """simple docstring"""

       a_					:		Any							=  []
       a_					:		Dict							=  fairseq_model.state_dict()

       a_					:		Optional[int]							=  hf_model.feature_extractor
       a_					:		str							=  hf_model.adapter

       for name, value in fairseq_dict.items():
              a_					:		str							=  False
              if "conv_layers" in name:
                     load_conv_layer(
                         __A  ,    __A  ,    __A  ,    __A  ,    hf_model.config.feat_extract_norm == 'group'  ,    )
                     a_					:		Dict							=  True
              elif any(x in name for x in ['adaptor', 'w2v_encoder.proj.', 'w2v_proj_ln.']				):
                     load_adapter(__A  ,    __A  ,    __A  ,    __A				)
                     a_					:		Tuple							=  True
              else:
                     for key, mapped_key in MAPPING.items():
                            if key in name or key.split('w2v_model.'				)[-1] == name.split('.'				)[0]:
                                   a_					:		Union[str, Any]							=  True
                                   if "*" in mapped_key:
                                          a_					:		List[Any]							=  name.split(__A				)[0].split('.'				)[-2]
                                          a_					:		str							=  mapped_key.replace('*'  ,    __A				)
                                   if "weight_g" in name:
                                          a_					:		Dict							=  'weight_g'
                                   elif "weight_v" in name:
                                          a_					:		Optional[Any]							=  'weight_v'
                                   elif "bias" in name:
                                          a_					:		Optional[int]							=  'bias'
                                   elif "weight" in name:
                                          a_					:		Dict							=  'weight'
                                   else:
                                          a_					:		Any							=  None
                                   set_recursively(__A  ,    __A  ,    __A  ,    __A  ,    __A				)
                            continue
              if not is_used:
                     unused_weights.append(__A				)

       logger.warning(F"""Unused weights: {unused_weights}"""				)







def        SCREAMING_SNAKE_CASE_						(			__A :			List[Any]  ,    __A :			Dict  ,    __A :			int  ,    __A :			int  ,    __A :			List[str]				)	->		List[Any]:

       """simple docstring"""

       a_					:		int							=  full_name.split('conv_layers.'				)[-1]
       a_					:		Dict							=  name.split('.'				)
       a_					:		str							=  int(items[0]				)
       a_					:		Optional[Any]							=  int(items[1]				)

       if type_id == 0:
              if "bias" in name:
                     assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, (
                         F"""{full_name} has size {value.shape}, but"""
                         F""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found."""
                     )
                     a_					:		Optional[Any]							=  value
                     logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}."""				)
              elif "weight" in name:
                     assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, (
                         F"""{full_name} has size {value.shape}, but"""
                         F""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found."""
                     )
                     a_					:		Any							=  value
                     logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}."""				)
       elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
              if "bias" in name:
                     assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, (
                         F"""{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was"""
                         " found."
                     )
                     a_					:		str							=  value
                     logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."""				)
              elif "weight" in name:
                     assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, (
                         F"""{full_name} has size {value.shape}, but"""
                         F""" {feature_extractor[layer_id].layer_norm.weight.data.shape} was found."""
                     )
                     a_					:		str							=  value
                     logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."""				)
       else:
              unused_weights.append(__A				)







def        SCREAMING_SNAKE_CASE_						(			__A :			Any  ,    __A :			int  ,    __A :			Optional[int]  ,    __A :			Optional[int]				)	->		List[Any]:

       """simple docstring"""

       a_					:		List[Any]							=  full_name.split('adaptor.'				)[-1]
       a_					:		str							=  name.split('.'				)

       if items[1].isdigit():
              a_					:		Dict							=  int(items[1]				)
       else:
              a_					:		int							=  None

       if "adaptor" not in full_name:
              if "proj_ln" in full_name:
                     # has to be layer norm
                     if "bias" in name:
                            assert (
                                value.shape == adapter.proj_layer_norm.bias.data.shape
                            ), F"""{full_name} has size {value.shape}, but {adapter.proj_layer_norm.bias.data.shape} was found."""
                            a_					:		int							=  value
                            logger.info(F"""Adapter proj layer norm bias was initialized from {full_name}."""				)
                     if "weight" in name:
                            assert (
                                value.shape == adapter.proj_layer_norm.weight.data.shape
                            ), F"""{full_name} has size {value.shape}, but {adapter.proj_layer_norm.weight.data.shape} was found."""
                            a_					:		Union[str, Any]							=  value
              else:
                     # has to be projection layer
                     if "bias" in name:
                            assert (
                                value.shape == adapter.proj.bias.data.shape
                            ), F"""{full_name} has size {value.shape}, but {adapter.proj.bias.data.shape} was found."""
                            a_					:		List[str]							=  value
                            logger.info(F"""Adapter proj layer bias was initialized from {full_name}."""				)
                     if "weight" in name:
                            assert (
                                value.shape == adapter.proj.weight.data.shape
                            ), F"""{full_name} has size {value.shape}, but {adapter.proj.weight.data.shape} was found."""
                            a_					:		str							=  value
                            logger.info(F"""Adapter proj layer weight was initialized from {full_name}."""				)
       elif isinstance(__A  ,    __A				):
              if "bias" in name:
                     assert (
                         value.shape == adapter.layers[layer_id].conv.bias.data.shape
                     ), F"""{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.bias.data.shape} was found."""
                     a_					:		Optional[Any]							=  value
                     logger.info(F"""Adapter layer {layer_id} bias was initialized from {full_name}."""				)
              elif "weight" in name:
                     assert (
                         value.shape == adapter.layers[layer_id].conv.weight.data.shape
                     ), F"""{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.weight.data.shape} was found."""
                     a_					:		List[Any]							=  value
                     logger.info(F"""Adapter layer {layer_id} bias was initialized from {full_name}."""				)
       else:
              unused_weights.append(__A				)







def        SCREAMING_SNAKE_CASE_						(			__A :			List[Any]				)	->		List[Any]:

       """simple docstring"""

       a_							,		a_					:		str							=  emb.weight.shape
       a_					:		Optional[int]							=  nn.Linear(__A  ,    __A  ,    bias=__A				)
       a_					:		Optional[int]							=  emb.weight.data
       return lin_layer







@torch.no_grad()
def        SCREAMING_SNAKE_CASE_						(			__A :			Dict  ,    __A :			Optional[int]  ,    __A :			Dict  ,    __A :			Optional[Any]  ,    __A :			str  ,    __A :			List[Any]  ,    __A :			Tuple  ,    __A :			Dict  ,    __A :			Optional[int]  ,    __A :			str  ,    __A :			str  ,    )	->		int:

       """simple docstring"""

       a_					:		Any							=  WavaVecaConfig.from_pretrained(
           __A  ,    add_adapter=__A  ,    adapter_stride=__A  ,    adapter_kernel_size=__A  ,    use_auth_token=__A  ,    output_hidden_size=__A  ,    )
       a_					:		Union[str, Any]							=  MBartConfig.from_pretrained(__A				)

       # load model
       a_							,		a_							,		a_					:		Union[str, Any]							=  fairseq.checkpoint_utils.load_model_ensemble_and_task(
           [checkpoint_path]  ,    arg_overrides={
               'config_yaml': config_yaml_path,
               'data': '/'.join(dict_path.split('/'				)[:-1]				),
               'w2v_path': checkpoint_path,
               'load_pretrained_decoder_from': None,
           }  ,    )
       a_					:		int							=  model[0].eval()

       # load feature extractor
       a_					:		List[Any]							=  WavaVecaFeatureExtractor.from_pretrained(__A  ,    use_auth_token=__A				)

       # set weights for wav2vec2 encoder
       a_					:		int							=  WavaVecaModel(__A				)

       recursively_load_weights_wavaveca(model.encoder  ,    __A				)

       # load decoder weights
       a_					:		Tuple							=  MBartForCausalLM(__A				)
       a_							,		a_					:		Optional[int]							=  hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict()  ,    strict=__A				)
       logger.warning(F"""The following keys are missing when loading the decoder weights: {missing_keys}"""				)
       logger.warning(F"""The following keys are unexpected when loading the decoder weights: {unexpected_keys}"""				)

       a_					:		Union[str, Any]							=  SpeechEncoderDecoderModel(encoder=__A  ,    decoder=__A				)
       a_					:		List[str]							=  False

       a_					:		List[Any]							=  MBartaaTokenizer(__A				)
       tokenizer.save_pretrained(__A				)

       a_					:		Optional[Any]							=  hf_wavavec.config.to_dict()
       a_					:		Dict							=  tokenizer.pad_token_id
       a_					:		int							=  tokenizer.bos_token_id
       a_					:		Tuple							=  tokenizer.eos_token_id
       a_					:		str							=  'mbart50'
       a_					:		Union[str, Any]							=  'wav2vec2'

       a_					:		Union[str, Any]							=  tokenizer.eos_token_id
       a_					:		Optional[Any]							=  25_00_04
       a_					:		List[Any]							=  tokenizer.eos_token_id

       a_					:		Optional[int]							=  SpeechEncoderDecoderConfig.from_dict(__A				)

       hf_wavavec.save_pretrained(__A				)
       feature_extractor.save_pretrained(__A				)


if __name__ == "__main__":
   UpperCAmelCase_      : Any  					=       argparse.ArgumentParser()
   parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
   parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint')
   parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model')
   parser.add_argument('--config_yaml_path', default=None, type=str, help='Path to yaml file of fine-tuned model')
   parser.add_argument(
       '--encoder_config_path',
       default='facebook/wav2vec2-xls-r-1b',
       type=str,
       help='Path to hf encoder wav2vec2 checkpoint config',
   )
   parser.add_argument(
       '--decoder_config_path',
       default='facebook/mbart-large-50-one-to-many-mmt',
       type=str,
       help='Path to hf decoder checkpoint config',
   )
   parser.add_argument('--add_adapter', default=True, type=bool, help='whethere to add model adapter layers')
   parser.add_argument('--adapter_stride', default=2, type=int, help='stride of adapter layers')
   parser.add_argument('--adapter_kernel_size', default=3, type=int, help='kernel size of adapter layers')
   parser.add_argument('--encoder_output_dim', default=1024, type=int, help='encoder output dim')
   parser.add_argument('--start_token_id', default=25_0004, type=int, help='`decoder_start_token_id` of model config')

   UpperCAmelCase_      : str  					=       parser.parse_args()
   convert_wavaveca_checkpoint(
       args.checkpoint_path,
       args.pytorch_dump_folder_path,
       args.dict_path,
       args.config_yaml_path,
       encoder_config_path=args.encoder_config_path,
       decoder_config_path=args.decoder_config_path,
       add_adapter=args.add_adapter,
       adapter_kernel_size=args.adapter_kernel_size,
       adapter_stride=args.adapter_stride,
       decoder_start_token_id=args.start_token_id,
       encoder_output_dim=args.encoder_output_dim,
   )






# Copyright 2023 The HuggingFace Inc. 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 ..models.auto import AutoModelForSeqaSeqLM, AutoTokenizer
from .base import PipelineTool


UpperCAmelCase_      : Tuple  					=       {
    'Acehnese Arabic': 'ace_Arab',
    'Acehnese Latin': 'ace_Latn',
    'Mesopotamian Arabic': 'acm_Arab',
    'Ta\'izzi-Adeni Arabic': 'acq_Arab',
    'Tunisian Arabic': 'aeb_Arab',
    'Afrikaans': 'afr_Latn',
    'South Levantine Arabic': 'ajp_Arab',
    'Akan': 'aka_Latn',
    'Amharic': 'amh_Ethi',
    'North Levantine Arabic': 'apc_Arab',
    'Modern Standard Arabic': 'arb_Arab',
    'Modern Standard Arabic Romanized': 'arb_Latn',
    'Najdi Arabic': 'ars_Arab',
    'Moroccan Arabic': 'ary_Arab',
    'Egyptian Arabic': 'arz_Arab',
    'Assamese': 'asm_Beng',
    'Asturian': 'ast_Latn',
    'Awadhi': 'awa_Deva',
    'Central Aymara': 'ayr_Latn',
    'South Azerbaijani': 'azb_Arab',
    'North Azerbaijani': 'azj_Latn',
    'Bashkir': 'bak_Cyrl',
    'Bambara': 'bam_Latn',
    'Balinese': 'ban_Latn',
    'Belarusian': 'bel_Cyrl',
    'Bemba': 'bem_Latn',
    'Bengali': 'ben_Beng',
    'Bhojpuri': 'bho_Deva',
    'Banjar Arabic': 'bjn_Arab',
    'Banjar Latin': 'bjn_Latn',
    'Standard Tibetan': 'bod_Tibt',
    'Bosnian': 'bos_Latn',
    'Buginese': 'bug_Latn',
    'Bulgarian': 'bul_Cyrl',
    'Catalan': 'cat_Latn',
    'Cebuano': 'ceb_Latn',
    'Czech': 'ces_Latn',
    'Chokwe': 'cjk_Latn',
    'Central Kurdish': 'ckb_Arab',
    'Crimean Tatar': 'crh_Latn',
    'Welsh': 'cym_Latn',
    'Danish': 'dan_Latn',
    'German': 'deu_Latn',
    'Southwestern Dinka': 'dik_Latn',
    'Dyula': 'dyu_Latn',
    'Dzongkha': 'dzo_Tibt',
    'Greek': 'ell_Grek',
    'English': 'eng_Latn',
    'Esperanto': 'epo_Latn',
    'Estonian': 'est_Latn',
    'Basque': 'eus_Latn',
    'Ewe': 'ewe_Latn',
    'Faroese': 'fao_Latn',
    'Fijian': 'fij_Latn',
    'Finnish': 'fin_Latn',
    'Fon': 'fon_Latn',
    'French': 'fra_Latn',
    'Friulian': 'fur_Latn',
    'Nigerian Fulfulde': 'fuv_Latn',
    'Scottish Gaelic': 'gla_Latn',
    'Irish': 'gle_Latn',
    'Galician': 'glg_Latn',
    'Guarani': 'grn_Latn',
    'Gujarati': 'guj_Gujr',
    'Haitian Creole': 'hat_Latn',
    'Hausa': 'hau_Latn',
    'Hebrew': 'heb_Hebr',
    'Hindi': 'hin_Deva',
    'Chhattisgarhi': 'hne_Deva',
    'Croatian': 'hrv_Latn',
    'Hungarian': 'hun_Latn',
    'Armenian': 'hye_Armn',
    'Igbo': 'ibo_Latn',
    'Ilocano': 'ilo_Latn',
    'Indonesian': 'ind_Latn',
    'Icelandic': 'isl_Latn',
    'Italian': 'ita_Latn',
    'Javanese': 'jav_Latn',
    'Japanese': 'jpn_Jpan',
    'Kabyle': 'kab_Latn',
    'Jingpho': 'kac_Latn',
    'Kamba': 'kam_Latn',
    'Kannada': 'kan_Knda',
    'Kashmiri Arabic': 'kas_Arab',
    'Kashmiri Devanagari': 'kas_Deva',
    'Georgian': 'kat_Geor',
    'Central Kanuri Arabic': 'knc_Arab',
    'Central Kanuri Latin': 'knc_Latn',
    'Kazakh': 'kaz_Cyrl',
    'Kabiyè': 'kbp_Latn',
    'Kabuverdianu': 'kea_Latn',
    'Khmer': 'khm_Khmr',
    'Kikuyu': 'kik_Latn',
    'Kinyarwanda': 'kin_Latn',
    'Kyrgyz': 'kir_Cyrl',
    'Kimbundu': 'kmb_Latn',
    'Northern Kurdish': 'kmr_Latn',
    'Kikongo': 'kon_Latn',
    'Korean': 'kor_Hang',
    'Lao': 'lao_Laoo',
    'Ligurian': 'lij_Latn',
    'Limburgish': 'lim_Latn',
    'Lingala': 'lin_Latn',
    'Lithuanian': 'lit_Latn',
    'Lombard': 'lmo_Latn',
    'Latgalian': 'ltg_Latn',
    'Luxembourgish': 'ltz_Latn',
    'Luba-Kasai': 'lua_Latn',
    'Ganda': 'lug_Latn',
    'Luo': 'luo_Latn',
    'Mizo': 'lus_Latn',
    'Standard Latvian': 'lvs_Latn',
    'Magahi': 'mag_Deva',
    'Maithili': 'mai_Deva',
    'Malayalam': 'mal_Mlym',
    'Marathi': 'mar_Deva',
    'Minangkabau Arabic ': 'min_Arab',
    'Minangkabau Latin': 'min_Latn',
    'Macedonian': 'mkd_Cyrl',
    'Plateau Malagasy': 'plt_Latn',
    'Maltese': 'mlt_Latn',
    'Meitei Bengali': 'mni_Beng',
    'Halh Mongolian': 'khk_Cyrl',
    'Mossi': 'mos_Latn',
    'Maori': 'mri_Latn',
    'Burmese': 'mya_Mymr',
    'Dutch': 'nld_Latn',
    'Norwegian Nynorsk': 'nno_Latn',
    'Norwegian Bokmål': 'nob_Latn',
    'Nepali': 'npi_Deva',
    'Northern Sotho': 'nso_Latn',
    'Nuer': 'nus_Latn',
    'Nyanja': 'nya_Latn',
    'Occitan': 'oci_Latn',
    'West Central Oromo': 'gaz_Latn',
    'Odia': 'ory_Orya',
    'Pangasinan': 'pag_Latn',
    'Eastern Panjabi': 'pan_Guru',
    'Papiamento': 'pap_Latn',
    'Western Persian': 'pes_Arab',
    'Polish': 'pol_Latn',
    'Portuguese': 'por_Latn',
    'Dari': 'prs_Arab',
    'Southern Pashto': 'pbt_Arab',
    'Ayacucho Quechua': 'quy_Latn',
    'Romanian': 'ron_Latn',
    'Rundi': 'run_Latn',
    'Russian': 'rus_Cyrl',
    'Sango': 'sag_Latn',
    'Sanskrit': 'san_Deva',
    'Santali': 'sat_Olck',
    'Sicilian': 'scn_Latn',
    'Shan': 'shn_Mymr',
    'Sinhala': 'sin_Sinh',
    'Slovak': 'slk_Latn',
    'Slovenian': 'slv_Latn',
    'Samoan': 'smo_Latn',
    'Shona': 'sna_Latn',
    'Sindhi': 'snd_Arab',
    'Somali': 'som_Latn',
    'Southern Sotho': 'sot_Latn',
    'Spanish': 'spa_Latn',
    'Tosk Albanian': 'als_Latn',
    'Sardinian': 'srd_Latn',
    'Serbian': 'srp_Cyrl',
    'Swati': 'ssw_Latn',
    'Sundanese': 'sun_Latn',
    'Swedish': 'swe_Latn',
    'Swahili': 'swh_Latn',
    'Silesian': 'szl_Latn',
    'Tamil': 'tam_Taml',
    'Tatar': 'tat_Cyrl',
    'Telugu': 'tel_Telu',
    'Tajik': 'tgk_Cyrl',
    'Tagalog': 'tgl_Latn',
    'Thai': 'tha_Thai',
    'Tigrinya': 'tir_Ethi',
    'Tamasheq Latin': 'taq_Latn',
    'Tamasheq Tifinagh': 'taq_Tfng',
    'Tok Pisin': 'tpi_Latn',
    'Tswana': 'tsn_Latn',
    'Tsonga': 'tso_Latn',
    'Turkmen': 'tuk_Latn',
    'Tumbuka': 'tum_Latn',
    'Turkish': 'tur_Latn',
    'Twi': 'twi_Latn',
    'Central Atlas Tamazight': 'tzm_Tfng',
    'Uyghur': 'uig_Arab',
    'Ukrainian': 'ukr_Cyrl',
    'Umbundu': 'umb_Latn',
    'Urdu': 'urd_Arab',
    'Northern Uzbek': 'uzn_Latn',
    'Venetian': 'vec_Latn',
    'Vietnamese': 'vie_Latn',
    'Waray': 'war_Latn',
    'Wolof': 'wol_Latn',
    'Xhosa': 'xho_Latn',
    'Eastern Yiddish': 'ydd_Hebr',
    'Yoruba': 'yor_Latn',
    'Yue Chinese': 'yue_Hant',
    'Chinese Simplified': 'zho_Hans',
    'Chinese Traditional': 'zho_Hant',
    'Standard Malay': 'zsm_Latn',
    'Zulu': 'zul_Latn',
}
class  SCREAMING_SNAKE_CASE__  (				lowercase__			):
      snake_case__				:     str        =						'''facebook/nllb-200-distilled-600M'''
      snake_case__				:     Union[str, Any]        =						(
          '''This is a tool that translates text from a language to another. It takes three inputs: `text`, which should '''
          '''be the text to translate, `src_lang`, which should be the language of the text to translate and `tgt_lang`, '''
          '''which should be the language for the desired ouput language. Both `src_lang` and `tgt_lang` are written in '''
          '''plain English, such as \'Romanian\', or \'Albanian\'. It returns the text translated in `tgt_lang`.'''
      )
      snake_case__				:     Optional[Any]        =						'''translator'''
      snake_case__				:     Tuple        =						AutoTokenizer
      snake_case__				:     Union[str, Any]        =						AutoModelForSeqaSeqLM
      snake_case__				:     Dict        =						LANGUAGE_CODES

      snake_case__				:     str        =						['''text''', '''text''', '''text''']
      snake_case__				:     Tuple        =						['''text''']
      def        SCREAMING_SNAKE_CASE							(       self				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     ,							SCREAMING_SNAKE_CASE__				:       Dict     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]     )		->			Tuple:
             if src_lang not in self.lang_to_code:
                    raise ValueError(F"""{src_lang} is not a supported language."""     )
             if tgt_lang not in self.lang_to_code:
                    raise ValueError(F"""{tgt_lang} is not a supported language."""     )
             a_					:		str							=  self.lang_to_code[src_lang]
             a_					:		Any							=  self.lang_to_code[tgt_lang]
             return self.pre_processor._build_translation_inputs(
                 SCREAMING_SNAKE_CASE__     ,							return_tensors='pt'     ,							src_lang=SCREAMING_SNAKE_CASE__     ,							tgt_lang=SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       int     ,							SCREAMING_SNAKE_CASE__				:       Tuple     )		->			Any:
             return self.model.generate(**SCREAMING_SNAKE_CASE__     )

      def        SCREAMING_SNAKE_CASE							(       self				:       Tuple     ,							SCREAMING_SNAKE_CASE__				:       List[str]     )		->			Dict:
             return self.post_processor.decode(outputs[0].tolist()     ,							skip_special_tokens=SCREAMING_SNAKE_CASE__     )


