

"""simple docstring"""





import os
from shutil import copyfile
from typing import List, Optional, Tuple

from tokenizers import processors

from ...tokenization_utils import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
							from .tokenization_mbart import MBartTokenizer
else:
							__UpperCamelCase    : Any				 =						None


__UpperCamelCase    : Optional[Any]				 =						logging.get_logger(__name__)


__UpperCamelCase    : Optional[Any]				 =						{'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''}

__UpperCamelCase    : Dict				 =						{
    '''vocab_file''': {
        '''facebook/mbart-large-en-ro''': (
            '''https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/sentencepiece.bpe.model'''
        ),
        '''facebook/mbart-large-cc25''': (
            '''https://huggingface.co/facebook/mbart-large-cc25/resolve/main/sentencepiece.bpe.model'''
        ),
    },
    '''tokenizer_file''': {
        '''facebook/mbart-large-en-ro''': '''https://huggingface.co/facebook/mbart-large-en-ro/resolve/main/tokenizer.json''',
        '''facebook/mbart-large-cc25''': '''https://huggingface.co/facebook/mbart-large-cc25/resolve/main/tokenizer.json''',
    },
}

__UpperCamelCase    : List[Any]				 =						{
    '''facebook/mbart-large-en-ro''': 1_0_2_4,
    '''facebook/mbart-large-cc25''': 1_0_2_4,
}

# fmt: off
__UpperCamelCase    : Optional[int]				 =						['''ar_AR''', '''cs_CZ''', '''de_DE''', '''en_XX''', '''es_XX''', '''et_EE''', '''fi_FI''', '''fr_XX''', '''gu_IN''', '''hi_IN''', '''it_IT''', '''ja_XX''', '''kk_KZ''', '''ko_KR''', '''lt_LT''', '''lv_LV''', '''my_MM''', '''ne_NP''', '''nl_XX''', '''ro_RO''', '''ru_RU''', '''si_LK''', '''tr_TR''', '''vi_VN''', '''zh_CN''']







class 							SCREAMING_SNAKE_CASE   (						a_			):






					"""simple docstring"""



					lowercase__												=			VOCAB_FILES_NAMES
					lowercase__												=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					lowercase__												=			PRETRAINED_VOCAB_FILES_MAP
					lowercase__												=			["input_ids", "attention_mask"]
					lowercase__												=			MBartTokenizer

					lowercase__												=			[]
					lowercase__												=			[]







					def __init__(			self     :							Dict  ,lowercase_     :							Optional[int]=None  ,lowercase_     :							List[str]=None  ,lowercase_     :							List[Any]="<s>"  ,lowercase_     :							str="</s>"  ,lowercase_     :							int="</s>"  ,lowercase_     :							Tuple="<s>"  ,lowercase_     :							List[str]="<unk>"  ,lowercase_     :							Tuple="<pad>"  ,lowercase_     :							int="<mask>"  ,lowercase_     :							List[Any]=None  ,lowercase_     :							Any=None  ,lowercase_     :							List[str]=None  ,**lowercase_     :							Dict  ,):
						# Mask token behave like a normal word, i.e. include the space before it
						lowerCAmelCase__      :		Optional[Any]    							=      AddedToken(lowercase_  ,lstrip=lowercase_  ,rstrip=lowercase_						) if isinstance(lowercase_  ,lowercase_						) else mask_token

						super().__init__(
						    vocab_file=lowercase_  ,tokenizer_file=lowercase_  ,bos_token=lowercase_  ,eos_token=lowercase_  ,sep_token=lowercase_  ,cls_token=lowercase_  ,unk_token=lowercase_  ,pad_token=lowercase_  ,mask_token=lowercase_  ,src_lang=lowercase_  ,tgt_lang=lowercase_  ,additional_special_tokens=lowercase_  ,**lowercase_  ,)

						lowerCAmelCase__      :		Optional[int]    							=      vocab_file
						lowerCAmelCase__      :		Union[str, Any]    							=      False if not self.vocab_file else True

						lowerCAmelCase__      :		Optional[Any]    							=      FAIRSEQ_LANGUAGE_CODES.copy()

						if additional_special_tokens is not None:
							# Only add those special tokens if they are not already there.
							_additional_special_tokens.extend(
							    [t for t in additional_special_tokens if t not in _additional_special_tokens]						)

						self.add_special_tokens({'''additional_special_tokens''': _additional_special_tokens}						)
						lowerCAmelCase__      :		Union[str, Any]    							=      {
						    lang_code: self.convert_tokens_to_ids(lowercase_						) for lang_code in FAIRSEQ_LANGUAGE_CODES
						}

						lowerCAmelCase__      :		Union[str, Any]    							=      src_lang if src_lang is not None else '''en_XX'''
						lowerCAmelCase__      :		Optional[int]    							=      self.convert_tokens_to_ids(self._src_lang						)
						lowerCAmelCase__      :		Union[str, Any]    							=      tgt_lang
						self.set_src_lang_special_tokens(self._src_lang						)







					@property
					def 			__lowerCAmelCase					(			self     :							Optional[Any]						):
						return self._src_lang







					@src_lang.setter
					def 			__lowerCAmelCase					(			self     :							List[str]  ,lowercase_     :							str						):
						lowerCAmelCase__      :		List[Any]    							=      new_src_lang
						self.set_src_lang_special_tokens(self._src_lang						)







					def 			__lowerCAmelCase					(			self     :							Any  ,lowercase_     :							List[int]  ,lowercase_     :							Optional[List[int]] = None						):
						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 			__lowerCAmelCase					(			self     :							Optional[int]  ,lowercase_     :							List[int]  ,lowercase_     :							Optional[List[int]] = None						):
						lowerCAmelCase__      :		str    							=      [self.sep_token_id]
						lowerCAmelCase__      :		str    							=      [self.cls_token_id]

						if token_ids_a is None:
							return len(cls + token_ids_a + sep						) * [0]
						return len(cls + token_ids_a + sep + sep + token_ids_a + sep						) * [0]







					def 			__lowerCAmelCase					(			self     :							Any  ,lowercase_     :							Optional[Any]  ,lowercase_     :							str  ,lowercase_     :							Optional[str]  ,lowercase_     :							Optional[str]  ,**lowercase_     :							Tuple						):
						if src_lang is None or tgt_lang is None:
							raise ValueError('''Translation requires a `src_lang` and a `tgt_lang` for this model'''						)
						lowerCAmelCase__      :		Any    							=      src_lang
						lowerCAmelCase__      :		Dict    							=      self(lowercase_  ,add_special_tokens=lowercase_  ,return_tensors=lowercase_  ,**lowercase_						)
						lowerCAmelCase__      :		Dict    							=      self.convert_tokens_to_ids(lowercase_						)
						lowerCAmelCase__      :		int    							=      tgt_lang_id
						return inputs







					def 			__lowerCAmelCase					(			self     :							List[Any]  ,lowercase_     :							List[str]  ,lowercase_     :							str = "en_XX"  ,lowercase_     :							Optional[List[str]] = None  ,lowercase_     :							str = "ro_RO"  ,**lowercase_     :							Optional[Any]  ,):
						lowerCAmelCase__      :		Optional[int]    							=      src_lang
						lowerCAmelCase__      :		Dict    							=      tgt_lang
						return super().prepare_seqaseq_batch(lowercase_  ,lowercase_  ,**lowercase_						)







					def 			__lowerCAmelCase					(			self     :							Any						):
						return self.set_src_lang_special_tokens(self.src_lang						)







					def 			__lowerCAmelCase					(			self     :							Dict						):
						return self.set_tgt_lang_special_tokens(self.tgt_lang						)







					def 			__lowerCAmelCase					(			self     :							List[Any]  ,lowercase_     :							Optional[int]						):
						lowerCAmelCase__      :		Optional[Any]    							=      self.convert_tokens_to_ids(lowercase_						)
						lowerCAmelCase__      :		str    							=      []
						lowerCAmelCase__      :		Union[str, Any]    							=      [self.eos_token_id, self.cur_lang_code]

						lowerCAmelCase__      :		List[Any]    							=      self.convert_ids_to_tokens(self.prefix_tokens						)
						lowerCAmelCase__      :		Union[str, Any]    							=      self.convert_ids_to_tokens(self.suffix_tokens						)

						lowerCAmelCase__      :		Optional[int]    							=      processors.TemplateProcessing(
						    single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str  ,pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str  ,special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str  ,self.prefix_tokens + self.suffix_tokens						)						)  ,)







					def 			__lowerCAmelCase					(			self     :							Dict  ,lowercase_     :							str						):
						lowerCAmelCase__      :		Optional[Any]    							=      self.convert_tokens_to_ids(lowercase_						)
						lowerCAmelCase__      :		Optional[Any]    							=      []
						lowerCAmelCase__      :		List[Any]    							=      [self.eos_token_id, self.cur_lang_code]

						lowerCAmelCase__      :		List[Any]    							=      self.convert_ids_to_tokens(self.prefix_tokens						)
						lowerCAmelCase__      :		List[str]    							=      self.convert_ids_to_tokens(self.suffix_tokens						)

						lowerCAmelCase__      :		List[Any]    							=      processors.TemplateProcessing(
						    single=prefix_tokens_str + ['''$A'''] + suffix_tokens_str  ,pair=prefix_tokens_str + ['''$A''', '''$B'''] + suffix_tokens_str  ,special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str  ,self.prefix_tokens + self.suffix_tokens						)						)  ,)







					def 			__lowerCAmelCase					(			self     :							Optional[int]  ,lowercase_     :							str  ,lowercase_     :							Optional[str] = None						):
						if not self.can_save_slow_tokenizer:
							raise ValueError(
							    '''Your fast tokenizer does not have the necessary information to save the vocabulary for a slow '''
							    '''tokenizer.'''						)

						if not os.path.isdir(lowercase_						):
							logger.error(F'Vocabulary path ({save_directory}) should be a directory.'						)
							return
						lowerCAmelCase__      :		int    							=      os.path.join(
						    lowercase_  ,(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']						)

						if os.path.abspath(self.vocab_file						) != os.path.abspath(lowercase_						):
							copyfile(self.vocab_file  ,lowercase_						)

						return (out_vocab_file,)







"""simple docstring"""





# 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 typing import TYPE_CHECKING

from ..models.auto import AutoModelForVisionaSeq
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
							from PIL import Image







class 							SCREAMING_SNAKE_CASE   (						a_			):






					"""simple docstring"""



					lowercase__												=			"Salesforce/blip-image-captioning-base"
					lowercase__												=			(
					    "This is a tool that generates a description of an image. It takes an input named `image` which should be the "
					    "image to caption, and returns a text that contains the description in English."
					)
					lowercase__												=			"image_captioner"
					lowercase__												=			AutoModelForVisionaSeq

					lowercase__												=			["image"]
					lowercase__												=			["text"]







					def __init__(			self     :							int  ,*lowercase_     :							int  ,**lowercase_     :							List[str]						):
						requires_backends(self  ,['''vision''']						)
						super().__init__(*lowercase_  ,**lowercase_						)







					def 			__lowerCAmelCase					(			self     :							List[str]  ,lowercase_     :							"Image"						):
						return self.pre_processor(images=lowercase_  ,return_tensors='''pt'''						)







					def 			__lowerCAmelCase					(			self     :							List[Any]  ,lowercase_     :							Any						):
						return self.model.generate(**lowercase_						)







					def 			__lowerCAmelCase					(			self     :							Dict  ,lowercase_     :							Union[str, Any]						):
						return self.pre_processor.batch_decode(lowercase_  ,skip_special_tokens=lowercase_						)[0].strip()




