





"""simple docstring"""



import json
import os
from pathlib import Path
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple, Union

import sentencepiece

from ...tokenization_utils import BatchEncoding, PreTrainedTokenizer
from ...utils import logging


a_			=      logging.get_logger(__name__)

a_			=      """▁"""

a_			=      {
    """vocab_file""": """vocab.json""",
    """spm_file""": """sentencepiece.bpe.model""",
    """tokenizer_config_file""": """tokenizer_config.json""",
}

a_			=      {
    """vocab_file""": {
        """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/vocab.json""",
        """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/vocab.json""",
    },
    """spm_file""": {
        """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/sentencepiece.bpe.model""",
        """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/sentencepiece.bpe.model""",
    },
    """tokenizer_config_file""": {
        """facebook/m2m100_418M""": """https://huggingface.co/facebook/m2m100_418M/resolve/main/tokenizer_config.json""",
        """facebook/m2m100_1.2B""": """https://huggingface.co/facebook/m2m100_1.2B/resolve/main/tokenizer_config.json""",
    },
}

a_			=      {
    """facebook/m2m100_418M""": 1024,
}

# fmt: off
a_			=      {
    """m2m100""": ["""af""", """am""", """ar""", """ast""", """az""", """ba""", """be""", """bg""", """bn""", """br""", """bs""", """ca""", """ceb""", """cs""", """cy""", """da""", """de""", """el""", """en""", """es""", """et""", """fa""", """ff""", """fi""", """fr""", """fy""", """ga""", """gd""", """gl""", """gu""", """ha""", """he""", """hi""", """hr""", """ht""", """hu""", """hy""", """id""", """ig""", """ilo""", """is""", """it""", """ja""", """jv""", """ka""", """kk""", """km""", """kn""", """ko""", """lb""", """lg""", """ln""", """lo""", """lt""", """lv""", """mg""", """mk""", """ml""", """mn""", """mr""", """ms""", """my""", """ne""", """nl""", """no""", """ns""", """oc""", """or""", """pa""", """pl""", """ps""", """pt""", """ro""", """ru""", """sd""", """si""", """sk""", """sl""", """so""", """sq""", """sr""", """ss""", """su""", """sv""", """sw""", """ta""", """th""", """tl""", """tn""", """tr""", """uk""", """ur""", """uz""", """vi""", """wo""", """xh""", """yi""", """yo""", """zh""", """zu"""],
    """wmt21""": ["""en""", """ha""", """is""", """ja""", """cs""", """ru""", """zh""", """de"""]
}






class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 _lowerCamelCase				=					VOCAB_FILES_NAMES
 _lowerCamelCase				=					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 _lowerCamelCase				=					PRETRAINED_VOCAB_FILES_MAP
 _lowerCamelCase				=					["""input_ids""", """attention_mask"""]

 _lowerCamelCase				=					[]
 _lowerCamelCase				=					[]

 def __init__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase=None ,						__lowerCamelCase=None ,						__lowerCamelCase="<s>" ,						__lowerCamelCase="</s>" ,						__lowerCamelCase="</s>" ,						__lowerCamelCase="<pad>" ,						__lowerCamelCase="<unk>" ,						__lowerCamelCase="m2m100" ,						__lowerCamelCase = None ,						__lowerCamelCase=8 ,						**__lowerCamelCase ,						):



     '''simple docstring'''




     __A   :						str									=					{} if sp_model_kwargs is None else sp_model_kwargs

     __A   :						Any									=					language_codes
     __A   :						Tuple									=					FAIRSEQ_LANGUAGE_CODES[language_codes]
     __A   :						Optional[int]									=					{lang_code: F"""__{lang_code}__""" for lang_code in fairseq_language_code}

     __A   :						Optional[Any]									=					kwargs.get('''additional_special_tokens''' ,						[]  )
     kwargs["additional_special_tokens"] += [
         self.get_lang_token(__lowerCamelCase  )
         for lang_code in fairseq_language_code
         if self.get_lang_token(__lowerCamelCase  ) not in kwargs["additional_special_tokens"]
     ]

     super().__init__(
         src_lang=__lowerCamelCase ,						tgt_lang=__lowerCamelCase ,						bos_token=__lowerCamelCase ,						eos_token=__lowerCamelCase ,						sep_token=__lowerCamelCase ,						unk_token=__lowerCamelCase ,						pad_token=__lowerCamelCase ,						language_codes=__lowerCamelCase ,						sp_model_kwargs=self.sp_model_kwargs ,						num_madeup_words=__lowerCamelCase ,						**__lowerCamelCase ,						)

     __A   :						Optional[Any]									=					vocab_file
     __A   :						Union[str, Any]									=					load_json(__lowerCamelCase  )
     __A   :						str									=					{v: k for k, v in self.encoder.items()}
     __A   :						Optional[Any]									=					spm_file
     __A   :						Dict									=					load_spm(__lowerCamelCase ,						self.sp_model_kwargs  )

     __A   :						Optional[int]									=					len(self.encoder  )

     __A   :						List[str]									=					{
         self.get_lang_token(__lowerCamelCase  ): self.encoder_size + i for i, lang_code in enumerate(__lowerCamelCase  )
     }
     __A   :						Optional[int]									=					{lang_code: self.encoder_size + i for i, lang_code in enumerate(__lowerCamelCase  )}
     __A   :						Optional[Any]									=					{v: k for k, v in self.lang_token_to_id.items()}

     __A   :						List[Any]									=					src_lang if src_lang is not None else '''en'''
     __A   :						int									=					tgt_lang
     __A   :						Any									=					self.get_lang_id(self._src_lang  )
     self.set_src_lang_special_tokens(self._src_lang  )

     __A   :						List[str]									=					num_madeup_words

 @property
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     return len(self.encoder  ) + len(self.lang_token_to_id  )

 @property
 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     return self._src_lang

 @src_lang.setter
 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						List[Any]									=					new_src_lang
     self.set_src_lang_special_tokens(self._src_lang  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     return self.sp_model.encode(__lowerCamelCase ,						out_type=__lowerCamelCase  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     if token in self.lang_token_to_id:
         return self.lang_token_to_id[token]
     return self.encoder.get(__lowerCamelCase ,						self.encoder[self.unk_token]  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     if index in self.id_to_lang_token:
         return self.id_to_lang_token[index]
     return self.decoder.get(__lowerCamelCase ,						self.unk_token  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Any									=					[]
     __A   :						Any									=					''''''
     for token in tokens:
         # make sure that special tokens are not decoded using sentencepiece model
         if token in self.all_special_tokens:
             out_string += self.sp_model.decode(__lowerCamelCase  ) + token
             __A   :						Tuple									=					[]
         else:
             current_sub_tokens.append(__lowerCamelCase  )
     out_string += self.sp_model.decode(__lowerCamelCase  )
     return out_string.strip()

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase = None ,						__lowerCamelCase = False  ):



     '''simple docstring'''




     if already_has_special_tokens:
         return super().get_special_tokens_mask(
             token_ids_a=__lowerCamelCase ,						token_ids_a=__lowerCamelCase ,						already_has_special_tokens=__lowerCamelCase  )

     __A   :						Optional[int]									=					[1] * len(self.prefix_tokens  )
     __A   :						Dict									=					[1] * len(self.suffix_tokens  )
     if token_ids_a is None:
         return prefix_ones + ([0] * len(__lowerCamelCase  )) + suffix_ones
     return prefix_ones + ([0] * len(__lowerCamelCase  )) + ([0] * len(__lowerCamelCase  )) + suffix_ones

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase = None  ):



     '''simple docstring'''




     if token_ids_a is None:
         return self.prefix_tokens + token_ids_a + self.suffix_tokens
     # We don't expect to process pairs, but leave the pair logic for API consistency
     return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					{self.convert_ids_to_tokens(__lowerCamelCase  ): i for i in range(self.vocab_size  )}
     vocab.update(self.added_tokens_encoder  )
     return vocab

 def __getstate__(   self  ):



     '''simple docstring'''




     __A   :						str									=					self.__dict__.copy()
     __A   :						Dict									=					None
     return state

 def __setstate__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						List[str]									=					d

     # for backward compatibility
     if not hasattr(self ,						'''sp_model_kwargs'''  ):
         __A   :						str									=					{}

     __A   :						Optional[Any]									=					load_spm(self.spm_file ,						self.sp_model_kwargs  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase = None  ):



     '''simple docstring'''




     __A   :						int									=					Path(__lowerCamelCase  )
     if not save_dir.is_dir():
         raise OSError(F"""{save_directory} should be a directory"""  )
     __A   :						int									=					save_dir / (
         (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''vocab_file''']
     )
     __A   :						Union[str, Any]									=					save_dir / (
         (filename_prefix + '''-''' if filename_prefix else '''''') + self.vocab_files_names['''spm_file''']
     )

     save_json(self.encoder ,						__lowerCamelCase  )

     if os.path.abspath(self.spm_file  ) != os.path.abspath(__lowerCamelCase  ) and os.path.isfile(self.spm_file  ):
         copyfile(self.spm_file ,						__lowerCamelCase  )
     elif not os.path.isfile(self.spm_file  ):
         with open(__lowerCamelCase ,						'''wb'''  ) as fi:
             __A   :						str									=					self.sp_model.serialized_model_proto()
             fi.write(__lowerCamelCase  )

     return (str(__lowerCamelCase  ), str(__lowerCamelCase  ))

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase = "en" ,						__lowerCamelCase = None ,						__lowerCamelCase = "ro" ,						**__lowerCamelCase ,						):



     '''simple docstring'''




     __A   :						List[str]									=					src_lang
     __A   :						List[Any]									=					tgt_lang
     self.set_src_lang_special_tokens(self.src_lang  )
     return super().prepare_seqaseq_batch(__lowerCamelCase ,						__lowerCamelCase ,						**__lowerCamelCase  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase ,						__lowerCamelCase ,						**__lowerCamelCase  ):



     '''simple docstring'''




     if src_lang is None or tgt_lang is None:
         raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model'''  )
     __A   :						List[str]									=					src_lang
     __A   :						Optional[Any]									=					self(__lowerCamelCase ,						add_special_tokens=__lowerCamelCase ,						**__lowerCamelCase  )
     __A   :						int									=					self.get_lang_id(__lowerCamelCase  )
     __A   :						str									=					tgt_lang_id
     return inputs

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     self.set_src_lang_special_tokens(self.src_lang  )

 def  UpperCamelCase__(   self  ):



     '''simple docstring'''




     self.set_tgt_lang_special_tokens(self.tgt_lang  )

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						List[Any]									=					self.get_lang_token(__lowerCamelCase  )
     __A   :						Union[str, Any]									=					self.lang_token_to_id[lang_token]
     __A   :						int									=					[self.cur_lang_id]
     __A   :						List[str]									=					[self.eos_token_id]

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						int									=					self.get_lang_token(__lowerCamelCase  )
     __A   :						List[str]									=					self.lang_token_to_id[lang_token]
     __A   :						Optional[int]									=					[self.cur_lang_id]
     __A   :						Optional[int]									=					[self.eos_token_id]

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     return self.lang_code_to_token[lang]







 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						str									=					self.get_lang_token(__lowerCamelCase  )
     return self.lang_token_to_id[lang_token]







def 		__lowercase					(       snake_case_  :  str    ,snake_case_  :  Dict[str, Any]						)       ->sentencepiece.SentencePieceProcessor:



    '''simple docstring'''







    __A   :						Optional[Any]									=					sentencepiece.SentencePieceProcessor(**snake_case_						)
    spm.Load(str(snake_case_						)						)
    return spm







def 		__lowercase					(       snake_case_  :  str						)       ->Union[Dict, List]:



    '''simple docstring'''







    with open(snake_case_    ,'''r'''						) as f:
        return json.load(snake_case_						)







def 		__lowercase					(       snake_case_  :  Dict    ,snake_case_  :  str						)       ->None:



    '''simple docstring'''







    with open(snake_case_    ,'''w'''						) as f:
        json.dump(snake_case_    ,snake_case_    ,indent=2						)











"""simple docstring"""



from collections import UserDict
from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
       from PIL import Image

       from ..image_utils import load_image

if is_torch_available():
       from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING

if is_tf_available():
       from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
       from ..tf_utils import stable_softmax

a_			=      logging.get_logger(__name__)






@add_end_docstrings(SCREAMING_SNAKE_CASE__					)
class 		__snake_case (		SCREAMING_SNAKE_CASE__					):





 """simple docstring"""


 def __init__(   self ,						**__lowerCamelCase  ):



     '''simple docstring'''




     super().__init__(**__lowerCamelCase  )

     requires_backends(self ,						'''vision'''  )
     self.check_model_type(
         TF_MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING
         if self.framework == '''tf'''
         else MODEL_FOR_ZERO_SHOT_IMAGE_CLASSIFICATION_MAPPING  )

 def __call__(   self ,						__lowerCamelCase ,						**__lowerCamelCase  ):



     '''simple docstring'''




     return super().__call__(__lowerCamelCase ,						**__lowerCamelCase  )

 def  UpperCamelCase__(   self ,						**__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Union[str, Any]									=					{}
     if "candidate_labels" in kwargs:
         __A   :						Tuple									=					kwargs['''candidate_labels''']
     if "hypothesis_template" in kwargs:
         __A   :						List[str]									=					kwargs['''hypothesis_template''']

     return preprocess_params, {}, {}

 def  UpperCamelCase__(   self ,						__lowerCamelCase ,						__lowerCamelCase=None ,						__lowerCamelCase="This is a photo of {}."  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					load_image(__lowerCamelCase  )
     __A   :						Optional[int]									=					self.image_processor(images=[image] ,						return_tensors=self.framework  )
     __A   :						int									=					candidate_labels
     __A   :						int									=					[hypothesis_template.format(__lowerCamelCase  ) for x in candidate_labels]
     __A   :						Dict									=					self.tokenizer(__lowerCamelCase ,						return_tensors=self.framework ,						padding=__lowerCamelCase  )
     __A   :						int									=					[text_inputs]
     return inputs

 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					model_inputs.pop('''candidate_labels'''  )
     __A   :						str									=					model_inputs.pop('''text_inputs'''  )
     if isinstance(text_inputs[0] ,						__lowerCamelCase  ):
         __A   :						Union[str, Any]									=					text_inputs[0]
     else:
         # Batching case.
         __A   :						str									=					text_inputs[0][0]

     __A   :						List[str]									=					self.model(**__lowerCamelCase ,						**__lowerCamelCase  )

     __A   :						Dict									=					{
         '''candidate_labels''': candidate_labels,
         '''logits''': outputs.logits_per_image,
     }
     return model_outputs







 def  UpperCamelCase__(   self ,						__lowerCamelCase  ):



     '''simple docstring'''




     __A   :						Optional[int]									=					model_outputs.pop('''candidate_labels'''  )
     __A   :						int									=					model_outputs['''logits'''][0]
     if self.framework == "pt":
         __A   :						Union[str, Any]									=					logits.softmax(dim=-1  ).squeeze(-1  )
         __A   :						Dict									=					probs.tolist()
         if not isinstance(__lowerCamelCase ,						__lowerCamelCase  ):
             __A   :						List[Any]									=					[scores]
     elif self.framework == "tf":
         __A   :						List[Any]									=					stable_softmax(__lowerCamelCase ,						axis=-1  )
         __A   :						str									=					probs.numpy().tolist()
     else:
         raise ValueError(F"""Unsupported framework: {self.framework}"""  )

     __A   :						str									=					[
         {'''score''': score, '''label''': candidate_label}
         for score, candidate_label in sorted(zip(__lowerCamelCase ,						__lowerCamelCase  ) ,						key=lambda __lowerCamelCase  : -x[0]  )
     ]
     return result




