





'''simple docstring'''







import os
import unicodedata
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

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


snake_case_     :       Tuple									=  logging.get_logger(__name__)
snake_case_     :       Union[str, Any]									=  {'vocab_file': 'spiece.model'}

snake_case_     :       Union[str, Any]									=  {
    'vocab_file': {
        'albert-base-v1': 'https://huggingface.co/albert-base-v1/resolve/main/spiece.model',
        'albert-large-v1': 'https://huggingface.co/albert-large-v1/resolve/main/spiece.model',
        'albert-xlarge-v1': 'https://huggingface.co/albert-xlarge-v1/resolve/main/spiece.model',
        'albert-xxlarge-v1': 'https://huggingface.co/albert-xxlarge-v1/resolve/main/spiece.model',
        'albert-base-v2': 'https://huggingface.co/albert-base-v2/resolve/main/spiece.model',
        'albert-large-v2': 'https://huggingface.co/albert-large-v2/resolve/main/spiece.model',
        'albert-xlarge-v2': 'https://huggingface.co/albert-xlarge-v2/resolve/main/spiece.model',
        'albert-xxlarge-v2': 'https://huggingface.co/albert-xxlarge-v2/resolve/main/spiece.model',
    }
}

snake_case_     :       int									=  {
    'albert-base-v1': 5_12,
    'albert-large-v1': 5_12,
    'albert-xlarge-v1': 5_12,
    'albert-xxlarge-v1': 5_12,
    'albert-base-v2': 5_12,
    'albert-large-v2': 5_12,
    'albert-xlarge-v2': 5_12,
    'albert-xxlarge-v2': 5_12,
}

snake_case_     :       Optional[int]									=  '▁'


class 					__a      (lowerCamelCase     ):
							__a					:   Tuple									=       VOCAB_FILES_NAMES
							__a					:   str									=       PRETRAINED_VOCAB_FILES_MAP
							__a					:   Optional[int]									=       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES

							def __init__(							self							:     List[Any]       ,     __magic_name__							:     Any       ,     __magic_name__							:     str=True       ,     __magic_name__							:     Optional[int]=True       ,     __magic_name__							:     List[Any]=False       ,     __magic_name__							:     str="[CLS]"       ,     __magic_name__							:     int="[SEP]"       ,     __magic_name__							:     Dict="<unk>"       ,     __magic_name__							:     Union[str, Any]="[SEP]"       ,     __magic_name__							:     Dict="<pad>"       ,     __magic_name__							:     Optional[int]="[CLS]"       ,     __magic_name__							:     Any="[MASK]"       ,     __magic_name__							:     Optional[Dict[str, Any]] = None       ,     **__magic_name__							:     Optional[int]       ,     )					->       str:







													"""simple docstring"""
													# Mask token behave like a normal word, i.e. include the space before it and
													# is included in the raw text, there should be a match in a non-normalized sentence.
													UpperCAmelCase_			:      Dict     						=  (
													    AddedToken(__A       ,     lstrip=__A       ,     rstrip=__A       ,     normalized=__A   )
													    if isinstance(__A       ,     __A   )
													    else mask_token
													)

													UpperCAmelCase_			:      List[str]     						=  {} if sp_model_kwargs is None else sp_model_kwargs

													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       ,     sp_model_kwargs=self.sp_model_kwargs       ,     **__A       ,     )

													UpperCAmelCase_			:      str     						=  do_lower_case
													UpperCAmelCase_			:      int     						=  remove_space
													UpperCAmelCase_			:      List[Any]     						=  keep_accents
													UpperCAmelCase_			:      int     						=  vocab_file

													UpperCAmelCase_			:      Any     						=  spm.SentencePieceProcessor(**self.sp_model_kwargs   )
													self.sp_model.Load(__A   )

							@property
							def     UpperCAmelCase__	(							self							:     Optional[int]   )					->       List[str]:







													"""simple docstring"""
													return len(self.sp_model   )

							def     UpperCAmelCase__	(							self							:     int   )					->       Union[str, Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[Any]     						=  {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							:     Dict   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      Optional[int]     						=  self.__dict__.copy()
													UpperCAmelCase_			:      int     						=  None
													return state

							def __setstate__(							self							:     List[Any]       ,     __magic_name__							:     Dict   )					->       Any:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  d

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

													UpperCAmelCase_			:      Tuple     						=  spm.SentencePieceProcessor(**self.sp_model_kwargs   )
													self.sp_model.Load(self.vocab_file   )

							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     Tuple   )					->       Optional[int]:







													"""simple docstring"""
													if self.remove_space:
																			UpperCAmelCase_			:      List[Any]     						=  " ".join(inputs.strip().split()   )
													else:
																			UpperCAmelCase_			:      List[str]     						=  inputs
													UpperCAmelCase_			:      str     						=  outputs.replace('''``'''       ,     '''\"'''   ).replace('''\'\''''       ,     '''\"'''   )

													if not self.keep_accents:
																			UpperCAmelCase_			:      Tuple     						=  unicodedata.normalize('''NFKD'''       ,     __A   )
																			UpperCAmelCase_			:      Optional[Any]     						=  "".join([c for c in outputs if not unicodedata.combining(__A   )]   )
													if self.do_lower_case:
																			UpperCAmelCase_			:      Optional[int]     						=  outputs.lower()

													return outputs

							def     UpperCAmelCase__	(							self							:     str       ,     __magic_name__							:     str   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  self.preprocess_text(__A   )
													UpperCAmelCase_			:      Optional[Any]     						=  self.sp_model.encode(__A       ,     out_type=__A   )
													UpperCAmelCase_			:      Union[str, Any]     						=  []
													for piece in pieces:
																			if len(__A   ) > 1 and piece[-1] == str(''','''   ) and piece[-2].isdigit():
																									UpperCAmelCase_			:      List[str]     						=  self.sp_model.EncodeAsPieces(piece[:-1].replace(__A       ,     ''''''   )   )
																									if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
																															if len(cur_pieces[0]   ) == 1:
																																					UpperCAmelCase_			:      Union[str, Any]     						=  cur_pieces[1:]
																															else:
																																					UpperCAmelCase_			:      Dict     						=  cur_pieces[0][1:]
																									cur_pieces.append(piece[-1]   )
																									new_pieces.extend(__A   )
																			else:
																									new_pieces.append(__A   )

													return new_pieces

							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     Union[str, Any]   )					->       Union[str, Any]:







													"""simple docstring"""
													return self.sp_model.PieceToId(__A   )

							def     UpperCAmelCase__	(							self							:     Union[str, Any]       ,     __magic_name__							:     str   )					->       Union[str, Any]:







													"""simple docstring"""
													return self.sp_model.IdToPiece(__A   )

							def     UpperCAmelCase__	(							self							:     List[str]       ,     __magic_name__							:     Tuple   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      Union[str, Any]     						=  []
													UpperCAmelCase_			:      List[Any]     						=  ""
													UpperCAmelCase_			:      List[str]     						=  False
													for token in tokens:
																			# make sure that special tokens are not decoded using sentencepiece model
																			if token in self.all_special_tokens:
																									if not prev_is_special:
																															out_string += " "
																									out_string += self.sp_model.decode(__A   ) + token
																									UpperCAmelCase_			:      Any     						=  True
																									UpperCAmelCase_			:      List[Any]     						=  []
																			else:
																									current_sub_tokens.append(__A   )
																									UpperCAmelCase_			:      Optional[Any]     						=  False
													out_string += self.sp_model.decode(__A   )
													return out_string.strip()

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     List[int]       ,     __magic_name__							:     Optional[List[int]] = None   )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      Any     						=  [self.sep_token_id]
													UpperCAmelCase_			:      List[Any]     						=  [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     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     List[int]       ,     __magic_name__							:     Optional[List[int]] = None       ,     __magic_name__							:     bool = False   )					->       List[str]:







													"""simple docstring"""

													if already_has_special_tokens:
																			return super().get_special_tokens_mask(
																			    token_ids_a=__A       ,     token_ids_a=__A       ,     already_has_special_tokens=__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     UpperCAmelCase__	(							self							:     str       ,     __magic_name__							:     List[int]       ,     __magic_name__							:     Optional[List[int]] = None   )					->       Tuple:







													"""simple docstring"""
													UpperCAmelCase_			:      Any     						=  [self.sep_token_id]
													UpperCAmelCase_			:      List[Any]     						=  [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     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     str       ,     __magic_name__							:     Optional[str] = None   )					->       List[Any]:







													"""simple docstring"""
													if not os.path.isdir(__A   ):
																			logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""   )
																			return
													UpperCAmelCase_			:      str     						=  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   ) and os.path.isfile(self.vocab_file   ):
																			copyfile(self.vocab_file       ,     __A   )
													elif not os.path.isfile(self.vocab_file   ):
																			with open(__A       ,     '''wb'''   ) as fi:
																									UpperCAmelCase_			:      List[str]     						=  self.sp_model.serialized_model_proto()
																									fi.write(__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

import torch

from ..models.auto import AutoModelForVisualQuestionAnswering, AutoProcessor
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
					from PIL import Image


class 					__a      (lowerCamelCase     ):
							__a					:   int									=       "dandelin/vilt-b32-finetuned-vqa"
							__a					:   Any									=       (
							    "This is a tool that answers a question about an image. It takes an input named `image` which should be the "
							    "image containing the information, as well as a `question` which should be the question in English. It "
							    "returns a text that is the answer to the question."
							)
							__a					:   Any									=       "image_qa"
							__a					:   str									=       AutoProcessor
							__a					:   Any									=       AutoModelForVisualQuestionAnswering

							__a					:   List[Any]									=       ["image", "text"]
							__a					:   int									=       ["text"]

							def __init__(							self							:     Tuple       ,     *__magic_name__							:     Any       ,     **__magic_name__							:     Any   )					->       Tuple:







													"""simple docstring"""
													requires_backends(self       ,     ['''vision''']   )
													super().__init__(*__magic_name__       ,     **__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Union[str, Any]       ,     __magic_name__							:     "Image"       ,     __magic_name__							:     str   )					->       Tuple:







													"""simple docstring"""
													return self.pre_processor(__magic_name__       ,     __magic_name__       ,     return_tensors='''pt'''   )

							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     List[str]   )					->       Optional[Any]:







													"""simple docstring"""
													with torch.no_grad():
																			return self.model(**__magic_name__   ).logits



							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     int   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      Dict     						=  outputs.argmax(-1   ).item()
													return self.model.config.idalabel[idx]





