
"""simple docstring"""




import collections
import types

import numpy as np

from ..utils import (
    add_end_docstrings,
    is_tensorflow_probability_available,
    is_tf_available,
    is_torch_available,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, Dataset, Pipeline, PipelineException


if is_torch_available():
					import torch

					from ..models.auto.modeling_auto import (
					    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
					    MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING,
					)

if is_tf_available() and is_tensorflow_probability_available():
					import tensorflow as tf
					import tensorflow_probability as tfp

					from ..models.auto.modeling_tf_auto import (
					    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
					    TF_MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING,
					)
class SCREAMING_SNAKE_CASE       (      lowerCAmelCase					):






	'''simple docstring'''







	def __call__(  self :Optional[int]       ,    __magic_name__ :Optional[int]=None       ,    __magic_name__ :Union[str, Any]=None       ,    **__magic_name__ :List[str]			)			->   str:

			'''simple docstring'''






			requires_backends(self       ,    '''pandas'''			)
			import pandas as pd

			if table is None:
					raise ValueError('''Keyword argument `table` cannot be None.'''			)
			elif query is None:
					if isinstance(__magic_name__       ,    __magic_name__			) and table.get('''query'''			) is not None and table.get('''table'''			) is not None:
							a__   		=     [table]
					elif isinstance(__magic_name__       ,    __magic_name__			) and len(__magic_name__			) > 0:
							if not all(isinstance(__magic_name__       ,    __magic_name__			) for d in table			):
									raise ValueError(
									    F"Keyword argument `table` should be a list of dict, but is {(type(__magic_name__			) for d in table)}"			)

							if table[0].get('''query'''			) is not None and table[0].get('''table'''			) is not None:
									a__   		=     table
							else:
									raise ValueError(
									    '''If keyword argument `table` is a list of dictionaries, each dictionary should have a `table`'''
									    F" and `query` key, but only dictionary has keys {table[0].keys()} `table` and `query` keys."			)
					elif Dataset is not None and isinstance(__magic_name__       ,    __magic_name__			) or isinstance(__magic_name__       ,    types.GeneratorType			):
							return table
					else:
							raise ValueError(
							    '''Invalid input. Keyword argument `table` should be either of type `dict` or `list`, but '''
							    F"is {type(__magic_name__			)})"			)
			else:
					a__   		=     [{'''table''': table, '''query''': query}]

			for tqa_pipeline_input in tqa_pipeline_inputs:
					if not isinstance(tqa_pipeline_input['''table''']       ,    pd.DataFrame			):
							if tqa_pipeline_input["table"] is None:
									raise ValueError('''Table cannot be None.'''			)

							a__   		=     pd.DataFrame(tqa_pipeline_input['''table''']			)

			return tqa_pipeline_inputs

@add_end_docstrings(lowerCAmelCase					)
class SCREAMING_SNAKE_CASE       (      lowerCAmelCase					):






	'''simple docstring'''







	snake_case__							:		Optional[int]         =		'table,query'


	def __init__(  self :Tuple       ,    __magic_name__ :List[str]=TableQuestionAnsweringArgumentHandler()       ,    *__magic_name__ :Union[str, Any]       ,    **__magic_name__ :List[str]			)			->   Any:

			'''simple docstring'''






			super().__init__(*__magic_name__       ,    **__magic_name__			)
			a__   		=     args_parser

			self.check_model_type(
			    dict(
			        TF_MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.items()
			        + TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.items()			)
			    if self.framework == '''tf'''
			    else dict(
			        MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.items() + MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.items()			)			)

			a__   		=     bool(getattr(self.model.config       ,    '''aggregation_labels'''       ,    __magic_name__			)			) and bool(
			    getattr(self.model.config       ,    '''num_aggregation_labels'''       ,    __magic_name__			)			)
			a__   		=     '''tapas''' if hasattr(self.model.config       ,    '''aggregation_labels'''			) else None


	def    _UpperCamelCase							(  self :Dict       ,    **__magic_name__ :Tuple			)			->   int:

			'''simple docstring'''






			return self.model(**__magic_name__			)


	def    _UpperCamelCase							(  self :List[Any]       ,    **__magic_name__ :int			)			->   Optional[Any]:

			'''simple docstring'''






			if self.framework == "pt":
					a__   		=     []
					a__   		=     []
					a__   		=     None
					a__   		=     inputs['''input_ids'''].shape[0]

					a__   		=     inputs['''input_ids'''].to(self.device			)
					a__   		=     inputs['''attention_mask'''].to(self.device			)
					a__   		=     inputs['''token_type_ids'''].to(self.device			)
					a__   		=     None

					for index in range(__magic_name__			):
							# If sequences have already been processed, the token type IDs will be created according to the previous
							# answer.
							if prev_answers is not None:
									a__   		=     token_type_ids_example[:, 3]  # shape (seq_len,)
									a__   		=     np.zeros_like(prev_labels_example.cpu().numpy()			)  # shape (seq_len,)

									a__   		=     token_type_ids[index]  # shape (seq_len, 7)
									for i in range(model_labels.shape[0]			):
											a__   		=     token_type_ids_example[:, 0].tolist()[i]
											a__   		=     token_type_ids_example[:, 1].tolist()[i] - 1
											a__   		=     token_type_ids_example[:, 2].tolist()[i] - 1

											if row_id >= 0 and col_id >= 0 and segment_id == 1:
													a__   		=     int(prev_answers[(col_id, row_id)]			)

									a__   		=     torch.from_numpy(__magic_name__			).type(torch.long			).to(self.device			)

							a__   		=     input_ids[index]
							a__   		=     attention_mask[index]  # shape (seq_len,)
							a__   		=     token_type_ids[index]  # shape (seq_len, 7)
							a__   		=     self.model(
							    input_ids=input_ids_example.unsqueeze(0			)       ,    attention_mask=attention_mask_example.unsqueeze(0			)       ,    token_type_ids=token_type_ids_example.unsqueeze(0			)       ,    )
							a__   		=     outputs.logits

							if self.aggregate:
									all_aggregations.append(outputs.logits_aggregation			)

							all_logits.append(__magic_name__			)

							a__   		=     torch.distributions.Bernoulli(logits=__magic_name__			)
							a__   		=     dist_per_token.probs * attention_mask_example.type(torch.floataa			).to(
							    dist_per_token.probs.device			)

							a__   		=     collections.defaultdict(__magic_name__			)
							for i, p in enumerate(probabilities.squeeze().tolist()			):
									a__   		=     token_type_ids_example[:, 0].tolist()[i]
									a__   		=     token_type_ids_example[:, 1].tolist()[i] - 1
									a__   		=     token_type_ids_example[:, 2].tolist()[i] - 1
									if col >= 0 and row >= 0 and segment_id == 1:
											coords_to_probs[(col, row)].append(__magic_name__			)

							a__   		=     {key: np.array(coords_to_probs[key]			).mean() > 0.5 for key in coords_to_probs}

					a__   		=     torch.cat(tuple(__magic_name__			)       ,    0			)

					return (logits_batch,) if not self.aggregate else (logits_batch, torch.cat(tuple(__magic_name__			)       ,    0			))
			else:
					a__   		=     []
					a__   		=     []
					a__   		=     None
					a__   		=     inputs['''input_ids'''].shape[0]

					a__   		=     inputs['''input_ids''']
					a__   		=     inputs['''attention_mask''']
					a__   		=     inputs['''token_type_ids'''].numpy()
					a__   		=     None

					for index in range(__magic_name__			):
							# If sequences have already been processed, the token type IDs will be created according to the previous
							# answer.
							if prev_answers is not None:
									a__   		=     token_type_ids_example[:, 3]  # shape (seq_len,)
									a__   		=     np.zeros_like(__magic_name__       ,    dtype=np.intaa			)  # shape (seq_len,)

									a__   		=     token_type_ids[index]  # shape (seq_len, 7)
									for i in range(model_labels.shape[0]			):
											a__   		=     token_type_ids_example[:, 0].tolist()[i]
											a__   		=     token_type_ids_example[:, 1].tolist()[i] - 1
											a__   		=     token_type_ids_example[:, 2].tolist()[i] - 1

											if row_id >= 0 and col_id >= 0 and segment_id == 1:
													a__   		=     int(prev_answers[(col_id, row_id)]			)

									a__   		=     model_labels

							a__   		=     input_ids[index]
							a__   		=     attention_mask[index]  # shape (seq_len,)
							a__   		=     token_type_ids[index]  # shape (seq_len, 7)
							a__   		=     self.model(
							    input_ids=np.expand_dims(__magic_name__       ,    axis=0			)       ,    attention_mask=np.expand_dims(__magic_name__       ,    axis=0			)       ,    token_type_ids=np.expand_dims(__magic_name__       ,    axis=0			)       ,    )
							a__   		=     outputs.logits

							if self.aggregate:
									all_aggregations.append(outputs.logits_aggregation			)

							all_logits.append(__magic_name__			)

							a__   		=     tfp.distributions.Bernoulli(logits=__magic_name__			)
							a__   		=     dist_per_token.probs_parameter() * tf.cast(__magic_name__       ,    tf.floataa			)

							a__   		=     collections.defaultdict(__magic_name__			)
							a__   		=     token_type_ids_example
							for i, p in enumerate(tf.squeeze(__magic_name__			).numpy().tolist()			):
									a__   		=     token_type_ids_example[:, 0].tolist()[i]
									a__   		=     token_type_ids_example[:, 1].tolist()[i] - 1
									a__   		=     token_type_ids_example[:, 2].tolist()[i] - 1
									if col >= 0 and row >= 0 and segment_id == 1:
											coords_to_probs[(col, row)].append(__magic_name__			)

							a__   		=     {key: np.array(coords_to_probs[key]			).mean() > 0.5 for key in coords_to_probs}

					a__   		=     tf.concat(tuple(__magic_name__			)       ,    0			)

					return (logits_batch,) if not self.aggregate else (logits_batch, tf.concat(tuple(__magic_name__			)       ,    0			))


	def __call__(  self :List[Any]       ,    *__magic_name__ :List[str]       ,    **__magic_name__ :int			)			->   str:

			'''simple docstring'''






			a__   		=     self._args_parser(*__magic_name__       ,    **__magic_name__			)

			a__   		=     super().__call__(__magic_name__       ,    **__magic_name__			)
			if len(__magic_name__			) == 1:
					return results[0]
			return results


	def    _UpperCamelCase							(  self :List[Any]       ,    __magic_name__ :str=None       ,    __magic_name__ :List[str]=None       ,    __magic_name__ :str=None       ,    **__magic_name__ :Dict			)			->   Any:

			'''simple docstring'''






			a__   		=     {}
			if padding is not None:
					a__   		=     padding
			if truncation is not None:
					a__   		=     truncation

			a__   		=     {}
			if sequential is not None:
					a__   		=     sequential
			return preprocess_params, forward_params, {}


	def    _UpperCamelCase							(  self :Dict       ,    __magic_name__ :Tuple       ,    __magic_name__ :Optional[Any]=None       ,    __magic_name__ :int=True       ,    __magic_name__ :int=None			)			->   Optional[int]:

			'''simple docstring'''






			if truncation is None:
					if self.type == "tapas":
							a__   		=     '''drop_rows_to_fit'''
					else:
							a__   		=     '''do_not_truncate'''

			a__		,	a__   		=     pipeline_input['''table'''], pipeline_input['''query''']
			if table.empty:
					raise ValueError('''table is empty'''			)
			if query is None or query == "":
					raise ValueError('''query is empty'''			)
			a__   		=     self.tokenizer(__magic_name__       ,    __magic_name__       ,    return_tensors=self.framework       ,    truncation=__magic_name__       ,    padding=__magic_name__			)
			a__   		=     table
			return inputs


	def    _UpperCamelCase							(  self :Optional[Any]       ,    __magic_name__ :Tuple       ,    __magic_name__ :List[str]=False			)			->   List[str]:

			'''simple docstring'''






			a__   		=     model_inputs.pop('''table'''			)

			if self.type == "tapas":
					if sequential:
							a__   		=     self.sequential_inference(**__magic_name__			)
					else:
							a__   		=     self.batch_inference(**__magic_name__			)
			else:
					a__   		=     self.model.generate(**__magic_name__			)
			a__   		=     {'''model_inputs''': model_inputs, '''table''': table, '''outputs''': outputs}
			return model_outputs






	def    _UpperCamelCase							(  self :Dict       ,    __magic_name__ :Dict			)			->   Optional[int]:

			'''simple docstring'''






			a__   		=     model_outputs['''model_inputs''']
			a__   		=     model_outputs['''table''']
			a__   		=     model_outputs['''outputs''']
			if self.type == "tapas":
					if self.aggregate:
							a__		,	a__   		=     outputs[:2]
							a__   		=     self.tokenizer.convert_logits_to_predictions(__magic_name__       ,    __magic_name__       ,    __magic_name__			)
							a__		,	a__   		=     predictions
							a__   		=     {i: self.model.config.aggregation_labels[pred] for i, pred in enumerate(__magic_name__			)}

							a__   		=     self.model.config.no_aggregation_label_index
							a__   		=     {
							    i: aggregators[i] + ''' > ''' for i, pred in enumerate(__magic_name__			) if pred != no_agg_label_index
							}
					else:
							a__   		=     outputs[0]
							a__   		=     self.tokenizer.convert_logits_to_predictions(__magic_name__       ,    __magic_name__			)
							a__   		=     predictions[0]
							a__   		=     {}
							a__   		=     {}
					a__   		=     []
					for index, coordinates in enumerate(__magic_name__			):
							a__   		=     [table.iat[coordinate] for coordinate in coordinates]
							a__   		=     aggregators.get(__magic_name__       ,    ''''''			)
							a__   		=     aggregators_prefix.get(__magic_name__       ,    ''''''			)
							a__   		=     {
							    '''answer''': aggregator_prefix + ''', '''.join(__magic_name__			),
							    '''coordinates''': coordinates,
							    '''cells''': [table.iat[coordinate] for coordinate in coordinates],
							}
							if aggregator:
									a__   		=     aggregator

							answers.append(__magic_name__			)
					if len(__magic_name__			) == 0:
							raise PipelineException('''Empty answer'''			)
			else:
					a__   		=     [{'''answer''': answer} for answer in self.tokenizer.batch_decode(__magic_name__       ,    skip_special_tokens=__magic_name__			)]

			return answers if len(__magic_name__			) > 1 else answers[0]






"""simple docstring"""




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import MobileNetVaImageProcessor
class SCREAMING_SNAKE_CASE       (      unittest.TestCase					):






	'''simple docstring'''







	def __init__(  self :Tuple       ,    __magic_name__ :str       ,    __magic_name__ :Optional[Any]=7       ,    __magic_name__ :int=3       ,    __magic_name__ :Union[str, Any]=18       ,    __magic_name__ :Union[str, Any]=30       ,    __magic_name__ :str=400       ,    __magic_name__ :str=True       ,    __magic_name__ :Any=None       ,    __magic_name__ :Tuple=True       ,    __magic_name__ :List[str]=None       ,    )			->   Optional[int]:

			'''simple docstring'''






			a__   		=     size if size is not None else {'''shortest_edge''': 20}
			a__   		=     crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
			a__   		=     parent
			a__   		=     batch_size
			a__   		=     num_channels
			a__   		=     image_size
			a__   		=     min_resolution
			a__   		=     max_resolution
			a__   		=     do_resize
			a__   		=     size
			a__   		=     do_center_crop
			a__   		=     crop_size






	def    _UpperCamelCase							(  self :Tuple			)			->   Any:

			'''simple docstring'''






			return {
			    "do_resize": self.do_resize,
			    "size": self.size,
			    "do_center_crop": self.do_center_crop,
			    "crop_size": self.crop_size,
			}

@require_torch
@require_vision
class SCREAMING_SNAKE_CASE       (      lowerCAmelCase					,   unittest.TestCase					):






	'''simple docstring'''







	snake_case__							:		str         =		MobileNetVaImageProcessor if is_vision_available() else None


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

			'''simple docstring'''






			a__   		=     MobileNetVaImageProcessingTester(self			)


	@property
	def    _UpperCamelCase							(  self :List[str]			)			->   Optional[Any]:

			'''simple docstring'''






			return self.image_processor_tester.prepare_image_processor_dict()


	def    _UpperCamelCase							(  self :Tuple			)			->   Tuple:

			'''simple docstring'''






			a__   		=     self.image_processing_class(**self.image_processor_dict			)
			self.assertTrue(hasattr(__magic_name__       ,    '''do_resize'''			)			)
			self.assertTrue(hasattr(__magic_name__       ,    '''size'''			)			)
			self.assertTrue(hasattr(__magic_name__       ,    '''do_center_crop'''			)			)
			self.assertTrue(hasattr(__magic_name__       ,    '''center_crop'''			)			)


	def    _UpperCamelCase							(  self :List[Any]			)			->   int:

			'''simple docstring'''






			a__   		=     self.image_processing_class.from_dict(self.image_processor_dict			)
			self.assertEqual(image_processor.size       ,    {'''shortest_edge''': 20}			)
			self.assertEqual(image_processor.crop_size       ,    {'''height''': 18, '''width''': 18}			)

			a__   		=     self.image_processing_class.from_dict(self.image_processor_dict       ,    size=42       ,    crop_size=84			)
			self.assertEqual(image_processor.size       ,    {'''shortest_edge''': 42}			)
			self.assertEqual(image_processor.crop_size       ,    {'''height''': 84, '''width''': 84}			)


	def    _UpperCamelCase							(  self :Optional[int]			)			->   Any:

			'''simple docstring'''






			pass


	def    _UpperCamelCase							(  self :Dict			)			->   Tuple:

			'''simple docstring'''






			a__   		=     self.image_processing_class(**self.image_processor_dict			)
			# create random PIL images
			a__   		=     prepare_image_inputs(self.image_processor_tester       ,    equal_resolution=__magic_name__			)
			for image in image_inputs:
					self.assertIsInstance(__magic_name__       ,    Image.Image			)

			# Test not batched input
			a__   		=     image_processing(image_inputs[0]       ,    return_tensors='''pt'''			).pixel_values
			self.assertEqual(
			    encoded_images.shape       ,    (
			        1,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size['''height'''],
			        self.image_processor_tester.crop_size['''width'''],
			    )       ,    )

			# Test batched
			a__   		=     image_processing(__magic_name__       ,    return_tensors='''pt'''			).pixel_values
			self.assertEqual(
			    encoded_images.shape       ,    (
			        self.image_processor_tester.batch_size,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size['''height'''],
			        self.image_processor_tester.crop_size['''width'''],
			    )       ,    )


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

			'''simple docstring'''






			a__   		=     self.image_processing_class(**self.image_processor_dict			)
			# create random numpy tensors
			a__   		=     prepare_image_inputs(self.image_processor_tester       ,    equal_resolution=__magic_name__       ,    numpify=__magic_name__			)
			for image in image_inputs:
					self.assertIsInstance(__magic_name__       ,    np.ndarray			)

			# Test not batched input
			a__   		=     image_processing(image_inputs[0]       ,    return_tensors='''pt'''			).pixel_values
			self.assertEqual(
			    encoded_images.shape       ,    (
			        1,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size['''height'''],
			        self.image_processor_tester.crop_size['''width'''],
			    )       ,    )

			# Test batched
			a__   		=     image_processing(__magic_name__       ,    return_tensors='''pt'''			).pixel_values
			self.assertEqual(
			    encoded_images.shape       ,    (
			        self.image_processor_tester.batch_size,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size['''height'''],
			        self.image_processor_tester.crop_size['''width'''],
			    )       ,    )






	def    _UpperCamelCase							(  self :List[Any]			)			->   str:

			'''simple docstring'''






			a__   		=     self.image_processing_class(**self.image_processor_dict			)
			# create random PyTorch tensors
			a__   		=     prepare_image_inputs(self.image_processor_tester       ,    equal_resolution=__magic_name__       ,    torchify=__magic_name__			)
			for image in image_inputs:
					self.assertIsInstance(__magic_name__       ,    torch.Tensor			)

			# Test not batched input
			a__   		=     image_processing(image_inputs[0]       ,    return_tensors='''pt'''			).pixel_values
			self.assertEqual(
			    encoded_images.shape       ,    (
			        1,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size['''height'''],
			        self.image_processor_tester.crop_size['''width'''],
			    )       ,    )

			# Test batched
			a__   		=     image_processing(__magic_name__       ,    return_tensors='''pt'''			).pixel_values
			self.assertEqual(
			    encoded_images.shape       ,    (
			        self.image_processor_tester.batch_size,
			        self.image_processor_tester.num_channels,
			        self.image_processor_tester.crop_size['''height'''],
			        self.image_processor_tester.crop_size['''width'''],
			    )       ,    )




