






'''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 						_UpperCamelCase  (							__UpperCAmelCase   ):


			'''simple docstring'''

			def __call__(							self					:			Optional[Any]							,					lowerCAmelCase__					:			int=None							,					lowerCAmelCase__					:			Optional[Any]=None							,					**lowerCAmelCase__					:			List[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(lowerCamelCase__							,					lowerCamelCase__						) and table.get("""query"""						) is not None and table.get("""table"""						) is not None:
									__SCREAMING_SNAKE_CASE	:		List[str]   	=		[table]
							elif isinstance(lowerCamelCase__							,					lowerCamelCase__						) and len(lowerCamelCase__						) > 0:
									if not all(isinstance(lowerCamelCase__							,					lowerCamelCase__						) for d in table						):
											raise ValueError(
											    F"Keyword argument `table` should be a list of dict, but is {(type(lowerCamelCase__						) for d in table)}"						)

									if table[0].get("""query"""						) is not None and table[0].get("""table"""						) is not None:
											__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		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(lowerCamelCase__							,					lowerCamelCase__						) or isinstance(lowerCamelCase__							,					types.GeneratorType						):
									return table
							else:
									raise ValueError(
									    """Invalid input. Keyword argument `table` should be either of type `dict` or `list`, but """
									    F"is {type(lowerCamelCase__						)})"						)
					else:
							__SCREAMING_SNAKE_CASE	:		Dict   	=		[{"""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."""						)

									__SCREAMING_SNAKE_CASE	:		List[str]   	=		pd.DataFrame(tqa_pipeline_input["""table"""]						)

					return tqa_pipeline_inputs






@add_end_docstrings(__UpperCAmelCase   )
class 						_UpperCamelCase  (							__UpperCAmelCase   ):


			'''simple docstring'''

			_A				:		List[str]         =		"""table,query"""

			def __init__(							self					:			int							,					lowerCAmelCase__					:			Tuple=TableQuestionAnsweringArgumentHandler()							,					*lowerCAmelCase__					:			int							,					**lowerCAmelCase__					:			Any						):





					"""simple docstring"""






					super().__init__(*lowerCamelCase__							,					**lowerCamelCase__						)
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		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()						)						)

					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		bool(getattr(self.model.config							,					"""aggregation_labels"""							,					lowerCamelCase__						)						) and bool(
					    getattr(self.model.config							,					"""num_aggregation_labels"""							,					lowerCamelCase__						)						)
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		"""tapas""" if hasattr(self.model.config							,					"""aggregation_labels"""						) else None

			def UpperCamelCase__ (							self					:			Union[str, Any]							,					**lowerCAmelCase__					:			Dict						):





					"""simple docstring"""






					return self.model(**lowerCamelCase__						)

			def UpperCamelCase__ (							self					:			Any							,					**lowerCAmelCase__					:			Optional[Any]						):





					"""simple docstring"""






					if self.framework == "pt":
							__SCREAMING_SNAKE_CASE	:		List[Any]   	=		[]
							__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		[]
							__SCREAMING_SNAKE_CASE	:		str   	=		None
							__SCREAMING_SNAKE_CASE	:		Dict   	=		inputs["""input_ids"""].shape[0]

							__SCREAMING_SNAKE_CASE	:		str   	=		inputs["""input_ids"""].to(self.device						)
							__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		inputs["""attention_mask"""].to(self.device						)
							__SCREAMING_SNAKE_CASE	:		List[str]   	=		inputs["""token_type_ids"""].to(self.device						)
							__SCREAMING_SNAKE_CASE	:		int   	=		None

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

											__SCREAMING_SNAKE_CASE	:		List[Any]   	=		token_type_ids[index]  # shape (seq_len, 7)
											for i in range(model_labels.shape[0]						):
													__SCREAMING_SNAKE_CASE	:		Dict   	=		token_type_ids_example[:, 0].tolist()[i]
													__SCREAMING_SNAKE_CASE	:		int   	=		token_type_ids_example[:, 1].tolist()[i] - 1
													__SCREAMING_SNAKE_CASE	:		Tuple   	=		token_type_ids_example[:, 2].tolist()[i] - 1

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

											__SCREAMING_SNAKE_CASE	:		int   	=		torch.from_numpy(lowerCamelCase__						).type(torch.long						).to(self.device						)

									__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		input_ids[index]
									__SCREAMING_SNAKE_CASE	:		Dict   	=		attention_mask[index]  # shape (seq_len,)
									__SCREAMING_SNAKE_CASE	:		Dict   	=		token_type_ids[index]  # shape (seq_len, 7)
									__SCREAMING_SNAKE_CASE	:		str   	=		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						)							,					)
									__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		outputs.logits

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

									all_logits.append(lowerCamelCase__						)

									__SCREAMING_SNAKE_CASE	:		List[Any]   	=		torch.distributions.Bernoulli(logits=lowerCamelCase__						)
									__SCREAMING_SNAKE_CASE	:		List[Any]   	=		dist_per_token.probs * attention_mask_example.type(torch.floataa						).to(
									    dist_per_token.probs.device						)

									__SCREAMING_SNAKE_CASE	:		Tuple   	=		collections.defaultdict(lowerCamelCase__						)
									for i, p in enumerate(probabilities.squeeze().tolist()						):
											__SCREAMING_SNAKE_CASE	:		Tuple   	=		token_type_ids_example[:, 0].tolist()[i]
											__SCREAMING_SNAKE_CASE	:		int   	=		token_type_ids_example[:, 1].tolist()[i] - 1
											__SCREAMING_SNAKE_CASE	:		List[str]   	=		token_type_ids_example[:, 2].tolist()[i] - 1
											if col >= 0 and row >= 0 and segment_id == 1:
													coords_to_probs[(col, row)].append(lowerCamelCase__						)

									__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		{key: np.array(coords_to_probs[key]						).mean() > 0.5 for key in coords_to_probs}

							__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		torch.cat(tuple(lowerCamelCase__						)							,					0						)

							return (logits_batch,) if not self.aggregate else (logits_batch, torch.cat(tuple(lowerCamelCase__						)							,					0						))
					else:
							__SCREAMING_SNAKE_CASE	:		str   	=		[]
							__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		[]
							__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		None
							__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		inputs["""input_ids"""].shape[0]

							__SCREAMING_SNAKE_CASE	:		Tuple   	=		inputs["""input_ids"""]
							__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		inputs["""attention_mask"""]
							__SCREAMING_SNAKE_CASE	:		str   	=		inputs["""token_type_ids"""].numpy()
							__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		None

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

											__SCREAMING_SNAKE_CASE	:		Any   	=		token_type_ids[index]  # shape (seq_len, 7)
											for i in range(model_labels.shape[0]						):
													__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		token_type_ids_example[:, 0].tolist()[i]
													__SCREAMING_SNAKE_CASE	:		List[Any]   	=		token_type_ids_example[:, 1].tolist()[i] - 1
													__SCREAMING_SNAKE_CASE	:		List[str]   	=		token_type_ids_example[:, 2].tolist()[i] - 1

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

											__SCREAMING_SNAKE_CASE	:		List[str]   	=		model_labels

									__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		input_ids[index]
									__SCREAMING_SNAKE_CASE	:		Any   	=		attention_mask[index]  # shape (seq_len,)
									__SCREAMING_SNAKE_CASE	:		int   	=		token_type_ids[index]  # shape (seq_len, 7)
									__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		self.model(
									    input_ids=np.expand_dims(lowerCamelCase__							,					axis=0						)							,					attention_mask=np.expand_dims(lowerCamelCase__							,					axis=0						)							,					token_type_ids=np.expand_dims(lowerCamelCase__							,					axis=0						)							,					)
									__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		outputs.logits

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

									all_logits.append(lowerCamelCase__						)

									__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		tfp.distributions.Bernoulli(logits=lowerCamelCase__						)
									__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		dist_per_token.probs_parameter() * tf.cast(lowerCamelCase__							,					tf.floataa						)

									__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		collections.defaultdict(lowerCamelCase__						)
									__SCREAMING_SNAKE_CASE	:		int   	=		token_type_ids_example
									for i, p in enumerate(tf.squeeze(lowerCamelCase__						).numpy().tolist()						):
											__SCREAMING_SNAKE_CASE	:		Dict   	=		token_type_ids_example[:, 0].tolist()[i]
											__SCREAMING_SNAKE_CASE	:		List[Any]   	=		token_type_ids_example[:, 1].tolist()[i] - 1
											__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		token_type_ids_example[:, 2].tolist()[i] - 1
											if col >= 0 and row >= 0 and segment_id == 1:
													coords_to_probs[(col, row)].append(lowerCamelCase__						)

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

							__SCREAMING_SNAKE_CASE	:		Tuple   	=		tf.concat(tuple(lowerCamelCase__						)							,					0						)

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

			def __call__(							self					:			List[str]							,					*lowerCAmelCase__					:			str							,					**lowerCAmelCase__					:			Optional[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		List[str]   	=		self._args_parser(*lowerCamelCase__							,					**lowerCamelCase__						)

					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		super().__call__(lowerCamelCase__							,					**lowerCamelCase__						)
					if len(lowerCamelCase__						) == 1:
							return results[0]
					return results

			def UpperCamelCase__ (							self					:			Union[str, Any]							,					lowerCAmelCase__					:			Optional[int]=None							,					lowerCAmelCase__					:			List[Any]=None							,					lowerCAmelCase__					:			Dict=None							,					**lowerCAmelCase__					:			int						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		{}
					if padding is not None:
							__SCREAMING_SNAKE_CASE	:		Tuple   	=		padding
					if truncation is not None:
							__SCREAMING_SNAKE_CASE	:		Dict   	=		truncation

					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		{}
					if sequential is not None:
							__SCREAMING_SNAKE_CASE	:		int   	=		sequential
					return preprocess_params, forward_params, {}

			def UpperCamelCase__ (							self					:			Tuple							,					lowerCAmelCase__					:			str							,					lowerCAmelCase__					:			List[str]=None							,					lowerCAmelCase__					:			Any=True							,					lowerCAmelCase__					:			Tuple=None						):





					"""simple docstring"""






					if truncation is None:
							if self.type == "tapas":
									__SCREAMING_SNAKE_CASE	:		Dict   	=		"""drop_rows_to_fit"""
							else:
									__SCREAMING_SNAKE_CASE	:		int   	=		"""do_not_truncate"""

					__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		Tuple   	=		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"""						)
					__SCREAMING_SNAKE_CASE	:		Tuple   	=		self.tokenizer(lowerCamelCase__							,					lowerCamelCase__							,					return_tensors=self.framework							,					truncation=lowerCamelCase__							,					padding=lowerCamelCase__						)
					__SCREAMING_SNAKE_CASE	:		List[Any]   	=		table
					return inputs

			def UpperCamelCase__ (							self					:			Dict							,					lowerCAmelCase__					:			Optional[int]							,					lowerCAmelCase__					:			List[str]=False						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		str   	=		model_inputs.pop("""table"""						)

					if self.type == "tapas":
							if sequential:
									__SCREAMING_SNAKE_CASE	:		Tuple   	=		self.sequential_inference(**lowerCamelCase__						)
							else:
									__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		self.batch_inference(**lowerCamelCase__						)
					else:
							__SCREAMING_SNAKE_CASE	:		List[str]   	=		self.model.generate(**lowerCamelCase__						)
					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		{"""model_inputs""": model_inputs, """table""": table, """outputs""": outputs}
					return model_outputs



			def UpperCamelCase__ (							self					:			List[Any]							,					lowerCAmelCase__					:			Any						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE	:		List[str]   	=		model_outputs["""model_inputs"""]
					__SCREAMING_SNAKE_CASE	:		Optional[Any]   	=		model_outputs["""table"""]
					__SCREAMING_SNAKE_CASE	:		List[str]   	=		model_outputs["""outputs"""]
					if self.type == "tapas":
							if self.aggregate:
									__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		Any   	=		outputs[:2]
									__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		self.tokenizer.convert_logits_to_predictions(lowerCamelCase__							,					lowerCamelCase__							,					lowerCamelCase__						)
									__SCREAMING_SNAKE_CASE	,						__SCREAMING_SNAKE_CASE	:		Dict   	=		predictions
									__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		{i: self.model.config.aggregation_labels[pred] for i, pred in enumerate(lowerCamelCase__						)}

									__SCREAMING_SNAKE_CASE	:		Dict   	=		self.model.config.no_aggregation_label_index
									__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		{
									    i: aggregators[i] + """ > """ for i, pred in enumerate(lowerCamelCase__						) if pred != no_agg_label_index
									}
							else:
									__SCREAMING_SNAKE_CASE	:		Any   	=		outputs[0]
									__SCREAMING_SNAKE_CASE	:		int   	=		self.tokenizer.convert_logits_to_predictions(lowerCamelCase__							,					lowerCamelCase__						)
									__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		predictions[0]
									__SCREAMING_SNAKE_CASE	:		Optional[int]   	=		{}
									__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		{}
							__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		[]
							for index, coordinates in enumerate(lowerCamelCase__						):
									__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		[table.iat[coordinate] for coordinate in coordinates]
									__SCREAMING_SNAKE_CASE	:		str   	=		aggregators.get(lowerCamelCase__							,					""""""						)
									__SCREAMING_SNAKE_CASE	:		Dict   	=		aggregators_prefix.get(lowerCamelCase__							,					""""""						)
									__SCREAMING_SNAKE_CASE	:		Union[str, Any]   	=		{
									    """answer""": aggregator_prefix + """, """.join(lowerCamelCase__						),
									    """coordinates""": coordinates,
									    """cells""": [table.iat[coordinate] for coordinate in coordinates],
									}
									if aggregator:
											__SCREAMING_SNAKE_CASE	:		List[Any]   	=		aggregator

									answers.append(lowerCamelCase__						)
							if len(lowerCamelCase__						) == 0:
									raise PipelineException("""Empty answer"""						)
					else:
							__SCREAMING_SNAKE_CASE	:		List[str]   	=		[{"""answer""": answer} for answer in self.tokenizer.batch_decode(lowerCamelCase__							,					skip_special_tokens=lowerCamelCase__						)]

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




'''simple docstring'''




from __future__ import annotations


def     lowerCAmelCase_       (     _lowerCamelCase:       list[int | float] ):
		if len(_lowerCamelCase ) == 0:
				raise ValueError("""find_min() arg is an empty sequence""" )
		__SCREAMING_SNAKE_CASE	:		List[str]   	=		nums[0]
		for num in nums:
				__SCREAMING_SNAKE_CASE	:		int   	=		min(_lowerCamelCase	,				_lowerCamelCase )
		return min_num


if __name__ == "__main__":
						import doctest

						doctest.testmod(verbose=True)