
'''simple docstring'''




import os
import unittest

from transformers import FunnelTokenizer, FunnelTokenizerFast
from transformers.models.funnel.tokenization_funnel import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin





@require_tokenizers
class  A				(							__snake_case    ,			unittest.TestCase						):
     __magic_name__						=		FunnelTokenizer
     __magic_name__						=		FunnelTokenizerFast
     __magic_name__						=		True
     __magic_name__						=		True





     def 				__lowerCAmelCase							(							self	) ->				Dict:






         """simple docstring"""






         super().setUp()

         A       :   List[str]					=						[
             '''<unk>''',
             '''<cls>''',
             '''<sep>''',
             '''want''',
             '''##want''',
             '''##ed''',
             '''wa''',
             '''un''',
             '''runn''',
             '''##ing''',
             ''',''',
             '''low''',
             '''lowest''',
         ]
         A       :   Dict					=						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 				__lowerCAmelCase							(							self						,			**SCREAMING_SNAKE_CASE	) ->				Optional[Any]:






         """simple docstring"""






         return FunnelTokenizer.from_pretrained(self.tmpdirname						,			**SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self						,			**SCREAMING_SNAKE_CASE	) ->				Any:






         """simple docstring"""






         return FunnelTokenizerFast.from_pretrained(self.tmpdirname						,			**SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE	) ->				Any:






         """simple docstring"""






         A       :   Union[str, Any]					=						'''UNwant\u00E9d,running'''
         A       :   Tuple					=						'''unwanted, running'''
         return input_text, output_text





     def 				__lowerCAmelCase							(							self	) ->				List[str]:






         """simple docstring"""






         A       :   Union[str, Any]					=						self.tokenizer_class(self.vocab_file	)

         A       :   Dict					=						tokenizer.tokenize('''UNwant\u00E9d,running'''	)
         self.assertListEqual(SCREAMING_SNAKE_CASE						,			['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing''']	)
         self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE	)						,			[7, 4, 5, 10, 8, 9]	)





     def 				__lowerCAmelCase							(							self	) ->				List[str]:






         """simple docstring"""






         A       :   List[str]					=						self.get_tokenizers(do_lower_case=SCREAMING_SNAKE_CASE	)
         for tokenizer in tokenizers:
             A       :   Dict					=						tokenizer('''UNwant\u00E9d,running'''	)
             A       :   Tuple					=						len(inputs['''input_ids''']	) - 1
             self.assertListEqual(inputs['''token_type_ids''']						,			[2] + [0] * sentence_len	)

             A       :   int					=						tokenizer('''UNwant\u00E9d,running'''						,			'''UNwant\u00E9d,running'''	)
             self.assertListEqual(inputs['''token_type_ids''']						,			[2] + [0] * sentence_len + [1] * sentence_len	)






'''simple docstring'''




from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
      from PIL import Image

      from ..image_utils import load_image

if is_torch_available():
      from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

lowercase			:				str    		=			logging.get_logger(__name__)





@add_end_docstrings(__snake_case						)
class  A				(							__snake_case						):





     def __init__(							self						,			*SCREAMING_SNAKE_CASE						,			**SCREAMING_SNAKE_CASE	) ->				List[Any]:






         """simple docstring"""






         super().__init__(*SCREAMING_SNAKE_CASE						,			**SCREAMING_SNAKE_CASE	)
         self.check_model_type(SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE=None						,			SCREAMING_SNAKE_CASE=None						,			SCREAMING_SNAKE_CASE=None						,			**SCREAMING_SNAKE_CASE	) ->				Tuple:






         """simple docstring"""






         A,  A       :   Dict					=						{}, {}
         if padding is not None:
             A       :   List[str]					=						padding
         if truncation is not None:
             A       :   Dict					=						truncation
         if top_k is not None:
             A       :   Optional[Any]					=						top_k
         return preprocess_params, {}, postprocess_params





     def __call__(							self						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE = None						,			**SCREAMING_SNAKE_CASE	) ->				Dict:






         """simple docstring"""






         if isinstance(SCREAMING_SNAKE_CASE						,			(Image.Image, str)	) and isinstance(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	):
             A       :   int					=						{'''image''': image, '''question''': question}
         else:
             A       :   Any					=						image
         A       :   Any					=						super().__call__(SCREAMING_SNAKE_CASE						,			**SCREAMING_SNAKE_CASE	)
         return results





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE=False						,			SCREAMING_SNAKE_CASE=False	) ->				Any:






         """simple docstring"""






         A       :   Union[str, Any]					=						load_image(inputs['''image''']	)
         A       :   Optional[Any]					=						self.tokenizer(
             inputs['''question''']						,			return_tensors=self.framework						,			padding=SCREAMING_SNAKE_CASE						,			truncation=SCREAMING_SNAKE_CASE	)
         A       :   Dict					=						self.image_processor(images=SCREAMING_SNAKE_CASE						,			return_tensors=self.framework	)
         model_inputs.update(SCREAMING_SNAKE_CASE	)
         return model_inputs





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE	) ->				List[str]:






         """simple docstring"""






         A       :   List[Any]					=						self.model(**SCREAMING_SNAKE_CASE	)
         return model_outputs





     def 				__lowerCAmelCase							(							self						,			SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE=5	) ->				int:






         """simple docstring"""






         if top_k > self.model.config.num_labels:
             A       :   Dict					=						self.model.config.num_labels

         if self.framework == "pt":
             A       :   Optional[int]					=						model_outputs.logits.sigmoid()[0]
             A,  A       :   int					=						probs.topk(SCREAMING_SNAKE_CASE	)
         else:
             raise ValueError(F'Unsupported framework: {self.framework}'	)

         A       :   int					=						scores.tolist()
         A       :   List[str]					=						ids.tolist()
         return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(SCREAMING_SNAKE_CASE						,			SCREAMING_SNAKE_CASE	)]




