




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

UpperCamelCase__ :   List[str]						=							logging.get_logger(__name__)







@add_end_docstrings(lowerCamelCase_							)
class 				lowerCAmelCase_       (					lowerCamelCase_							):






				def __init__(			self      ,*snake_case__      ,**snake_case__ ):
							super().__init__(*snake_case__      ,**snake_case__ )
							self.check_model_type(snake_case__ )






				def 		snake_case	(			self      ,snake_case__=None      ,snake_case__=None      ,snake_case__=None      ,**snake_case__ ):
							SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      {}, {}
							if padding is not None:
										SCREAMING_SNAKE_CASE_   : Any					  =      padding
							if truncation is not None:
										SCREAMING_SNAKE_CASE_   : Tuple					  =      truncation
							if top_k is not None:
										SCREAMING_SNAKE_CASE_   : int					  =      top_k
							return preprocess_params, {}, postprocess_params






				def __call__(			self      ,snake_case__      ,snake_case__ = None      ,**snake_case__ ):
							if isinstance(snake_case__      ,(Image.Image, str) ) and isinstance(snake_case__      ,snake_case__ ):
										SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      {'image': image, 'question': question}
							else:
										SCREAMING_SNAKE_CASE_   : Optional[int]					  =      image
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      super().__call__(snake_case__      ,**snake_case__ )
							return results






				def 		snake_case	(			self      ,snake_case__      ,snake_case__=False      ,snake_case__=False ):
							SCREAMING_SNAKE_CASE_   : List[str]					  =      load_image(inputs['image'] )
							SCREAMING_SNAKE_CASE_   : Dict					  =      self.tokenizer(
							    inputs['question']      ,return_tensors=self.framework      ,padding=snake_case__      ,truncation=snake_case__ )
							SCREAMING_SNAKE_CASE_   : Any					  =      self.image_processor(images=snake_case__      ,return_tensors=self.framework )
							model_inputs.update(snake_case__ )
							return model_inputs






				def 		snake_case	(			self      ,snake_case__ ):
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      self.model(**snake_case__ )
							return model_outputs






				def 		snake_case	(			self      ,snake_case__      ,snake_case__=5 ):
							if top_k > self.model.config.num_labels:
										SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      self.model.config.num_labels

							if self.framework == "pt":
										SCREAMING_SNAKE_CASE_   : Any					  =      model_outputs.logits.sigmoid()[0]
										SCREAMING_SNAKE_CASE_  ,    SCREAMING_SNAKE_CASE_   : Dict					  =      probs.topk(snake_case__ )
							else:
										raise ValueError(F'Unsupported framework: {self.framework}' )

							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      scores.tolist()
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      ids.tolist()
							return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(snake_case__      ,snake_case__ )]






from __future__ import annotations

import unittest

from transformers import FunnelConfig, is_tf_available
from transformers.testing_utils import require_tf

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers import (
		    TFFunnelBaseModel,
		    TFFunnelForMaskedLM,
		    TFFunnelForMultipleChoice,
		    TFFunnelForPreTraining,
		    TFFunnelForQuestionAnswering,
		    TFFunnelForSequenceClassification,
		    TFFunnelForTokenClassification,
		    TFFunnelModel,
		)







class 					__lowercase  :





							"""simple docstring"""

							def __init__(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase=13    ,						__UpperCAmelCase=7    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=True    ,						__UpperCAmelCase=99    ,						__UpperCAmelCase=[1, 1, 2]    ,						__UpperCAmelCase=1    ,						__UpperCAmelCase=32    ,						__UpperCAmelCase=4    ,						__UpperCAmelCase=8    ,						__UpperCAmelCase=37    ,						__UpperCAmelCase="gelu_new"    ,						__UpperCAmelCase=0.1    ,						__UpperCAmelCase=0.1    ,						__UpperCAmelCase=0.0    ,						__UpperCAmelCase=5_12    ,						__UpperCAmelCase=3    ,						__UpperCAmelCase=0.0_2    ,						__UpperCAmelCase=3    ,						__UpperCAmelCase=4    ,						__UpperCAmelCase=None    ,						__UpperCAmelCase=False    ,						)		->       Tuple:
														A      :					Optional[int]      		=					parent
														A      :					int      		=					batch_size
														A      :					Union[str, Any]      		=					seq_length
														A      :					Optional[Any]      		=					is_training
														A      :					int      		=					use_input_mask
														A      :					List[Any]      		=					use_token_type_ids
														A      :					Optional[int]      		=					use_labels
														A      :					List[Any]      		=					vocab_size
														A      :					int      		=					block_sizes
														A      :					Optional[int]      		=					num_decoder_layers
														A      :					int      		=					d_model
														A      :					Dict      		=					n_head
														A      :					Tuple      		=					d_head
														A      :					int      		=					d_inner
														A      :					Tuple      		=					hidden_act
														A      :					Tuple      		=					hidden_dropout
														A      :					Any      		=					attention_dropout
														A      :					Any      		=					activation_dropout
														A      :					List[Any]      		=					max_position_embeddings
														A      :					int      		=					type_vocab_size
														A      :					Dict      		=					2
														A      :					List[str]      		=					num_labels
														A      :					Optional[int]      		=					num_choices
														A      :					int      		=					scope
														A      :					int      		=					initializer_std

														# Used in the tests to check the size of the first attention layer
														A      :					Optional[int]      		=					n_head
														# Used in the tests to check the size of the first hidden state
														A      :					List[Any]      		=					self.d_model
														# Used in the tests to check the number of output hidden states/attentions
														A      :					Dict      		=					sum(self.block_sizes						) + (0 if base else self.num_decoder_layers)
														# FunnelModel adds two hidden layers: input embeddings and the sum of the upsampled encoder hidden state with
														# the last hidden state of the first block (which is the first hidden state of the decoder).
														if not base:
																					A      :					Union[str, Any]      		=					self.num_hidden_layers + 2




							def       snake_case			(					self						)		->       List[Any]:
														A      :					int      		=					ids_tensor([self.batch_size, self.seq_length]    ,						self.vocab_size						)

														A      :					Any      		=					None
														if self.use_input_mask:
																					A      :					List[Any]      		=					random_attention_mask([self.batch_size, self.seq_length]						)

														A      :					List[str]      		=					None
														if self.use_token_type_ids:
																					A      :					List[Any]      		=					ids_tensor([self.batch_size, self.seq_length]    ,						self.type_vocab_size						)

														A      :					Any      		=					None
														A      :					Union[str, Any]      		=					None
														A      :					Optional[Any]      		=					None
														if self.use_labels:
																					A      :					Tuple      		=					ids_tensor([self.batch_size]    ,						self.type_sequence_label_size						)
																					A      :					Union[str, Any]      		=					ids_tensor([self.batch_size, self.seq_length]    ,						self.num_labels						)
																					A      :					List[Any]      		=					ids_tensor([self.batch_size]    ,						self.num_choices						)

														A      :					List[str]      		=					FunnelConfig(
														    vocab_size=self.vocab_size    ,						block_sizes=self.block_sizes    ,						num_decoder_layers=self.num_decoder_layers    ,						d_model=self.d_model    ,						n_head=self.n_head    ,						d_head=self.d_head    ,						d_inner=self.d_inner    ,						hidden_act=self.hidden_act    ,						hidden_dropout=self.hidden_dropout    ,						attention_dropout=self.attention_dropout    ,						activation_dropout=self.activation_dropout    ,						max_position_embeddings=self.max_position_embeddings    ,						type_vocab_size=self.type_vocab_size    ,						initializer_std=self.initializer_std    ,						)

														return (
														    config,
														    input_ids,
														    token_type_ids,
														    input_mask,
														    sequence_labels,
														    token_labels,
														    choice_labels,
														)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Optional[int]:
														A      :					str      		=					TFFunnelModel(config=__UpperCAmelCase						)
														A      :					str      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					Tuple      		=					model(__UpperCAmelCase						)

														A      :					str      		=					[input_ids, input_mask]
														A      :					Optional[Any]      		=					model(__UpperCAmelCase						)

														A      :					List[str]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.d_model)						)

														A      :					str      		=					False
														A      :					List[str]      		=					TFFunnelModel(config=__UpperCAmelCase						)
														A      :					str      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.d_model)						)

														A      :					Union[str, Any]      		=					False
														A      :					Tuple      		=					TFFunnelModel(config=__UpperCAmelCase						)
														A      :					Any      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, self.seq_length, self.d_model)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Union[str, Any]:
														A      :					Dict      		=					TFFunnelBaseModel(config=__UpperCAmelCase						)
														A      :					Optional[int]      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					List[str]      		=					model(__UpperCAmelCase						)

														A      :					Union[str, Any]      		=					[input_ids, input_mask]
														A      :					Optional[Any]      		=					model(__UpperCAmelCase						)

														A      :					Union[str, Any]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, 2, self.d_model)						)

														A      :					List[Any]      		=					False
														A      :					Union[str, Any]      		=					TFFunnelBaseModel(config=__UpperCAmelCase						)
														A      :					Union[str, Any]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, 3, self.d_model)						)

														A      :					int      		=					False
														A      :					Tuple      		=					TFFunnelBaseModel(config=__UpperCAmelCase						)
														A      :					Tuple      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.last_hidden_state.shape    ,						(self.batch_size, 2, self.d_model)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       List[Any]:
														A      :					List[str]      		=					TFFunnelForPreTraining(config=__UpperCAmelCase						)
														A      :					Any      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					Optional[Any]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       List[Any]:
														A      :					str      		=					TFFunnelForMaskedLM(config=__UpperCAmelCase						)
														A      :					int      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					str      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.vocab_size)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Union[str, Any]:
														A      :					Tuple      		=					self.num_labels
														A      :					List[Any]      		=					TFFunnelForSequenceClassification(config=__UpperCAmelCase						)
														A      :					List[str]      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					Any      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_labels)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Dict:
														A      :					Union[str, Any]      		=					self.num_choices
														A      :					Tuple      		=					TFFunnelForMultipleChoice(config=__UpperCAmelCase						)
														A      :					Tuple      		=					tf.tile(tf.expand_dims(__UpperCAmelCase    ,						1						)    ,						(1, self.num_choices, 1)						)
														A      :					List[Any]      		=					tf.tile(tf.expand_dims(__UpperCAmelCase    ,						1						)    ,						(1, self.num_choices, 1)						)
														A      :					Any      		=					tf.tile(tf.expand_dims(__UpperCAmelCase    ,						1						)    ,						(1, self.num_choices, 1)						)
														A      :					str      		=					{
														    '''input_ids''': multiple_choice_inputs_ids,
														    '''attention_mask''': multiple_choice_input_mask,
														    '''token_type_ids''': multiple_choice_token_type_ids,
														}
														A      :					Any      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.num_choices)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       str:
														A      :					Optional[Any]      		=					self.num_labels
														A      :					List[str]      		=					TFFunnelForTokenClassification(config=__UpperCAmelCase						)
														A      :					int      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					Optional[Any]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.logits.shape    ,						(self.batch_size, self.seq_length, self.num_labels)						)




							def       snake_case			(					self    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						__UpperCAmelCase    ,						)		->       Any:
														A      :					Dict      		=					TFFunnelForQuestionAnswering(config=__UpperCAmelCase						)
														A      :					str      		=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
														A      :					List[Any]      		=					model(__UpperCAmelCase						)
														self.parent.assertEqual(result.start_logits.shape    ,						(self.batch_size, self.seq_length)						)
														self.parent.assertEqual(result.end_logits.shape    ,						(self.batch_size, self.seq_length)						)






							def       snake_case			(					self						)		->       str:
														A      :					Union[str, Any]      		=					self.prepare_config_and_inputs()
														(
														    (
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		(
														    A
														)		,		
														)      :					Optional[Any]      		=					config_and_inputs
														A      :					Union[str, Any]      		=					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
														return config, inputs_dict







@require_tf
class 					__lowercase  (						_SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE		,       unittest.TestCase				):





							"""simple docstring"""

							UpperCAmelCase_						:		Any								=	(
							    (
							        TFFunnelModel,
							        TFFunnelForMaskedLM,
							        TFFunnelForPreTraining,
							        TFFunnelForQuestionAnswering,
							        TFFunnelForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							UpperCAmelCase_						:		Optional[Any]								=	(
							    {
							        '''feature-extraction''': (TFFunnelBaseModel, TFFunnelModel),
							        '''fill-mask''': TFFunnelForMaskedLM,
							        '''question-answering''': TFFunnelForQuestionAnswering,
							        '''text-classification''': TFFunnelForSequenceClassification,
							        '''token-classification''': TFFunnelForTokenClassification,
							        '''zero-shot''': TFFunnelForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							UpperCAmelCase_						:		Optional[Any]								=	False
							UpperCAmelCase_						:		Optional[Any]								=	False




							def       snake_case			(					self						)		->       str:
														A      :					Dict      		=					TFFunnelModelTester(self						)
														A      :					int      		=					ConfigTester(self    ,						config_class=__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Tuple:
														self.config_tester.run_common_tests()




							def       snake_case			(					self						)		->       List[str]:
														A      :					Tuple      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Union[str, Any]:
														A      :					Optional[Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_pretraining(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Optional[Any]:
														A      :					List[Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       Optional[Any]:
														A      :					List[Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase						)






							def       snake_case			(					self						)		->       Optional[int]:
														A      :					List[str]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase						)







@require_tf
class 					__lowercase  (						_SCREAMING_SNAKE_CASE		,       unittest.TestCase				):





							"""simple docstring"""

							UpperCAmelCase_						:		Any								=	(
							    (TFFunnelBaseModel, TFFunnelForMultipleChoice, TFFunnelForSequenceClassification) if is_tf_available() else ()
							)
							UpperCAmelCase_						:		str								=	False
							UpperCAmelCase_						:		Union[str, Any]								=	False




							def       snake_case			(					self						)		->       int:
														A      :					Optional[int]      		=					TFFunnelModelTester(self    ,						base=__UpperCAmelCase						)
														A      :					Optional[Any]      		=					ConfigTester(self    ,						config_class=__UpperCAmelCase						)




							def       snake_case			(					self						)		->       str:
														self.config_tester.run_common_tests()




							def       snake_case			(					self						)		->       Tuple:
														A      :					Optional[Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_base_model(*__UpperCAmelCase						)




							def       snake_case			(					self						)		->       int:
														A      :					Union[str, Any]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase						)






							def       snake_case			(					self						)		->       Tuple:
														A      :					List[str]      		=					self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_multiple_choice(*__UpperCAmelCase						)






