



'''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 							snake_case__			(							lowerCamelCase_     ):





	def __call__(				self      :       str				,lowerCamelCase__      :       Dict=None				,lowerCamelCase__      :       List[str]=None				,**lowerCamelCase__      :       Union[str, Any]				):







							'''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:
																			_UpperCamelCase      :	Tuple				       =     [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:
																									_UpperCamelCase      :	int				       =     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:
													_UpperCamelCase      :	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.'				)

																			_UpperCamelCase      :	Tuple				       =     pd.DataFrame(tqa_pipeline_input['table']				)

							return tqa_pipeline_inputs






@add_end_docstrings(lowerCamelCase_     )
class 							snake_case__			(							lowerCamelCase_     ):
	lowercase__						       =    """table,query"""





	def __init__(				self      :       int				,lowerCamelCase__      :       Tuple=TableQuestionAnsweringArgumentHandler()				,*lowerCamelCase__      :       Tuple				,**lowerCamelCase__      :       Union[str, Any]				):







							'''simple docstring'''
							super().__init__(*lowerCamelCase__				,**lowerCamelCase__				)
							_UpperCamelCase      :	Optional[int]				       =     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()				)				)

							_UpperCamelCase      :	Tuple				       =     bool(getattr(self.model.config				,'aggregation_labels'				,lowerCamelCase__				)				) and bool(
							    getattr(self.model.config				,'num_aggregation_labels'				,lowerCamelCase__				)				)
							_UpperCamelCase      :	Tuple				       =     "tapas" if hasattr(self.model.config				,'aggregation_labels'				) else None





	def 			UpperCamelCase_						(				self      :       str				,**lowerCamelCase__      :       str				):







							'''simple docstring'''
							return self.model(**lowerCamelCase__				)





	def 			UpperCamelCase_						(				self      :       Optional[int]				,**lowerCamelCase__      :       Any				):







							'''simple docstring'''







							if self.framework == "pt":
													_UpperCamelCase      :	Optional[int]				       =     []
													_UpperCamelCase      :	Union[str, Any]				       =     []
													_UpperCamelCase      :	Tuple				       =     None
													_UpperCamelCase      :	int				       =     inputs["input_ids"].shape[0]

													_UpperCamelCase      :	int				       =     inputs["input_ids"].to(self.device				)
													_UpperCamelCase      :	str				       =     inputs["attention_mask"].to(self.device				)
													_UpperCamelCase      :	Optional[Any]				       =     inputs["token_type_ids"].to(self.device				)
													_UpperCamelCase      :	List[str]				       =     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:
																									_UpperCamelCase      :	Tuple				       =     token_type_ids_example[:, 3]  # shape (seq_len,)
																									_UpperCamelCase      :	str				       =     np.zeros_like(prev_labels_example.cpu().numpy()				)  # shape (seq_len,)

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

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

																									_UpperCamelCase      :	Any				       =     torch.from_numpy(lowerCamelCase__				).type(torch.long				).to(self.device				)

																			_UpperCamelCase      :	Any				       =     input_ids[index]
																			_UpperCamelCase      :	Tuple				       =     attention_mask[index]  # shape (seq_len,)
																			_UpperCamelCase      :	List[Any]				       =     token_type_ids[index]  # shape (seq_len, 7)
																			_UpperCamelCase      :	List[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				)				,)
																			_UpperCamelCase      :	Tuple				       =     outputs.logits

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

																			all_logits.append(lowerCamelCase__				)

																			_UpperCamelCase      :	Optional[Any]				       =     torch.distributions.Bernoulli(logits=lowerCamelCase__				)
																			_UpperCamelCase      :	Union[str, Any]				       =     dist_per_token.probs * attention_mask_example.type(torch.floataa				).to(
																			    dist_per_token.probs.device				)

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

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

													_UpperCamelCase      :	Optional[int]				       =     torch.cat(tuple(lowerCamelCase__				)				,0				)

													return (logits_batch,) if not self.aggregate else (logits_batch, torch.cat(tuple(lowerCamelCase__				)				,0				))
							else:
													_UpperCamelCase      :	int				       =     []
													_UpperCamelCase      :	Dict				       =     []
													_UpperCamelCase      :	Dict				       =     None
													_UpperCamelCase      :	List[Any]				       =     inputs["input_ids"].shape[0]

													_UpperCamelCase      :	Tuple				       =     inputs["input_ids"]
													_UpperCamelCase      :	Optional[int]				       =     inputs["attention_mask"]
													_UpperCamelCase      :	Dict				       =     inputs["token_type_ids"].numpy()
													_UpperCamelCase      :	Dict				       =     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:
																									_UpperCamelCase      :	Optional[int]				       =     token_type_ids_example[:, 3]  # shape (seq_len,)
																									_UpperCamelCase      :	Optional[int]				       =     np.zeros_like(lowerCamelCase__				,dtype=np.intaa				)  # shape (seq_len,)

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

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

																									_UpperCamelCase      :	Tuple				       =     model_labels

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

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

																			all_logits.append(lowerCamelCase__				)

																			_UpperCamelCase      :	int				       =     tfp.distributions.Bernoulli(logits=lowerCamelCase__				)
																			_UpperCamelCase      :	int				       =     dist_per_token.probs_parameter() * tf.cast(lowerCamelCase__				,tf.floataa				)

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

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

													_UpperCamelCase      :	str				       =     tf.concat(tuple(lowerCamelCase__				)				,0				)

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





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







							'''simple docstring'''
							_UpperCamelCase      :	Optional[Any]				       =     self._args_parser(*lowerCamelCase__				,**lowerCamelCase__				)

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





	def 			UpperCamelCase_						(				self      :       List[str]				,lowerCamelCase__      :       Dict=None				,lowerCamelCase__      :       Optional[int]=None				,lowerCamelCase__      :       int=None				,**lowerCamelCase__      :       str				):







							'''simple docstring'''
							_UpperCamelCase      :	Optional[Any]				       =     {}
							if padding is not None:
													_UpperCamelCase      :	List[Any]				       =     padding
							if truncation is not None:
													_UpperCamelCase      :	int				       =     truncation

							_UpperCamelCase      :	List[str]				       =     {}
							if sequential is not None:
													_UpperCamelCase      :	int				       =     sequential
							return preprocess_params, forward_params, {}





	def 			UpperCamelCase_						(				self      :       Optional[int]				,lowerCamelCase__      :       str				,lowerCamelCase__      :       Optional[Any]=None				,lowerCamelCase__      :       Any=True				,lowerCamelCase__      :       Dict=None				):







							'''simple docstring'''







							if truncation is None:
													if self.type == "tapas":
																			_UpperCamelCase      :	List[Any]				       =     "drop_rows_to_fit"
													else:
																			_UpperCamelCase      :	Union[str, Any]				       =     "do_not_truncate"

							_UpperCamelCase      :	Dict				       =     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'				)
							_UpperCamelCase      :	Union[str, Any]				       =     self.tokenizer(lowerCamelCase__				,lowerCamelCase__				,return_tensors=self.framework				,truncation=lowerCamelCase__				,padding=lowerCamelCase__				)
							_UpperCamelCase      :	Optional[int]				       =     table
							return inputs





	def 			UpperCamelCase_						(				self      :       Any				,lowerCamelCase__      :       int				,lowerCamelCase__      :       Union[str, Any]=False				):







							'''simple docstring'''
							_UpperCamelCase      :	Dict				       =     model_inputs.pop('table'				)

							if self.type == "tapas":
													if sequential:
																			_UpperCamelCase      :	Dict				       =     self.sequential_inference(**lowerCamelCase__				)
													else:
																			_UpperCamelCase      :	Tuple				       =     self.batch_inference(**lowerCamelCase__				)
							else:
													_UpperCamelCase      :	int				       =     self.model.generate(**lowerCamelCase__				)
							_UpperCamelCase      :	int				       =     {"model_inputs": model_inputs, "table": table, "outputs": outputs}
							return model_outputs





	def 			UpperCamelCase_						(				self      :       Optional[int]				,lowerCamelCase__      :       Dict				):







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

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

																			answers.append(lowerCamelCase__				)
													if len(lowerCamelCase__				) == 0:
																			raise PipelineException('Empty answer'				)
							else:
													_UpperCamelCase      :	Dict				       =     [{"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'''
import os

from ...utils import logging
from .utils import DataProcessor, InputExample


snake_case_				:			Any						 =						logging.get_logger(__name__)






class 							lowercase__			(							lowercase     ):





	def __init__(				self      :       Union[str, Any]				,lowerCamelCase__      :       Tuple				,lowerCamelCase__      :       Optional[int]=None				):







							'''simple docstring'''
							_UpperCamelCase      :	Any				       =     language
							_UpperCamelCase      :	int				       =     train_language





	def 			UpperCamelCase_						(				self      :       Optional[int]				,lowerCamelCase__      :       Dict				):







							'''simple docstring'''
							_UpperCamelCase      :	Tuple				       =     self.language if self.train_language is None else self.train_language
							_UpperCamelCase      :	List[Any]				       =     self._read_tsv(os.path.join(lowerCamelCase__				,F'XNLI-MT-1.0/multinli/multinli.train.{lg}.tsv'				)				)
							_UpperCamelCase      :	Union[str, Any]				       =     []
							for i, line in enumerate(lowerCamelCase__				):
													if i == 0:
																			continue
													_UpperCamelCase      :	Optional[Any]				       =     F'train-{i}'
													_UpperCamelCase      :	List[str]				       =     line[0]
													_UpperCamelCase      :	Union[str, Any]				       =     line[1]
													_UpperCamelCase      :	Tuple				       =     'contradiction' if line[2] == 'contradictory' else line[2]
													if not isinstance(lowerCamelCase__				,lowerCamelCase__				):
																			raise ValueError(F'Training input {text_a} is not a string'				)
													if not isinstance(lowerCamelCase__				,lowerCamelCase__				):
																			raise ValueError(F'Training input {text_b} is not a string'				)
													if not isinstance(lowerCamelCase__				,lowerCamelCase__				):
																			raise ValueError(F'Training label {label} is not a string'				)
													examples.append(InputExample(guid=lowerCamelCase__				,text_a=lowerCamelCase__				,text_b=lowerCamelCase__				,label=lowerCamelCase__				)				)
							return examples





	def 			UpperCamelCase_						(				self      :       Optional[Any]				,lowerCamelCase__      :       List[str]				):







							'''simple docstring'''
							_UpperCamelCase      :	Optional[int]				       =     self._read_tsv(os.path.join(lowerCamelCase__				,'XNLI-1.0/xnli.test.tsv'				)				)
							_UpperCamelCase      :	List[str]				       =     []
							for i, line in enumerate(lowerCamelCase__				):
													if i == 0:
																			continue
													_UpperCamelCase      :	Optional[int]				       =     line[0]
													if language != self.language:
																			continue
													_UpperCamelCase      :	Optional[int]				       =     F'test-{i}'
													_UpperCamelCase      :	Optional[int]				       =     line[6]
													_UpperCamelCase      :	List[str]				       =     line[7]
													_UpperCamelCase      :	str				       =     line[1]
													if not isinstance(lowerCamelCase__				,lowerCamelCase__				):
																			raise ValueError(F'Training input {text_a} is not a string'				)
													if not isinstance(lowerCamelCase__				,lowerCamelCase__				):
																			raise ValueError(F'Training input {text_b} is not a string'				)
													if not isinstance(lowerCamelCase__				,lowerCamelCase__				):
																			raise ValueError(F'Training label {label} is not a string'				)
													examples.append(InputExample(guid=lowerCamelCase__				,text_a=lowerCamelCase__				,text_b=lowerCamelCase__				,label=lowerCamelCase__				)				)
							return examples





	def 			UpperCamelCase_						(				self      :       str				):







							'''simple docstring'''
							return ["contradiction", "entailment", "neutral"]
snake_case_				:			int						 =						{
    'xnli': XnliProcessor,
}

snake_case_				:			Tuple						 =						{
    'xnli': 'classification',
}

snake_case_				:			Any						 =						{
    'xnli': 3,
}


