


"""simple docstring"""







import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin




class 				UpperCamelCase_			(				UpperCamelCase):

	"""simple docstring"""





	snake_case__       :						Dict					      =    "MCTCTFeatureExtractor"
	snake_case__       :						List[str]					      =    "AutoTokenizer"




	def __init__(				self    :       Optional[Any]    ,    UpperCAmelCase__    :       int    ,    UpperCAmelCase__    :       Optional[int]     ) ->			Tuple:
						super().__init__(UpperCAmelCase__    ,    UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       self.feature_extractor
						__SCREAMING_SNAKE_CASE		     =       False




	def __call__(				self    :       Union[str, Any]    ,    *UpperCAmelCase__    :       Any    ,    **UpperCAmelCase__    :       int     ) ->			Union[str, Any]:
						# For backward compatibility
						if self._in_target_context_manager:
											return self.current_processor(*UpperCAmelCase__    ,    **UpperCAmelCase__     )

						if "raw_speech" in kwargs:
											warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead."     )
											__SCREAMING_SNAKE_CASE		     =       kwargs.pop("raw_speech"     )
						else:
											__SCREAMING_SNAKE_CASE		     =       kwargs.pop("audio"    ,    UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       kwargs.pop("sampling_rate"    ,    UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       kwargs.pop("text"    ,    UpperCAmelCase__     )
						if len(UpperCAmelCase__     ) > 0:
											__SCREAMING_SNAKE_CASE		     =       args[0]
											__SCREAMING_SNAKE_CASE		     =       args[1:]

						if audio is None and text is None:
											raise ValueError("You need to specify either an `audio` or `text` input to process."     )

						if audio is not None:
											__SCREAMING_SNAKE_CASE		     =       self.feature_extractor(UpperCAmelCase__    ,    *UpperCAmelCase__    ,    sampling_rate=UpperCAmelCase__    ,    **UpperCAmelCase__     )
						if text is not None:
											__SCREAMING_SNAKE_CASE		     =       self.tokenizer(UpperCAmelCase__    ,    **UpperCAmelCase__     )

						if text is None:
											return inputs
						elif audio is None:
											return encodings
						else:
											__SCREAMING_SNAKE_CASE		     =       encodings["input_ids"]
											return inputs




	def 						UpperCAmelCase_   (				self    :       Optional[Any]    ,    *UpperCAmelCase__    :       Tuple    ,    **UpperCAmelCase__    :       Union[str, Any]     ) ->			Any:
						return self.tokenizer.batch_decode(*UpperCAmelCase__    ,    **UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       Optional[Any]    ,    *UpperCAmelCase__    :       Tuple    ,    **UpperCAmelCase__    :       Dict     ) ->			Dict:
						# For backward compatibility
						if self._in_target_context_manager:
											return self.current_processor.pad(*UpperCAmelCase__    ,    **UpperCAmelCase__     )

						__SCREAMING_SNAKE_CASE		     =       kwargs.pop("input_features"    ,    UpperCAmelCase__     )
						__SCREAMING_SNAKE_CASE		     =       kwargs.pop("labels"    ,    UpperCAmelCase__     )
						if len(UpperCAmelCase__     ) > 0:
											__SCREAMING_SNAKE_CASE		     =       args[0]
											__SCREAMING_SNAKE_CASE		     =       args[1:]

						if input_features is not None:
											__SCREAMING_SNAKE_CASE		     =       self.feature_extractor.pad(UpperCAmelCase__    ,    *UpperCAmelCase__    ,    **UpperCAmelCase__     )
						if labels is not None:
											__SCREAMING_SNAKE_CASE		     =       self.tokenizer.pad(UpperCAmelCase__    ,    **UpperCAmelCase__     )

						if labels is None:
											return input_features
						elif input_features is None:
											return labels
						else:
											__SCREAMING_SNAKE_CASE		     =       labels["input_ids"]
											return input_features




	def 						UpperCAmelCase_   (				self    :       Dict    ,    *UpperCAmelCase__    :       Dict    ,    **UpperCAmelCase__    :       Any     ) ->			Tuple:
						return self.tokenizer.decode(*UpperCAmelCase__    ,    **UpperCAmelCase__     )




	@contextmanager
	def 						UpperCAmelCase_   (				self    :       List[str]     ) ->			Union[str, Any]:
						warnings.warn(
						    "`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your "
						    "labels by using the argument `text` of the regular `__call__` method (either in the same call as "
						    "your audio inputs, or in a separate call."     )
						__SCREAMING_SNAKE_CASE		     =       True
						__SCREAMING_SNAKE_CASE		     =       self.tokenizer
						yield
						__SCREAMING_SNAKE_CASE		     =       self.feature_extractor
						__SCREAMING_SNAKE_CASE		     =       False




"""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_			:

			"""simple docstring"""





			@staticmethod
			def 						UpperCAmelCase_   (				*UpperCAmelCase__    :       List[Any]    ,    **UpperCAmelCase__    :       Optional[Any]     ) ->			Optional[int]:
								pass




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

	"""simple docstring"""





	snake_case__       :						List[Any]					      =    MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING




	def 						UpperCAmelCase_   (				self    :       Dict    ,    UpperCAmelCase__    :       Tuple    ,    UpperCAmelCase__    :       List[Any]    ,    UpperCAmelCase__    :       Optional[int]     ) ->			str:
						__SCREAMING_SNAKE_CASE		     =       pipeline("visual-question-answering"    ,    model="hf-internal-testing/tiny-vilt-random-vqa"     )
						__SCREAMING_SNAKE_CASE		     =       [
						    {
						        "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    :       Dict    ,    UpperCAmelCase__    :       List[Any]    ,    UpperCAmelCase__    :       Any     ) ->			Any:
						__SCREAMING_SNAKE_CASE		     =       vqa_pipeline(UpperCAmelCase__    ,    top_k=1     )
						self.assertEqual(
						    UpperCAmelCase__    ,    [
						        [{"score": ANY(UpperCAmelCase__     ), "answer": ANY(UpperCAmelCase__     )}],
						        [{"score": ANY(UpperCAmelCase__     ), "answer": ANY(UpperCAmelCase__     )}],
						    ]    ,    )




	@require_torch
	def 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			int:
						__SCREAMING_SNAKE_CASE		     =       pipeline("visual-question-answering"    ,    model="hf-internal-testing/tiny-vilt-random-vqa"     )
						__SCREAMING_SNAKE_CASE		     =       "./tests/fixtures/tests_samples/COCO/000000039769.png"
						__SCREAMING_SNAKE_CASE		     =       "How many cats are there?"

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

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




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

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

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

						__SCREAMING_SNAKE_CASE		     =       vqa_pipeline(
						    [{"image": image, "question": question}, {"image": image, "question": question}]    ,    top_k=2     )
						self.assertEqual(
						    nested_simplify(UpperCAmelCase__    ,    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 						UpperCAmelCase_   (				self    :       str     ) ->			Optional[Any]:
						pass
