





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

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


_lowerCamelCase 					=      logging.get_logger(__name__)

_lowerCamelCase 					=      {'vocab_file': 'sentencepiece.model'}

_lowerCamelCase 					=      {
    'vocab_file': {
        'google/rembert': 'https://huggingface.co/google/rembert/resolve/main/sentencepiece.model',
    },
}

_lowerCamelCase 					=      {
    'google/rembert': 256,
}




class 	UpperCamelCase_							(  UpperCamelCase__     ):
							lowerCamelCase_							     =       VOCAB_FILES_NAMES
							lowerCamelCase_							     =       PRETRAINED_VOCAB_FILES_MAP
							lowerCamelCase_							     =       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES


							def __init__(				self     :Optional[int]				,					__A     :Any				,					__A     :int=False				,					__A     :Any=True				,					__A     :List[Any]=True				,					__A     :int="[CLS]"				,					__A     :Optional[Any]="[SEP]"				,					__A     :Union[str, Any]="[UNK]"				,					__A     :List[Any]="[SEP]"				,					__A     :Union[str, Any]="[PAD]"				,					__A     :List[str]="[CLS]"				,					__A     :Dict="[MASK]"				,					**__A     :List[str]				,					) -> Tuple:



									"""simple docstring"""



									super().__init__(
									    do_lower_case=__A				,					remove_space=__A				,					keep_accents=__A				,					bos_token=__A				,					eos_token=__A				,					unk_token=__A				,					sep_token=__A				,					pad_token=__A				,					cls_token=__A				,					mask_token=__A				,					**__A				,					)

									SCREAMING_SNAKE_CASE__								=   do_lower_case
									SCREAMING_SNAKE_CASE__								=   remove_space
									SCREAMING_SNAKE_CASE__								=   keep_accents
									SCREAMING_SNAKE_CASE__								=   vocab_file

									SCREAMING_SNAKE_CASE__								=   spm.SentencePieceProcessor()
									self.sp_model.Load(__A		)


							@property
							def   _snake_case	(				self     :Optional[int]		) -> Optional[int]:



									"""simple docstring"""



									return len(self.sp_model		)


							def   _snake_case	(				self     :Dict		) -> str:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   {self.convert_ids_to_tokens(__A		): i for i in range(self.vocab_size		)}
									vocab.update(self.added_tokens_encoder		)
									return vocab


							def __getstate__(				self     :Optional[Any]		) -> str:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.__dict__.copy()
									SCREAMING_SNAKE_CASE__								=   None
									return state


							def __setstate__(				self     :Dict				,					__A     :Any		) -> Optional[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   d
									SCREAMING_SNAKE_CASE__								=   spm.SentencePieceProcessor()
									self.sp_model.Load(self.vocab_file		)


							def   _snake_case	(				self     :Union[str, Any]				,					__A     :Dict				,					__A     :Optional[Any]=False		) -> Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.sp_model.EncodeAsPieces(__A		)
									return pieces


							def   _snake_case	(				self     :str				,					__A     :int		) -> int:



									"""simple docstring"""



									return self.sp_model.PieceToId(__A		)


							def   _snake_case	(				self     :Optional[Any]				,					__A     :List[str]		) -> int:



									"""simple docstring"""



									return self.sp_model.IdToPiece(__A		)


							def   _snake_case	(				self     :Optional[int]				,					__A     :Optional[Any]		) -> Tuple:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   self.sp_model.decode_pieces(__A		)
									return out_string


							def   _snake_case	(				self     :int				,					__A     :List[int]				,					__A     :Optional[List[int]] = None		) -> List[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   [self.sep_token_id]
									SCREAMING_SNAKE_CASE__								=   [self.cls_token_id]
									if token_ids_a is None:
											return cls + token_ids_a + sep
									return cls + token_ids_a + sep + token_ids_a + sep


							def   _snake_case	(				self     :List[Any]				,					__A     :List[int]				,					__A     :Optional[List[int]] = None				,					__A     :bool = False		) -> List[int]:



									"""simple docstring"""



									if already_has_special_tokens:
											if token_ids_a is not None:
													raise ValueError(
													    """You should not supply a second sequence if the provided sequence of """
													    """ids is already formatted with special tokens for the model."""		)
											return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a]

									if token_ids_a is not None:
											return [1] + ([0] * len(__A		)) + [1] + ([0] * len(__A		)) + [1]
									return [1] + ([0] * len(__A		)) + [1]


							def   _snake_case	(				self     :Optional[Any]				,					__A     :List[int]				,					__A     :Optional[List[int]] = None		) -> List[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   [self.sep_token_id]
									SCREAMING_SNAKE_CASE__								=   [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		) * [0] + len(token_ids_a + sep		) * [1]


							def   _snake_case	(				self     :Optional[Any]				,					__A     :str				,					__A     :Optional[str] = None		) -> Tuple[str]:



									"""simple docstring"""



									if not os.path.isdir(__A		):
											logger.error("""Vocabulary path ({}) should be a directory""".format(__A		)		)
											return
									SCREAMING_SNAKE_CASE__								=   os.path.join(
									    __A				,					(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]		)

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

									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 						__UpperCamelCase     (     UpperCamelCase						):
					lowercase_    :				Optional[Any]      =  """Salesforce/blip-image-captioning-base"""
					lowercase_    :				Optional[int]      =  (
					    """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_    :				int      =  """image_captioner"""
					lowercase_    :				int      =  AutoModelForVisionaSeq

					lowercase_    :				List[str]      =  ["""image"""]
					lowercase_    :				int      =  ["""text"""]
					def __init__(							self							:	Any     , *UpperCAmelCase							:	str     , **UpperCAmelCase							:	List[str]   )	->       Union[str, Any]:
											requires_backends(self     , ['vision']   )
											super().__init__(*UpperCAmelCase     , **UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	int     , UpperCAmelCase							:	"Image"   )	->       Union[str, Any]:
											return self.pre_processor(images=UpperCAmelCase     , return_tensors='pt'   )
					def        UpperCAmelCase__ (							self							:	int     , UpperCAmelCase							:	Optional[int]   )	->       Optional[int]:
											return self.model.generate(**UpperCAmelCase   )
					def        UpperCAmelCase__ (							self							:	Dict     , UpperCAmelCase							:	int   )	->       Dict:
											return self.pre_processor.batch_decode(UpperCAmelCase     , skip_special_tokens=UpperCAmelCase   )[0].strip()