
'''simple docstring'''




import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
      from transformers import TvltImageProcessor

if is_speech_available():
      from transformers import TvltFeatureExtractor

from transformers import TvltProcessor





@require_torch
class  A				(							unittest.TestCase						):





     def 				__lowerCAmelCase							(							self	) ->				Union[str, Any]:






         """simple docstring"""






         A       :   Optional[Any]					=						'''ZinengTang/tvlt-base'''
         A       :   List[Any]					=						tempfile.mkdtemp()





     def 				__lowerCAmelCase							(							self						,			**SCREAMING_SNAKE_CASE	) ->				Optional[int]:






         """simple docstring"""






         return TvltImageProcessor.from_pretrained(self.checkpoint						,			**SCREAMING_SNAKE_CASE	)





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






         """simple docstring"""






         return TvltFeatureExtractor.from_pretrained(self.checkpoint						,			**SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				Tuple:






         """simple docstring"""






         shutil.rmtree(self.tmpdirname	)





     def 				__lowerCAmelCase							(							self	) ->				Any:






         """simple docstring"""






         A       :   Tuple					=						self.get_image_processor()
         A       :   Tuple					=						self.get_feature_extractor()

         A       :   Any					=						TvltProcessor(image_processor=SCREAMING_SNAKE_CASE						,			feature_extractor=SCREAMING_SNAKE_CASE	)
         processor.save_pretrained(self.tmpdirname	)
         A       :   List[Any]					=						TvltProcessor.from_pretrained(self.tmpdirname	)

         self.assertIsInstance(processor.feature_extractor						,			SCREAMING_SNAKE_CASE	)
         self.assertIsInstance(processor.image_processor						,			SCREAMING_SNAKE_CASE	)





     def 				__lowerCAmelCase							(							self	) ->				Optional[int]:






         """simple docstring"""






         A       :   str					=						self.get_image_processor()
         A       :   Optional[int]					=						self.get_feature_extractor()

         A       :   int					=						TvltProcessor(image_processor=SCREAMING_SNAKE_CASE						,			feature_extractor=SCREAMING_SNAKE_CASE	)

         A       :   Union[str, Any]					=						np.ones([12000]	)

         A       :   List[Any]					=						feature_extractor(SCREAMING_SNAKE_CASE						,			return_tensors='''np'''	)
         A       :   Dict					=						processor(audio=SCREAMING_SNAKE_CASE						,			return_tensors='''np'''	)

         for key in audio_dict.keys():
             self.assertAlmostEqual(audio_dict[key].sum()						,			input_processor[key].sum()						,			delta=1e-2	)





     def 				__lowerCAmelCase							(							self	) ->				Union[str, Any]:






         """simple docstring"""






         A       :   Tuple					=						self.get_image_processor()
         A       :   int					=						self.get_feature_extractor()

         A       :   int					=						TvltProcessor(image_processor=SCREAMING_SNAKE_CASE						,			feature_extractor=SCREAMING_SNAKE_CASE	)

         A       :   int					=						np.ones([3, 224, 224]	)

         A       :   Optional[Any]					=						image_processor(SCREAMING_SNAKE_CASE						,			return_tensors='''np'''	)
         A       :   Dict					=						processor(images=SCREAMING_SNAKE_CASE						,			return_tensors='''np'''	)

         for key in image_dict.keys():
             self.assertAlmostEqual(image_dict[key].sum()						,			input_processor[key].sum()						,			delta=1e-2	)





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






         """simple docstring"""






         A       :   Tuple					=						self.get_image_processor()
         A       :   Union[str, Any]					=						self.get_feature_extractor()

         A       :   List[str]					=						TvltProcessor(image_processor=SCREAMING_SNAKE_CASE						,			feature_extractor=SCREAMING_SNAKE_CASE	)

         A       :   Dict					=						np.ones([12000]	)
         A       :   Tuple					=						np.ones([3, 224, 224]	)

         A       :   Optional[Any]					=						processor(audio=SCREAMING_SNAKE_CASE						,			images=SCREAMING_SNAKE_CASE	)

         self.assertListEqual(list(inputs.keys()	)						,			['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask''']	)

         # test if it raises when no input is passed
         with pytest.raises(SCREAMING_SNAKE_CASE	):
             processor()





     def 				__lowerCAmelCase							(							self	) ->				int:






         """simple docstring"""






         A       :   List[Any]					=						self.get_image_processor()
         A       :   str					=						self.get_feature_extractor()

         A       :   Optional[int]					=						TvltProcessor(image_processor=SCREAMING_SNAKE_CASE						,			feature_extractor=SCREAMING_SNAKE_CASE	)

         self.assertListEqual(
             processor.model_input_names						,			image_processor.model_input_names + feature_extractor.model_input_names						,			msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match'''						,			)






'''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	)]




