





'''simple docstring'''






import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin




class 			UpperCAmelCase					(      snake_case_							,		unittest.TestCase       ):
      _lowercase:      List[Any]											=       TransfoXLTokenizer
      _lowercase:      str											=       False
      _lowercase:      Optional[Any]											=       False




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				Optional[int]:
            super().setUp()

            _lowerCAmelCase												=							[
                """<unk>""",
                """[CLS]""",
                """[SEP]""",
                """want""",
                """unwanted""",
                """wa""",
                """un""",
                """running""",
                """,""",
                """low""",
                """l""",
            ]
            _lowerCAmelCase												=							os.path.join(self.tmpdirname      ,      VOCAB_FILES_NAMES["""vocab_file"""]  )
            with open(self.vocab_file      ,      """w"""      ,      encoding="""utf-8"""  ) as vocab_writer:
                  vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]  )  )




      def     lowercase__   (      self      :			List[Any]      ,      **__snake_case      :			Any  )    ->				Tuple:
            _lowerCAmelCase												=							True
            return TransfoXLTokenizer.from_pretrained(self.tmpdirname      ,      **__snake_case  )




      def     lowercase__   (      self      :			str      ,      __snake_case      :			Dict  )    ->				Dict:
            _lowerCAmelCase												=							"""<unk> UNwanted , running"""
            _lowerCAmelCase												=							"""<unk> unwanted, running"""
            return input_text, output_text




      def     lowercase__   (      self      :			Optional[int]  )    ->				str:
            _lowerCAmelCase												=							TransfoXLTokenizer(vocab_file=self.vocab_file      ,      lower_case=__snake_case  )

            _lowerCAmelCase												=							tokenizer.tokenize("""<unk> UNwanted , running"""  )
            self.assertListEqual(__snake_case      ,      ["""<unk>""", """unwanted""", """,""", """running"""]  )

            self.assertListEqual(tokenizer.convert_tokens_to_ids(__snake_case  )      ,      [0, 4, 8, 7]  )




      def     lowercase__   (      self      :			List[Any]  )    ->				Any:
            _lowerCAmelCase												=							TransfoXLTokenizer(lower_case=__snake_case  )

            self.assertListEqual(
                tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """  )      ,      ["""hello""", """!""", """how""", """are""", """you""", """?"""]  )




      def     lowercase__   (      self      :			Optional[Any]  )    ->				str:
            _lowerCAmelCase												=							TransfoXLTokenizer(lower_case=__snake_case  )

            self.assertListEqual(
                tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """  )      ,      ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""]  )




      def     lowercase__   (      self      :			str  )    ->				List[Any]:
            _lowerCAmelCase												=							TransfoXLTokenizer(lower_case=__snake_case  )
            _lowerCAmelCase												=							"""Hello (bracket) and side-scrolled [and] Henry's $5,000 with 3.34 m. What's up!?"""
            _lowerCAmelCase												=							[
                """Hello""",
                """(""",
                """bracket""",
                """)""",
                """and""",
                """side""",
                """@-@""",
                """scrolled""",
                """[""",
                """and""",
                """]""",
                """Henry""",
                """'s""",
                """$""",
                """5""",
                """@,@""",
                """000""",
                """with""",
                """3""",
                """@.@""",
                """34""",
                """m""",
                """.""",
                """What""",
                """'s""",
                """up""",
                """!""",
                """?""",
            ]

            self.assertListEqual(tokenizer.tokenize(__snake_case  )      ,      __snake_case  )

            self.assertEqual(tokenizer.convert_tokens_to_string(__snake_case  )      ,      __snake_case  )




      def     lowercase__   (      self      :			int  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							self.get_tokenizer()
            _lowerCAmelCase												=							len(__snake_case  )

            tokenizer.add_tokens(["""new1""", """new2"""]  )
            tokenizer.move_added_token("""new1"""      ,      1  )

            # Check that moved token is not copied (duplicate)
            self.assertEqual(len(__snake_case  )      ,      original_len + 2  )
            # Check that token is moved to specified id
            self.assertEqual(tokenizer.encode("""new1"""  )      ,      [1]  )
            self.assertEqual(tokenizer.decode([1]  )      ,      """new1"""  )













'''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 			UpperCAmelCase					:




            @staticmethod
            def     lowercase__   (      *__snake_case      :			Optional[Any]      ,      **__snake_case      :			Any  )    ->				Tuple:
                  pass




@is_pipeline_test
@require_torch
@require_vision
class 			UpperCAmelCase					(      unittest.TestCase       ):
      _lowercase:      Union[str, Any]											=       MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING




      def     lowercase__   (      self      :			List[str]      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			str      ,      __snake_case      :			List[str]  )    ->				int:
            _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     lowercase__   (      self      :			Any      ,      __snake_case      :			List[Any]      ,      __snake_case      :			List[Any]  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							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     lowercase__   (      self      :			str  )    ->				int:
            _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=__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  )}]  )

            _lowerCAmelCase												=							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     lowercase__   (      self      :			List[Any]  )    ->				List[str]:
            _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=__snake_case      ,      question=__snake_case      ,      top_k=2  )
            self.assertEqual(
                nested_simplify(__snake_case      ,      decimals=4  )      ,      [{"""score""": 0.87_99, """answer""": """2"""}, {"""score""": 0.2_96, """answer""": """1"""}]  )

            _lowerCAmelCase												=							vqa_pipeline({"""image""": image, """question""": question}      ,      top_k=2  )
            self.assertEqual(
                nested_simplify(__snake_case      ,      decimals=4  )      ,      [{"""score""": 0.87_99, """answer""": """2"""}, {"""score""": 0.2_96, """answer""": """1"""}]  )

            _lowerCAmelCase												=							vqa_pipeline(
                [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]      ,      top_k=2  )
            self.assertEqual(
                nested_simplify(__snake_case      ,      decimals=4  )      ,      [[{"""score""": 0.87_99, """answer""": """2"""}, {"""score""": 0.2_96, """answer""": """1"""}]] * 2      ,      )




      @require_tf
      @unittest.skip("""Visual question answering not implemented in TF"""  )
      def     lowercase__   (      self      :			List[str]  )    ->				Union[str, Any]:
            pass






