






"""simple docstring"""

import numpy as np

def 	__lowerCamelCase			(						__UpperCamelCase )		->					List[str]:

		"""simple docstring"""







		lowerCAmelCase_     :							List[Any]             =    np.exp(__UpperCamelCase )

		# Add up the all the exponentials
		lowerCAmelCase_     :							List[Any]             =    np.sum(__UpperCamelCase )

		# Divide every exponent by the sum of all exponents
		lowerCAmelCase_     :							Dict             =    exponent_vector / sum_of_exponents

		return softmax_vector


if __name__ == "__main__":
					print(softmax((0,)))








"""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 	__lowerCamelCase       (				A__    ):





		'''simple docstring'''






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

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

								lowerCAmelCase_     :							Optional[int]             =    pd.DataFrame(tqa_pipeline_input["table"]			)

				return tqa_pipeline_inputs







@add_end_docstrings(A__    )
class 	__lowerCamelCase       (				A__    ):





		'''simple docstring'''






		a_			:       str      =							"""table,query"""





		def __init__(     self							:			str		,    a_							:			Optional[Any]=TableQuestionAnsweringArgumentHandler()		,    *a_							:			List[Any]		,    **a_							:			Union[str, Any]			):
				super().__init__(*a_		,    **a_			)
				lowerCAmelCase_     :							Dict             =    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()			)			)

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





		def      lowerCamelCase (     self							:			Optional[Any]		,    **a_							:			Union[str, Any]			):
				return self.model(**a_			)





		def      lowerCamelCase (     self							:			Optional[Any]		,    **a_							:			Union[str, Any]			):
				if self.framework == "pt":
						lowerCAmelCase_     :							Optional[int]             =    []
						lowerCAmelCase_     :							str             =    []
						lowerCAmelCase_     :							str             =    None
						lowerCAmelCase_     :							Union[str, Any]             =    inputs["input_ids"].shape[0]

						lowerCAmelCase_     :							Dict             =    inputs["input_ids"].to(self.device			)
						lowerCAmelCase_     :							Dict             =    inputs["attention_mask"].to(self.device			)
						lowerCAmelCase_     :							List[str]             =    inputs["token_type_ids"].to(self.device			)
						lowerCAmelCase_     :							Optional[Any]             =    None

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

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

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

										lowerCAmelCase_     :							List[Any]             =    torch.from_numpy(a_			).type(torch.long			).to(self.device			)

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

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

								all_logits.append(a_			)

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

								lowerCAmelCase_     :							Optional[Any]             =    collections.defaultdict(a_			)
								for i, p in enumerate(probabilities.squeeze().tolist()			):
										lowerCAmelCase_     :							List[Any]             =    token_type_ids_example[:, 0].tolist()[i]
										lowerCAmelCase_     :							List[str]             =    token_type_ids_example[:, 1].tolist()[i] - 1
										lowerCAmelCase_     :							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(a_			)

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

						lowerCAmelCase_     :							Optional[Any]             =    torch.cat(tuple(a_			)		,    0			)

						return (logits_batch,) if not self.aggregate else (logits_batch, torch.cat(tuple(a_			)		,    0			))
				else:
						lowerCAmelCase_     :							str             =    []
						lowerCAmelCase_     :							List[str]             =    []
						lowerCAmelCase_     :							str             =    None
						lowerCAmelCase_     :							List[Any]             =    inputs["input_ids"].shape[0]

						lowerCAmelCase_     :							Tuple             =    inputs["input_ids"]
						lowerCAmelCase_     :							Optional[int]             =    inputs["attention_mask"]
						lowerCAmelCase_     :							Dict             =    inputs["token_type_ids"].numpy()
						lowerCAmelCase_     :							Optional[Any]             =    None

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

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

												if row_id >= 0 and col_id >= 0 and segment_id == 1:
														lowerCAmelCase_     :							Union[str, Any]             =    int(prev_answers[(col_id, row_id)]			)

										lowerCAmelCase_     :							str             =    model_labels

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

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

								all_logits.append(a_			)

								lowerCAmelCase_     :							Optional[int]             =    tfp.distributions.Bernoulli(logits=a_			)
								lowerCAmelCase_     :							Any             =    dist_per_token.probs_parameter() * tf.cast(a_		,    tf.floataa			)

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

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

						lowerCAmelCase_     :							str             =    tf.concat(tuple(a_			)		,    0			)

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





		def __call__(     self							:			Optional[Any]		,    *a_							:			Tuple		,    **a_							:			List[Any]			):
				lowerCAmelCase_     :							int             =    self._args_parser(*a_		,    **a_			)

				lowerCAmelCase_     :							str             =    super().__call__(a_		,    **a_			)
				if len(a_			) == 1:
						return results[0]
				return results





		def      lowerCamelCase (     self							:			Tuple		,    a_							:			int=None		,    a_							:			Union[str, Any]=None		,    a_							:			str=None		,    **a_							:			Tuple			):
				lowerCAmelCase_     :							str             =    {}
				if padding is not None:
						lowerCAmelCase_     :							str             =    padding
				if truncation is not None:
						lowerCAmelCase_     :							str             =    truncation

				lowerCAmelCase_     :							Any             =    {}
				if sequential is not None:
						lowerCAmelCase_     :							str             =    sequential
				return preprocess_params, forward_params, {}





		def      lowerCamelCase (     self							:			List[Any]		,    a_							:			Optional[int]		,    a_							:			List[str]=None		,    a_							:			List[Any]=True		,    a_							:			Optional[Any]=None			):
				if truncation is None:
						if self.type == "tapas":
								lowerCAmelCase_     :							Union[str, Any]             =    "drop_rows_to_fit"
						else:
								lowerCAmelCase_     :							List[str]             =    "do_not_truncate"

				lowerCAmelCase_       ,			lowerCAmelCase_     :							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"			)
				lowerCAmelCase_     :							Dict             =    self.tokenizer(a_		,    a_		,    return_tensors=self.framework		,    truncation=a_		,    padding=a_			)
				lowerCAmelCase_     :							Optional[int]             =    table
				return inputs





		def      lowerCamelCase (     self							:			Any		,    a_							:			Optional[Any]		,    a_							:			List[str]=False			):
				lowerCAmelCase_     :							Optional[Any]             =    model_inputs.pop("table"			)

				if self.type == "tapas":
						if sequential:
								lowerCAmelCase_     :							Dict             =    self.sequential_inference(**a_			)
						else:
								lowerCAmelCase_     :							List[Any]             =    self.batch_inference(**a_			)
				else:
						lowerCAmelCase_     :							Optional[int]             =    self.model.generate(**a_			)
				lowerCAmelCase_     :							Any             =    {"model_inputs": model_inputs, "table": table, "outputs": outputs}
				return model_outputs





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

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

								answers.append(a_			)
						if len(a_			) == 0:
								raise PipelineException("Empty answer"			)
				else:
						lowerCAmelCase_     :							int             =    [{"answer": answer} for answer in self.tokenizer.batch_decode(a_		,    skip_special_tokens=a_			)]

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