






"""simple docstring"""
import json
import os
import unittest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin



@require_tokenizers
class      SCREAMING_SNAKE_CASE  (				_SCREAMING_SNAKE_CASE       ,       unittest.TestCase						):





	"""simple docstring"""




	_A       :				Optional[int]    =				CLIPTokenizer
	_A       :				Dict    =				CLIPTokenizerFast
	_A       :				int    =				True
	_A       :				Union[str, Any]    =				{}
	_A       :				Optional[int]    =				False





	def    lowerCamelCase(self							):
		super().setUp()

		# fmt: off
		A_		:						Dict	    =   ["""l""", """o""", """w""", """e""", """r""", """s""", """t""", """i""", """d""", """n""", """lo""", """l</w>""", """w</w>""", """r</w>""", """t</w>""", """low</w>""", """er</w>""", """lowest</w>""", """newer</w>""", """wider""", """<unk>""", """<|startoftext|>""", """<|endoftext|>"""]
		# fmt: on
		A_		:						Tuple	    =   dict(zip(lowerCAmelCase_     ,     range(len(lowerCAmelCase_							)							)							)							)
		A_		:						Tuple	    =   ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>"""]
		A_		:						Dict	    =   {"""unk_token""": """<unk>"""}

		A_		:						Union[str, Any]	    =   os.path.join(self.tmpdirname     ,     VOCAB_FILES_NAMES["""vocab_file"""]							)
		A_		:						str	    =   os.path.join(self.tmpdirname     ,     VOCAB_FILES_NAMES["""merges_file"""]							)
		with open(self.vocab_file     ,     """w"""     ,     encoding="""utf-8"""							) as fp:
			fp.write(json.dumps(lowerCAmelCase_							) + """\n"""							)
		with open(self.merges_file     ,     """w"""     ,     encoding="""utf-8"""							) as fp:
			fp.write("""\n""".join(lowerCAmelCase_							)							)





	def    lowerCamelCase(self     ,     **lowerCAmelCase_							):
		kwargs.update(self.special_tokens_map							)
		return CLIPTokenizer.from_pretrained(self.tmpdirname     ,     **lowerCAmelCase_							)





	def    lowerCamelCase(self     ,     **lowerCAmelCase_							):
		kwargs.update(self.special_tokens_map							)
		return CLIPTokenizerFast.from_pretrained(self.tmpdirname     ,     **lowerCAmelCase_							)





	def    lowerCamelCase(self     ,     lowerCAmelCase_							):
		A_		:						Union[str, Any]	    =   """lower newer"""
		A_		:						Tuple	    =   """lower newer"""
		return input_text, output_text





	def    lowerCamelCase(self							):
		A_		:						List[Any]	    =   CLIPTokenizer(self.vocab_file     ,     self.merges_file     ,     **self.special_tokens_map							)
		A_		:						int	    =   """lower newer"""
		A_		:						List[str]	    =   ["""lo""", """w""", """er</w>""", """n""", """e""", """w""", """er</w>"""]
		A_		:						Union[str, Any]	    =   tokenizer.tokenize(lowerCAmelCase_							)
		self.assertListEqual(lowerCAmelCase_     ,     lowerCAmelCase_							)

		A_		:						List[str]	    =   tokens + [tokenizer.unk_token]
		A_		:						Optional[Any]	    =   [10, 2, 16, 9, 3, 2, 16, 20]
		self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase_							)     ,     lowerCAmelCase_							)





	@require_ftfy
	def    lowerCamelCase(self							):
		for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
			with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""							):
				A_		:						int	    =   self.tokenizer_class.from_pretrained(lowerCAmelCase_     ,     **lowerCAmelCase_							)
				A_		:						str	    =   self.rust_tokenizer_class.from_pretrained(lowerCAmelCase_     ,     **lowerCAmelCase_							)

				A_		:						List[str]	    =   """A\n'll 11p223RF☆ho!!to?'d'd''d of a cat to-$''d."""
				A_		:						Dict	    =   tokenizer_s.tokenize(lowerCAmelCase_							)
				A_		:						Optional[Any]	    =   tokenizer_r.tokenize(lowerCAmelCase_							)

				self.assertListEqual(lowerCAmelCase_     ,     lowerCAmelCase_							)

				# Test that the tokenization is identical on an example containing a character (Latin Small Letter A
				# with Tilde) encoded in 2 different ways
				A_		:						Dict	    =   """xa\u0303y""" + """ """ + """x\xe3y"""
				A_		:						Optional[Any]	    =   tokenizer_s.tokenize(lowerCAmelCase_							)
				A_		:						Tuple	    =   tokenizer_r.tokenize(lowerCAmelCase_							)

				self.assertListEqual(lowerCAmelCase_     ,     lowerCAmelCase_							)

				# Test that the tokenization is identical on unicode of space type
				A_		:						Optional[int]	    =   [
				    """\u0009""",  # (horizontal tab, '\t')
				    """\u000B""",  # (vertical tab)
				    """\u000C""",  # (form feed)
				    """\u0020""",  # (space, ' ')
				    """\u200E""",  # (left-to-right mark):w
				    """\u200F""",  # (right-to-left mark)
				]
				for unicode_seq in spaces_unicodes:
					A_		:						Any	    =   tokenizer_s.tokenize(lowerCAmelCase_							)
					A_		:						str	    =   tokenizer_r.tokenize(lowerCAmelCase_							)

					self.assertListEqual(lowerCAmelCase_     ,     lowerCAmelCase_							)

				# Test that the tokenization is identical on unicode of line break type
				A_		:						Union[str, Any]	    =   [
				    """\u000A""",  # (line feed, '\n')
				    """\r\n""",  # (carriage return and line feed, '\r\n')
				    """\u000D""",  # (carriage return, '\r')
				    """\r""",  # (carriage return, '\r')
				    """\u000D""",  # (carriage return, '\r')
				    """\u2028""",  # (line separator)
				    """\u2029""",  # (paragraph separator)
				    # "\u0085", # (next line)
				]

				# The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms
				# it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a
				# space (and thus into an empty list).

				for unicode_seq in line_break_unicodes:
					A_		:						int	    =   tokenizer_s.tokenize(lowerCAmelCase_							)
					A_		:						Optional[Any]	    =   tokenizer_r.tokenize(lowerCAmelCase_							)

					self.assertListEqual(lowerCAmelCase_     ,     lowerCAmelCase_							)





	def    lowerCamelCase(self							):
		# Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space`
		for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
			with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""							):
				A_		:						Dict	    =   """hello"""  # `hello` is a token in the vocabulary of `pretrained_name`
				A_		:						Dict	    =   f"""{text_of_1_token} {text_of_1_token}"""

				A_		:						Optional[Any]	    =   self.rust_tokenizer_class.from_pretrained(
				    lowerCAmelCase_     ,     use_fast=lowerCAmelCase_     ,     )
				A_		:						Any	    =   tokenizer_r(lowerCAmelCase_     ,     return_offsets_mapping=lowerCAmelCase_     ,     add_special_tokens=lowerCAmelCase_							)
				self.assertEqual(encoding.offset_mapping[0]     ,     (0, len(lowerCAmelCase_							))							)
				self.assertEqual(
				    encoding.offset_mapping[1]     ,     (len(lowerCAmelCase_							) + 1, len(lowerCAmelCase_							) + 1 + len(lowerCAmelCase_							))     ,     )

				A_		:						Any	    =   f""" {text}"""

				A_		:						Union[str, Any]	    =   self.rust_tokenizer_class.from_pretrained(
				    lowerCAmelCase_     ,     use_fast=lowerCAmelCase_     ,     )
				A_		:						str	    =   tokenizer_r(lowerCAmelCase_     ,     return_offsets_mapping=lowerCAmelCase_     ,     add_special_tokens=lowerCAmelCase_							)
				self.assertEqual(encoding.offset_mapping[0]     ,     (1, 1 + len(lowerCAmelCase_							))							)
				self.assertEqual(
				    encoding.offset_mapping[1]     ,     (1 + len(lowerCAmelCase_							) + 1, 1 + len(lowerCAmelCase_							) + 1 + len(lowerCAmelCase_							))     ,     )





	def    lowerCamelCase(self							):
		# Test related to the breaking change introduced in transformers v4.17.0
		# We need to check that an error in raised when the user try to load a previous version of the tokenizer.
		with self.assertRaises(lowerCAmelCase_							) as context:
			self.rust_tokenizer_class.from_pretrained("""robot-test/old-clip-tokenizer"""							)

		self.assertTrue(
		    context.exception.args[0].startswith(
		        """The `backend_tokenizer` provided does not match the expected format."""							)							)





	@require_ftfy
	def    lowerCamelCase(self							):
		super().test_tokenization_python_rust_equals()





	def    lowerCamelCase(self							):
		# CLIP always lower cases letters
		pass









"""simple docstring"""
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      SCREAMING_SNAKE_CASE  :





								"""simple docstring"""




								@staticmethod
								def    lowerCamelCase(*lowerCAmelCase_     ,     **lowerCAmelCase_							):
									pass



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





	"""simple docstring"""




	_A       :				List[Any]    =				MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING





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





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

		A_		:						Optional[int]	    =   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_							)}]							)

		A_		:						Any	    =   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    lowerCamelCase(self							):
		A_		:						int	    =   pipeline("""visual-question-answering"""     ,     model="""dandelin/vilt-b32-finetuned-vqa"""							)
		A_		:						Tuple	    =   """./tests/fixtures/tests_samples/COCO/000000039769.png"""
		A_		:						int	    =   """How many cats are there?"""

		A_		:						List[Any]	    =   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"""}]							)

		A_		:						Any	    =   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"""}]							)

		A_		:						Tuple	    =   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    lowerCamelCase(self							):
		pass

