





"""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 PreTrainedTokenizer
from ...utils import logging


__A											=						logging.get_logger(__name__)

__A											=						"""▁"""

__A											=						{
    """vocab_file""": """vocab.json""",
    """spm_file""": """sentencepiece.bpe.model""",
}

__A											=						{
    """vocab_file""": {
        """facebook/s2t-small-librispeech-asr""": (
            """https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/vocab.json"""
        ),
    },
    """spm_file""": {
        """facebook/s2t-small-librispeech-asr""": (
            """https://huggingface.co/facebook/s2t-small-librispeech-asr/resolve/main/sentencepiece.bpe.model"""
        )
    },
}

__A											=						{
    """facebook/s2t-small-librispeech-asr""": 1024,
}

__A											=						["""pt""", """fr""", """ru""", """nl""", """ro""", """it""", """es""", """de"""]

__A											=						{"""mustc""": MUSTC_LANGS}






class    _lowerCAmelCase  (	a			):
       """simple docstring"""




       __magic_name__	:Tuple						    =   VOCAB_FILES_NAMES
       __magic_name__	:Union[str, Any]						    =   PRETRAINED_VOCAB_FILES_MAP
       __magic_name__	:Tuple						    =   MAX_MODEL_INPUT_SIZES
       __magic_name__	:Optional[Any]						    =   ["""input_ids""", """attention_mask"""]

       __magic_name__	:List[int]						    =   []






       def __init__(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase       ,					__UpperCAmelCase="<s>"       ,					__UpperCAmelCase="</s>"       ,					__UpperCAmelCase="<pad>"       ,					__UpperCAmelCase="<unk>"       ,					__UpperCAmelCase=False       ,					__UpperCAmelCase=False       ,					__UpperCAmelCase=None       ,					__UpperCAmelCase=None       ,					__UpperCAmelCase = None       ,					**__UpperCAmelCase       ,					):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[int]											=				{} if sp_model_kwargs is None else sp_model_kwargs

         super().__init__(
             bos_token=__UpperCAmelCase       ,					eos_token=__UpperCAmelCase       ,					unk_token=__UpperCAmelCase       ,					pad_token=__UpperCAmelCase       ,					do_upper_case=__UpperCAmelCase       ,					do_lower_case=__UpperCAmelCase       ,					tgt_lang=__UpperCAmelCase       ,					lang_codes=__UpperCAmelCase       ,					sp_model_kwargs=self.sp_model_kwargs       ,					**__UpperCAmelCase       ,					)
         lowerCAmelCase__     :Optional[int]											=				do_upper_case
         lowerCAmelCase__     :Optional[Any]											=				do_lower_case

         lowerCAmelCase__     :Any											=				load_json(__UpperCAmelCase )
         lowerCAmelCase__     :List[Any]											=				{v: k for k, v in self.encoder.items()}
         lowerCAmelCase__     :Optional[Any]											=				spm_file
         lowerCAmelCase__     :Optional[Any]											=				load_spm(__UpperCAmelCase       ,					self.sp_model_kwargs )

         if lang_codes is not None:
           lowerCAmelCase__     :Optional[Any]											=				lang_codes
           lowerCAmelCase__     :int											=				LANGUAGES[lang_codes]
           lowerCAmelCase__     :str											=				[F"<lang:{lang}>" for lang in self.langs]
           lowerCAmelCase__     :List[str]											=				{lang: self.sp_model.PieceToId(F"<lang:{lang}>" ) for lang in self.langs}

           lowerCAmelCase__     :Any											=				self.lang_tokens
           lowerCAmelCase__     :str											=				tgt_lang if tgt_lang is not None else self.langs[0]

           self.set_tgt_lang_special_tokens(self._tgt_lang )
         else:
           lowerCAmelCase__     :List[str]											=				{}






       @property
       def 	snake_case		(       self ):



         '''simple docstring'''

         return len(self.encoder )






       @property
       def 	snake_case		(       self ):



         '''simple docstring'''

         return self._tgt_lang






       @tgt_lang.setter
       def 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :str											=				new_tgt_lang
         self.set_tgt_lang_special_tokens(__UpperCAmelCase )






       def 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :int											=				self.lang_code_to_id[tgt_lang]
         lowerCAmelCase__     :Any											=				[lang_code_id]






       def 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

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






       def 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         return self.encoder.get(__UpperCAmelCase       ,					self.encoder[self.unk_token] )






       def 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         return self.decoder.get(__UpperCAmelCase       ,					self.unk_token )






       def 	snake_case		(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :List[Any]											=				[]
         lowerCAmelCase__     :List[str]											=				''
         for token in tokens:
           # make sure that special tokens are not decoded using sentencepiece model
           if token in self.all_special_tokens:
             lowerCAmelCase__     :Union[str, Any]											=				self.sp_model.decode(__UpperCAmelCase )
             out_string += (decoded.upper() if self.do_upper_case else decoded) + token + " "
             lowerCAmelCase__     :Any											=				[]
           else:
             current_sub_tokens.append(__UpperCAmelCase )
         lowerCAmelCase__     :Optional[Any]											=				self.sp_model.decode(__UpperCAmelCase )
         out_string += decoded.upper() if self.do_upper_case else decoded
         return out_string.strip()






       def 	snake_case		(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase=None ):



         '''simple docstring'''

         if token_ids_a is None:
           return self.prefix_tokens + token_ids_a + [self.eos_token_id]
         # 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.eos_token_id]






       def 	snake_case		(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase = None       ,					__UpperCAmelCase = False ):



         '''simple docstring'''

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

         lowerCAmelCase__     :Any											=				[1] * len(self.prefix_tokens )
         lowerCAmelCase__     :Tuple											=				[1]
         if token_ids_a is None:
           return prefix_ones + ([0] * len(__UpperCAmelCase )) + suffix_ones
         return prefix_ones + ([0] * len(__UpperCAmelCase )) + ([0] * len(__UpperCAmelCase )) + suffix_ones






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :List[str]											=				self.encoder.copy()
         vocab.update(self.added_tokens_encoder )
         return vocab






       def __getstate__(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Union[str, Any]											=				self.__dict__.copy()
         lowerCAmelCase__     :Tuple											=				None
         return state






       def __setstate__(       self       ,					__UpperCAmelCase ):



         '''simple docstring'''

         lowerCAmelCase__     :Tuple											=				d

         # for backward compatibility
         if not hasattr(self       ,					'sp_model_kwargs' ):
           lowerCAmelCase__     :List[str]											=				{}

         lowerCAmelCase__     :Union[str, Any]											=				load_spm(self.spm_file       ,					self.sp_model_kwargs )






       def 	snake_case		(       self       ,					__UpperCAmelCase       ,					__UpperCAmelCase = None ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[Any]											=				Path(__UpperCAmelCase )
         assert save_dir.is_dir(), F"{save_directory} should be a directory"
         lowerCAmelCase__     :List[str]											=				save_dir / (
             (filename_prefix + '-' if filename_prefix else '') + self.vocab_files_names['vocab_file']
         )
         lowerCAmelCase__     :Optional[Any]											=				save_dir / (
             (filename_prefix + '-' if filename_prefix else '') + self.vocab_files_names['spm_file']
         )

         save_json(self.encoder       ,					__UpperCAmelCase )

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

         return (str(__UpperCAmelCase ), str(__UpperCAmelCase ))






def 					__A  (_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE     )   ->sentencepiece.SentencePieceProcessor:





  """simple docstring"""


  lowerCAmelCase__     :List[Any]											=				sentencepiece.SentencePieceProcessor(**_SCREAMING_SNAKE_CASE     )
  spm.Load(str(_SCREAMING_SNAKE_CASE     )     )
  return spm
def 					__A  (_SCREAMING_SNAKE_CASE     )   ->Union[Dict, List]:





  """simple docstring"""


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




def 					__A  (_SCREAMING_SNAKE_CASE	,  _SCREAMING_SNAKE_CASE     )   ->None:





  """simple docstring"""


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





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_lowercase    	=		{
    "configuration_upernet": ["UperNetConfig"],
}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _lowercase    	=		[
       "UperNetForSemanticSegmentation",
       "UperNetPreTrainedModel",
   ]


if TYPE_CHECKING:
   from .configuration_upernet import UperNetConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_upernet import UperNetForSemanticSegmentation, UperNetPreTrainedModel


else:
   import sys

   _lowercase    	=		_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
