
import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
  from PIL import Image
else:

  class __snake_case						:
      @staticmethod
      def  _SCREAMING_SNAKE_CASE			(    *snake_case						,**snake_case ):



             '''simple docstring'''







             pass

@is_pipeline_test
@require_torch
@require_vision
class __snake_case						( unittest.TestCase	):
    _a						:		List[str]=							MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING
    def  _SCREAMING_SNAKE_CASE			(    self						,snake_case						,snake_case						,snake_case ):



           '''simple docstring'''







           lowercase				:	Any			      = pipeline("""visual-question-answering"""						,model="""hf-internal-testing/tiny-vilt-random-vqa""" )
           lowercase				:	Any			      = [
               {
                   """image""": Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ),
                   """question""": """How many cats are there?""",
               },
               {
                   """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
                   """question""": """How many cats are there?""",
               },
           ]
           return vqa_pipeline, examples
    def  _SCREAMING_SNAKE_CASE			(    self						,snake_case						,snake_case ):



           '''simple docstring'''







           lowercase				:	Union[str, Any]			      = vqa_pipeline(snake_case						,top_k=1 )
           self.assertEqual(
               snake_case						,[
                   [{"""score""": ANY(snake_case ), """answer""": ANY(snake_case )}],
                   [{"""score""": ANY(snake_case ), """answer""": ANY(snake_case )}],
               ]						,)
    @require_torch
    def  _SCREAMING_SNAKE_CASE			(    self ):



           '''simple docstring'''







           lowercase				:	Any			      = pipeline("""visual-question-answering"""						,model="""hf-internal-testing/tiny-vilt-random-vqa""" )
           lowercase				:	Optional[Any]			      = """./tests/fixtures/tests_samples/COCO/000000039769.png"""
           lowercase				:	str			      = """How many cats are there?"""

           lowercase				:	Tuple			      = vqa_pipeline(image=snake_case						,question="""How many cats are there?"""						,top_k=2 )
           self.assertEqual(
               snake_case						,[{"""score""": ANY(snake_case ), """answer""": ANY(snake_case )}, {"""score""": ANY(snake_case ), """answer""": ANY(snake_case )}] )

           lowercase				:	int			      = vqa_pipeline({"""image""": image, """question""": question}						,top_k=2 )
           self.assertEqual(
               snake_case						,[{"""score""": ANY(snake_case ), """answer""": ANY(snake_case )}, {"""score""": ANY(snake_case ), """answer""": ANY(snake_case )}] )
    @slow
    @require_torch
    def  _SCREAMING_SNAKE_CASE			(    self ):



           '''simple docstring'''







           lowercase				:	List[Any]			      = pipeline("""visual-question-answering"""						,model="""dandelin/vilt-b32-finetuned-vqa""" )
           lowercase				:	str			      = """./tests/fixtures/tests_samples/COCO/000000039769.png"""
           lowercase				:	Any			      = """How many cats are there?"""

           lowercase				:	Optional[int]			      = vqa_pipeline(image=snake_case						,question=snake_case						,top_k=2 )
           self.assertEqual(
               nested_simplify(snake_case						,decimals=4 )						,[{"""score""": 0.8_799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}] )

           lowercase				:	List[Any]			      = vqa_pipeline({"""image""": image, """question""": question}						,top_k=2 )
           self.assertEqual(
               nested_simplify(snake_case						,decimals=4 )						,[{"""score""": 0.8_799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}] )

           lowercase				:	Union[str, Any]			      = vqa_pipeline(
               [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]						,top_k=2 )
           self.assertEqual(
               nested_simplify(snake_case						,decimals=4 )						,[[{"""score""": 0.8_799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}]] * 2						,)
    @require_tf
    @unittest.skip("""Visual question answering not implemented in TF""" )
    def  _SCREAMING_SNAKE_CASE			(    self ):



           '''simple docstring'''







           pass




"""simple docstring"""



import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_convbert import ConvBertTokenizer


lowercase_							=							logging.get_logger(__name__)

lowercase_							=							{'vocab_file': 'vocab.txt'}

lowercase_							=							{
    'vocab_file': {
        'YituTech/conv-bert-base': 'https://huggingface.co/YituTech/conv-bert-base/resolve/main/vocab.txt',
        'YituTech/conv-bert-medium-small': (
            'https://huggingface.co/YituTech/conv-bert-medium-small/resolve/main/vocab.txt'
        ),
        'YituTech/conv-bert-small': 'https://huggingface.co/YituTech/conv-bert-small/resolve/main/vocab.txt',
    }
}

lowercase_							=							{
    'YituTech/conv-bert-base': 5_12,
    'YituTech/conv-bert-medium-small': 5_12,
    'YituTech/conv-bert-small': 5_12,
}


lowercase_							=							{
    'YituTech/conv-bert-base': {'do_lower_case': True},
    'YituTech/conv-bert-medium-small': {'do_lower_case': True},
    'YituTech/conv-bert-small': {'do_lower_case': True},
}







class    __a						( __snake_case							):
				lowerCamelCase     :       Optional[int]			       =VOCAB_FILES_NAMES
				lowerCamelCase     :       List[str]			       =PRETRAINED_VOCAB_FILES_MAP
				lowerCamelCase     :       str			       =PRETRAINED_INIT_CONFIGURATION
				lowerCamelCase     :       Any			       =PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				lowerCamelCase     :       List[str]			       =ConvBertTokenizer







				def __init__( self      ,	UpperCAmelCase=None      ,	UpperCAmelCase=None      ,	UpperCAmelCase=True      ,	UpperCAmelCase="[UNK]"      ,	UpperCAmelCase="[SEP]"      ,	UpperCAmelCase="[PAD]"      ,	UpperCAmelCase="[CLS]"      ,	UpperCAmelCase="[MASK]"      ,	UpperCAmelCase=True      ,	UpperCAmelCase=None      ,	**UpperCAmelCase      ,	):

					'''simple docstring'''






					super().__init__(
					    UpperCAmelCase      ,	tokenizer_file=UpperCAmelCase      ,	do_lower_case=UpperCAmelCase      ,	unk_token=UpperCAmelCase      ,	sep_token=UpperCAmelCase      ,	pad_token=UpperCAmelCase      ,	cls_token=UpperCAmelCase      ,	mask_token=UpperCAmelCase      ,	tokenize_chinese_chars=UpperCAmelCase      ,	strip_accents=UpperCAmelCase      ,	**UpperCAmelCase      ,	)

					lowerCAmelCase_      							=			json.loads(self.backend_tokenizer.normalizer.__getstate__()   )
					if (
					    normalizer_state.get('''lowercase'''      ,	UpperCAmelCase   ) != do_lower_case
					    or normalizer_state.get('''strip_accents'''      ,	UpperCAmelCase   ) != strip_accents
					    or normalizer_state.get('''handle_chinese_chars'''      ,	UpperCAmelCase   ) != tokenize_chinese_chars
					):
						lowerCAmelCase_      							=			getattr(UpperCAmelCase      ,	normalizer_state.pop('''type'''   )   )
						lowerCAmelCase_      							=			do_lower_case
						lowerCAmelCase_      							=			strip_accents
						lowerCAmelCase_      							=			tokenize_chinese_chars
						lowerCAmelCase_      							=			normalizer_class(**UpperCAmelCase   )

					lowerCAmelCase_      							=			do_lower_case







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase=None   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			[self.cls_token_id] + token_ids_a + [self.sep_token_id]

					if token_ids_a:
						output += token_ids_a + [self.sep_token_id]

					return output







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase = None   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			[self.sep_token_id]
					lowerCAmelCase_      							=			[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      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase = None   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self._tokenizer.model.save(UpperCAmelCase      ,	name=UpperCAmelCase   )
					return tuple(UpperCAmelCase   )