

"""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     (						__a):
			def __call__(						self ,						_A=None ,						_A=None ,						**_A       )      ->      Optional[int]:






				'''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(_A ,						_A       ) and table.get("""query"""       ) is not None and table.get("""table"""       ) is not None:
						_UpperCAmelCase      :							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:
							_UpperCAmelCase      :							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(_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:
					_UpperCAmelCase      :							Optional[Any]       =   [{"""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      :							Any       =   pd.DataFrame(tqa_pipeline_input["""table"""]       )

				return tqa_pipeline_inputs






@add_end_docstrings(__a)
class       _UpperCAmelCase     (						__a):
			__a	:    List[str]			   =							"""table,query"""
			def __init__(						self ,						_A=TableQuestionAnsweringArgumentHandler() ,						*_A ,						**_A       )      ->      List[str]:






				'''simple docstring'''







				super().__init__(*_A ,						**_A       )
				_UpperCAmelCase      :							List[str]       =   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      :							Union[str, Any]       =   bool(getattr(self.model.config ,						"""aggregation_labels""" ,						_A       )       ) and bool(
				    getattr(self.model.config ,						"""num_aggregation_labels""" ,						_A       )       )
				_UpperCAmelCase      :							List[str]       =   """tapas""" if hasattr(self.model.config ,						"""aggregation_labels"""       ) else None
			def 							__snake_case	(						self ,						**_A       )      ->      Union[str, Any]:






				'''simple docstring'''







				return self.model(**_A       )
			def 							__snake_case	(						self ,						**_A       )      ->      str:






				'''simple docstring'''







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

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

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

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

							_UpperCAmelCase      :							str       =   torch.from_numpy(_A       ).type(torch.long       ).to(self.device       )

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

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

						all_logits.append(_A       )

						_UpperCAmelCase      :							Optional[int]       =   torch.distributions.Bernoulli(logits=_A       )
						_UpperCAmelCase      :							Optional[Any]       =   dist_per_token.probs * attention_mask_example.type(torch.floataa       ).to(
						    dist_per_token.probs.device       )

						_UpperCAmelCase      :							Optional[int]       =   collections.defaultdict(_A       )
						for i, p in enumerate(probabilities.squeeze().tolist()       ):
							_UpperCAmelCase      :							Tuple       =   token_type_ids_example[:, 0].tolist()[i]
							_UpperCAmelCase      :							Optional[int]       =   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(_A       )

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

					_UpperCAmelCase      :							Tuple       =   torch.cat(tuple(_A       ) ,						0       )

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

					_UpperCAmelCase      :							int       =   inputs["""input_ids"""]
					_UpperCAmelCase      :							Dict       =   inputs["""attention_mask"""]
					_UpperCAmelCase      :							Any       =   inputs["""token_type_ids"""].numpy()
					_UpperCAmelCase      :							Union[str, 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:
							_UpperCAmelCase      :							List[str]       =   token_type_ids_example[:, 3]  # shape (seq_len,)
							_UpperCAmelCase      :							Optional[int]       =   np.zeros_like(_A ,						dtype=np.intaa       )  # shape (seq_len,)

							_UpperCAmelCase      :							List[str]       =   token_type_ids[index]  # shape (seq_len, 7)
							for i in range(model_labels.shape[0]       ):
								_UpperCAmelCase      :							Optional[int]       =   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 row_id >= 0 and col_id >= 0 and segment_id == 1:
									_UpperCAmelCase      :							Optional[int]       =   int(prev_answers[(col_id, row_id)]       )

							_UpperCAmelCase      :							List[str]       =   model_labels

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

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

						all_logits.append(_A       )

						_UpperCAmelCase      :							Any       =   tfp.distributions.Bernoulli(logits=_A       )
						_UpperCAmelCase      :							Tuple       =   dist_per_token.probs_parameter() * tf.cast(_A ,						tf.floataa       )

						_UpperCAmelCase      :							Any       =   collections.defaultdict(_A       )
						_UpperCAmelCase      :							Dict       =   token_type_ids_example
						for i, p in enumerate(tf.squeeze(_A       ).numpy().tolist()       ):
							_UpperCAmelCase      :							Optional[int]       =   token_type_ids_example[:, 0].tolist()[i]
							_UpperCAmelCase      :							Optional[int]       =   token_type_ids_example[:, 1].tolist()[i] - 1
							_UpperCAmelCase      :							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       )

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

					_UpperCAmelCase      :							Dict       =   tf.concat(tuple(_A       ) ,						0       )

					return (logits_batch,) if not self.aggregate else (logits_batch, tf.concat(tuple(_A       ) ,						0       ))
			def __call__(						self ,						*_A ,						**_A       )      ->      Union[str, Any]:






				'''simple docstring'''







				_UpperCAmelCase      :							Any       =   self._args_parser(*_A ,						**_A       )

				_UpperCAmelCase      :							Dict       =   super().__call__(_A ,						**_A       )
				if len(_A       ) == 1:
					return results[0]
				return results
			def 							__snake_case	(						self ,						_A=None ,						_A=None ,						_A=None ,						**_A       )      ->      str:






				'''simple docstring'''







				_UpperCAmelCase      :							Union[str, Any]       =   {}
				if padding is not None:
					_UpperCAmelCase      :							int       =   padding
				if truncation is not None:
					_UpperCAmelCase      :							Dict       =   truncation

				_UpperCAmelCase      :							int       =   {}
				if sequential is not None:
					_UpperCAmelCase      :							Optional[int]       =   sequential
				return preprocess_params, forward_params, {}
			def 							__snake_case	(						self ,						_A ,						_A=None ,						_A=True ,						_A=None       )      ->      List[Any]:






				'''simple docstring'''







				if truncation is None:
					if self.type == "tapas":
						_UpperCAmelCase      :							int       =   """drop_rows_to_fit"""
					else:
						_UpperCAmelCase      :							Any       =   """do_not_truncate"""

				_UpperCAmelCase      :							Any       =   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      :							Optional[int]       =   self.tokenizer(_A ,						_A ,						return_tensors=self.framework ,						truncation=_A ,						padding=_A       )
				_UpperCAmelCase      :							Union[str, Any]       =   table
				return inputs
			def 							__snake_case	(						self ,						_A ,						_A=False       )      ->      int:






				'''simple docstring'''







				_UpperCAmelCase      :							Tuple       =   model_inputs.pop("""table"""       )

				if self.type == "tapas":
					if sequential:
						_UpperCAmelCase      :							int       =   self.sequential_inference(**_A       )
					else:
						_UpperCAmelCase      :							Dict       =   self.batch_inference(**_A       )
				else:
					_UpperCAmelCase      :							Union[str, Any]       =   self.model.generate(**_A       )
				_UpperCAmelCase      :							int       =   {"""model_inputs""": model_inputs, """table""": table, """outputs""": outputs}
				return model_outputs
			def 							__snake_case	(						self ,						_A       )      ->      List[str]:






				'''simple docstring'''







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

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

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

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








"""simple docstring"""






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


lowerCamelCase__			:					Union[str, Any]							  =					{
    '''configuration_unispeech_sat''': ['''UNISPEECH_SAT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''UniSpeechSatConfig'''],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						lowerCamelCase__			:					List[str]							  =					[
						    '''UNISPEECH_SAT_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''UniSpeechSatForAudioFrameClassification''',
						    '''UniSpeechSatForCTC''',
						    '''UniSpeechSatForPreTraining''',
						    '''UniSpeechSatForSequenceClassification''',
						    '''UniSpeechSatForXVector''',
						    '''UniSpeechSatModel''',
						    '''UniSpeechSatPreTrainedModel''',
						]

if TYPE_CHECKING:
						from .configuration_unispeech_sat import UNISPEECH_SAT_PRETRAINED_CONFIG_ARCHIVE_MAP, UniSpeechSatConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_unispeech_sat import (
												    UNISPEECH_SAT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    UniSpeechSatForAudioFrameClassification,
												    UniSpeechSatForCTC,
												    UniSpeechSatForPreTraining,
												    UniSpeechSatForSequenceClassification,
												    UniSpeechSatForXVector,
												    UniSpeechSatModel,
												    UniSpeechSatPreTrainedModel,
												)

else:
						import sys

						lowerCamelCase__			:					Any							  =					_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





