
"""simple docstring"""





import os
import re
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


_a														=		logging.get_logger(__name__)

_a														=		{"""vocab_file""": """spiece.model"""}

_a														=		{
    """vocab_file""": {
        """google/bigbird-roberta-base""": """https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model""",
        """google/bigbird-roberta-large""": (
            """https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model"""
        ),
        """google/bigbird-base-trivia-itc""": (
            """https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model"""
        ),
    }
}

_a														=		{
    """google/bigbird-roberta-base""": 4096,
    """google/bigbird-roberta-large""": 4096,
    """google/bigbird-base-trivia-itc""": 4096,
}






class    _UpperCAmelCase(   lowerCamelCase     ):
		lowercase__      =				VOCAB_FILES_NAMES
		lowercase__      =				PRETRAINED_VOCAB_FILES_MAP
		lowercase__      =				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		lowercase__      =				['input_ids', 'attention_mask']
		lowercase__      =				[]



		def __init__( self    ,		__a    ,		__a="<unk>"    ,		__a="<s>"    ,		__a="</s>"    ,		__a="<pad>"    ,		__a="[SEP]"    ,		__a="[MASK]"    ,		__a="[CLS]"    ,		__a = None    ,		**__a    ,		)	->	None:






							'''simple docstring'''
							_UpperCamelCase									=  AddedToken(__a    ,		lstrip=__a    ,		rstrip=__a) if isinstance(__a    ,		__a) else bos_token
							_UpperCamelCase									=  AddedToken(__a    ,		lstrip=__a    ,		rstrip=__a) if isinstance(__a    ,		__a) else eos_token
							_UpperCamelCase									=  AddedToken(__a    ,		lstrip=__a    ,		rstrip=__a) if isinstance(__a    ,		__a) else unk_token
							_UpperCamelCase									=  AddedToken(__a    ,		lstrip=__a    ,		rstrip=__a) if isinstance(__a    ,		__a) else pad_token
							_UpperCamelCase									=  AddedToken(__a    ,		lstrip=__a    ,		rstrip=__a) if isinstance(__a    ,		__a) else cls_token
							_UpperCamelCase									=  AddedToken(__a    ,		lstrip=__a    ,		rstrip=__a) if isinstance(__a    ,		__a) else sep_token

							# Mask token behave like a normal word, i.e. include the space before it
							_UpperCamelCase									=  AddedToken(__a    ,		lstrip=__a    ,		rstrip=__a) if isinstance(__a    ,		__a) else mask_token

							_UpperCamelCase									=  {} if sp_model_kwargs is None else sp_model_kwargs

							super().__init__(
							    bos_token=__a    ,		eos_token=__a    ,		unk_token=__a    ,		pad_token=__a    ,		sep_token=__a    ,		mask_token=__a    ,		cls_token=__a    ,		sp_model_kwargs=self.sp_model_kwargs    ,		**__a    ,		)

							_UpperCamelCase									=  vocab_file

							_UpperCamelCase									=  spm.SentencePieceProcessor(**self.sp_model_kwargs)
							self.sp_model.Load(__a)



		@property
		def 						UpperCAmelCase							( self)	->	Tuple:






							'''simple docstring'''
							return self.sp_model.get_piece_size()



		def 						UpperCAmelCase							( self)	->	Any:






							'''simple docstring'''
							_UpperCamelCase									=  {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)	->	List[str]:






							'''simple docstring'''
							_UpperCamelCase									=  self.__dict__.copy()
							_UpperCamelCase									=  None
							return state



		def __setstate__( self    ,		__a)	->	Union[str, Any]:






							'''simple docstring'''
							_UpperCamelCase									=  d

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

							_UpperCamelCase									=  spm.SentencePieceProcessor(**self.sp_model_kwargs)
							self.sp_model.Load(self.vocab_file)



		def 						UpperCAmelCase							( self    ,		__a)	->	List[str]:






							'''simple docstring'''
							return self.sp_model.encode(__a    ,		out_type=__a)



		def 						UpperCAmelCase							( self    ,		__a)	->	Union[str, Any]:






							'''simple docstring'''
							return self.sp_model.piece_to_id(__a)



		def 						UpperCAmelCase							( self    ,		__a)	->	List[Any]:






							'''simple docstring'''
							_UpperCamelCase									=  self.sp_model.IdToPiece(__a)
							return token



		def 						UpperCAmelCase							( self    ,		__a)	->	Dict:






							'''simple docstring'''
							_UpperCamelCase									=  []
							_UpperCamelCase									=  ''''''
							_UpperCamelCase									=  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									=  True
																	_UpperCamelCase									=  []
												else:
																	current_sub_tokens.append(__a)
																	_UpperCamelCase									=  False
							out_string += self.sp_model.decode(__a)
							return out_string.strip()



		def 						UpperCAmelCase							( self    ,		__a    ,		__a = False    ,		__a = None    ,		__a = True    ,		**__a    ,		)	->	str:






							'''simple docstring'''
							_UpperCamelCase									=  kwargs.pop('''use_source_tokenizer'''    ,		__a)

							_UpperCamelCase									=  self.convert_ids_to_tokens(__a    ,		skip_special_tokens=__a)

							# To avoid mixing byte-level and unicode for byte-level BPT
							# we need to build string separately for added tokens and byte-level tokens
							# cf. https://github.com/huggingface/transformers/issues/1133
							_UpperCamelCase									=  []
							_UpperCamelCase									=  []
							for token in filtered_tokens:
												if skip_special_tokens and token in self.all_special_ids:
																	continue
												if token in self.added_tokens_encoder:
																	if current_sub_text:
																						sub_texts.append(self.convert_tokens_to_string(__a))
																						_UpperCamelCase									=  []
																	sub_texts.append(__a)
												else:
																	current_sub_text.append(__a)
							if current_sub_text:
												sub_texts.append(self.convert_tokens_to_string(__a))

							# Mimic the behavior of the Rust tokenizer:
							# No space before [MASK] and [SEP]
							if spaces_between_special_tokens:
												_UpperCamelCase									=  re.sub(R''' (\[(MASK|SEP)\])'''    ,		R'''\1'''    ,		''' '''.join(__a))
							else:
												_UpperCamelCase									=  ''''''.join(__a)

							_UpperCamelCase									=  (
							    clean_up_tokenization_spaces
							    if clean_up_tokenization_spaces is not None
							    else self.clean_up_tokenization_spaces
							)
							if clean_up_tokenization_spaces:
												_UpperCamelCase									=  self.clean_up_tokenization(__a)
												return clean_text
							else:
												return text



		def 						UpperCAmelCase							( self    ,		__a    ,		__a = None)	->	Tuple[str]:






							'''simple docstring'''
							if not os.path.isdir(__a):
												logger.error(F'''Vocabulary path ({save_directory}) should be a directory''')
												return
							_UpperCamelCase									=  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									=  self.sp_model.serialized_model_proto()
																	fi.write(__a)

							return (out_vocab_file,)



		def 						UpperCAmelCase							( self    ,		__a    ,		__a = None)	->	List[int]:






							'''simple docstring'''
							if token_ids_a is None:
												return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
							_UpperCamelCase									=  [self.cls_token_id]
							_UpperCamelCase									=  [self.sep_token_id]
							return cls + token_ids_a + sep + token_ids_a + sep



		def 						UpperCAmelCase							( self    ,		__a    ,		__a = None    ,		__a = False)	->	List[int]:






							'''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 None:
												return [1] + ([0] * len(__a)) + [1]
							return [1] + ([0] * len(__a)) + [1] + ([0] * len(__a)) + [1]




		def 						UpperCAmelCase							( self    ,		__a    ,		__a = None)	->	List[int]:






							'''simple docstring'''
							_UpperCamelCase									=  [self.sep_token_id]
							_UpperCamelCase									=  [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]








"""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    _UpperCAmelCase(   lowerCamelCase     ):
		lowercase__      =				'dandelin/vilt-b32-finetuned-vqa'
		lowercase__      =				(
		    '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.'
		)
		lowercase__      =				'image_qa'
		lowercase__      =				AutoProcessor
		lowercase__      =				AutoModelForVisualQuestionAnswering

		lowercase__      =				['image', 'text']
		lowercase__      =				['text']



		def __init__( self    ,		*__a    ,		**__a)	->	int:






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



		def 						UpperCAmelCase							( self    ,		__a    ,		__a)	->	Dict:






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



		def 						UpperCAmelCase							( self    ,		__a)	->	Tuple:






							'''simple docstring'''
							with torch.no_grad():
												return self.model(**__a).logits




		def 						UpperCAmelCase							( self    ,		__a)	->	Optional[Any]:






							'''simple docstring'''
							_UpperCamelCase										=  outputs.argmax(-1).item()
							return self.model.config.idalabel[idx]






