






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 		UpperCamelCase__    :




									'''simple docstring'''





									@staticmethod
									def 			UpperCamelCase_					(      *_lowerCAmelCase   ,**_lowerCAmelCase       ):
															pass




@is_pipeline_test
@require_torch
@require_vision
class 		UpperCamelCase__    (unittest.TestCase			):




		'''simple docstring'''





		_UpperCamelCase				  =					MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       ):
								lowerCamelCase__         =  pipeline("""visual-question-answering"""   ,model="""hf-internal-testing/tiny-vilt-random-vqa"""       )
								lowerCamelCase__         =  [
								    {
								        """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 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase       ):
								lowerCamelCase__         =  vqa_pipeline(_lowerCAmelCase   ,top_k=1       )
								self.assertEqual(
								    _lowerCAmelCase   ,[
								        [{"""score""": ANY(_lowerCAmelCase       ), """answer""": ANY(_lowerCAmelCase       )}],
								        [{"""score""": ANY(_lowerCAmelCase       ), """answer""": ANY(_lowerCAmelCase       )}],
								    ]   ,)






		@require_torch
		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  pipeline("""visual-question-answering"""   ,model="""hf-internal-testing/tiny-vilt-random-vqa"""       )
								lowerCamelCase__         =  """./tests/fixtures/tests_samples/COCO/000000039769.png"""
								lowerCamelCase__         =  """How many cats are there?"""

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

								lowerCamelCase__         =  vqa_pipeline({"""image""": image, """question""": question}   ,top_k=2       )
								self.assertEqual(
								    _lowerCAmelCase   ,[{"""score""": ANY(_lowerCAmelCase       ), """answer""": ANY(_lowerCAmelCase       )}, {"""score""": ANY(_lowerCAmelCase       ), """answer""": ANY(_lowerCAmelCase       )}]       )






		@slow
		@require_torch
		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  pipeline("""visual-question-answering"""   ,model="""dandelin/vilt-b32-finetuned-vqa"""       )
								lowerCamelCase__         =  """./tests/fixtures/tests_samples/COCO/000000039769.png"""
								lowerCamelCase__         =  """How many cats are there?"""

								lowerCamelCase__         =  vqa_pipeline(image=_lowerCAmelCase   ,question=_lowerCAmelCase   ,top_k=2       )
								self.assertEqual(
								    nested_simplify(_lowerCAmelCase   ,decimals=4       )   ,[{"""score""": 0.8799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}]       )

								lowerCamelCase__         =  vqa_pipeline({"""image""": image, """question""": question}   ,top_k=2       )
								self.assertEqual(
								    nested_simplify(_lowerCAmelCase   ,decimals=4       )   ,[{"""score""": 0.8799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}]       )

								lowerCamelCase__         =  vqa_pipeline(
								    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]   ,top_k=2       )
								self.assertEqual(
								    nested_simplify(_lowerCAmelCase   ,decimals=4       )   ,[[{"""score""": 0.8799, """answer""": """2"""}, {"""score""": 0.296, """answer""": """1"""}]] * 2   ,)






		@require_tf
		@unittest.skip("""Visual question answering not implemented in TF"""       )
		def 			UpperCamelCase_					(      self       ):
								pass









'''simple docstring'''







import json
from typing import Iterator, List, Union

from tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers, trainers
from tokenizers.implementations.base_tokenizer import BaseTokenizer
from tokenizers.models import Unigram
from tokenizers.processors import TemplateProcessing




class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		def __init__(      self   ,_lowerCAmelCase = "▁"   ,_lowerCAmelCase = True   ,_lowerCAmelCase = "<unk>"   ,_lowerCAmelCase = "</s>"   ,_lowerCAmelCase = "<pad>"   ,):
								lowerCamelCase__         =  {
								    """pad""": {"""id""": 0, """token""": pad_token},
								    """eos""": {"""id""": 1, """token""": eos_token},
								    """unk""": {"""id""": 2, """token""": unk_token},
								}

								lowerCamelCase__         =  [None] * len(self.special_tokens       )
								for token_dict in self.special_tokens.values():
														lowerCamelCase__         =  token_dict["""token"""]

								lowerCamelCase__         =  Tokenizer(Unigram()       )

								lowerCamelCase__         =  normalizers.Sequence(
								    [
								        normalizers.Nmt(),
								        normalizers.NFKC(),
								        normalizers.Replace(Regex(""" {2,}"""       )   ,""" """       ),
								        normalizers.Lowercase(),
								    ]       )
								lowerCamelCase__         =  pre_tokenizers.Sequence(
								    [
								        pre_tokenizers.Metaspace(replacement=_lowerCAmelCase   ,add_prefix_space=_lowerCAmelCase       ),
								        pre_tokenizers.Digits(individual_digits=_lowerCAmelCase       ),
								        pre_tokenizers.Punctuation(),
								    ]       )
								lowerCamelCase__         =  decoders.Metaspace(replacement=_lowerCAmelCase   ,add_prefix_space=_lowerCAmelCase       )

								lowerCamelCase__         =  TemplateProcessing(
								    single=F'''$A {self.special_tokens["eos"]["token"]}'''   ,special_tokens=[(self.special_tokens["""eos"""]["""token"""], self.special_tokens["""eos"""]["""id"""])]   ,)

								lowerCamelCase__         =  {
								    """model""": """SentencePieceUnigram""",
								    """replacement""": replacement,
								    """add_prefix_space""": add_prefix_space,
								}

								super().__init__(_lowerCAmelCase   ,_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = 80_00   ,_lowerCAmelCase = True   ,):
								lowerCamelCase__         =  trainers.UnigramTrainer(
								    vocab_size=_lowerCAmelCase   ,special_tokens=self.special_tokens_list   ,show_progress=_lowerCAmelCase   ,)

								if isinstance(_lowerCAmelCase   ,_lowerCAmelCase       ):
														lowerCamelCase__         =  [files]
								self._tokenizer.train(_lowerCAmelCase   ,trainer=_lowerCAmelCase       )

								self.add_unk_id()






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = 80_00   ,_lowerCAmelCase = True   ,):
								lowerCamelCase__         =  trainers.UnigramTrainer(
								    vocab_size=_lowerCAmelCase   ,special_tokens=self.special_tokens_list   ,show_progress=_lowerCAmelCase   ,)

								self._tokenizer.train_from_iterator(_lowerCAmelCase   ,trainer=_lowerCAmelCase       )

								self.add_unk_id()






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  json.loads(self._tokenizer.to_str()       )

								lowerCamelCase__         =  self.special_tokens["""unk"""]["""id"""]

								lowerCamelCase__         =  Tokenizer.from_str(json.dumps(_lowerCAmelCase       )       )

