






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 							__A	(     a					):





				"""simple docstring"""







				def __call__(					self				,    lowerCamelCase__=None				,    lowerCamelCase__=None				,    **lowerCamelCase__       ):




									"""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 :   Dict									=[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 :   Union[str, 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:
														__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 :   int									=pd.DataFrame(tqa_pipeline_input['table']       )

									return tqa_pipeline_inputs




@add_end_docstrings(a					)
class 							__A	(     a					):





				"""simple docstring"""







				UpperCamelCase__					: Optional[int]     					="""table,query"""


				def __init__(					self				,    lowerCamelCase__=TableQuestionAnsweringArgumentHandler()				,    *lowerCamelCase__				,    **lowerCamelCase__       ):




									"""simple docstring"""

									super().__init__(*lowerCamelCase__				,    **lowerCamelCase__       )
									__UpperCamelCase :   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()       )       )

									__UpperCamelCase :   List[str]									=bool(getattr(self.model.config				,    'aggregation_labels'				,    lowerCamelCase__       )       ) and bool(
									    getattr(self.model.config				,    'num_aggregation_labels'				,    lowerCamelCase__       )       )
									__UpperCamelCase :   Optional[int]									='tapas' if hasattr(self.model.config				,    'aggregation_labels'       ) else None


				def  __lowercase				(					self				,    **lowerCamelCase__       ):




									"""simple docstring"""

									return self.model(**lowerCamelCase__       )


				def  __lowercase				(					self				,    **lowerCamelCase__       ):




									"""simple docstring"""

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

														__UpperCamelCase :   Optional[int]									=inputs['input_ids'].to(self.device       )
														__UpperCamelCase :   Any									=inputs['attention_mask'].to(self.device       )
														__UpperCamelCase :   List[str]									=inputs['token_type_ids'].to(self.device       )
														__UpperCamelCase :   Tuple									=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 :   Tuple									=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 :   List[Any]									=token_type_ids_example[:, 1].tolist()[i] - 1
																													__UpperCamelCase :   List[str]									=token_type_ids_example[:, 2].tolist()[i] - 1

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

																								__UpperCamelCase :   str									=torch.from_numpy(lowerCamelCase__       ).type(torch.long       ).to(self.device       )

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

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

																			all_logits.append(lowerCamelCase__       )

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

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

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

														__UpperCamelCase :   str									=torch.cat(tuple(lowerCamelCase__       )				,    0       )

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

														__UpperCamelCase :   List[str]									=inputs['input_ids']
														__UpperCamelCase :   Union[str, Any]									=inputs['attention_mask']
														__UpperCamelCase :   Any									=inputs['token_type_ids'].numpy()
														__UpperCamelCase :   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 :   List[str]									=token_type_ids_example[:, 3]  # shape (seq_len,)
																								__UpperCamelCase :   str									=np.zeros_like(lowerCamelCase__				,    dtype=np.intaa       )  # shape (seq_len,)

																								__UpperCamelCase :   Union[str, Any]									=token_type_ids[index]  # shape (seq_len, 7)
																								for i in range(model_labels.shape[0]       ):
																													__UpperCamelCase :   Any									=token_type_ids_example[:, 0].tolist()[i]
																													__UpperCamelCase :   Optional[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 :   str									=int(prev_answers[(col_id, row_id)]       )

																								__UpperCamelCase :   str									=model_labels

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

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

																			all_logits.append(lowerCamelCase__       )

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

																			__UpperCamelCase :   str									=collections.defaultdict(lowerCamelCase__       )
																			__UpperCamelCase :   Tuple									=token_type_ids_example
																			for i, p in enumerate(tf.squeeze(lowerCamelCase__       ).numpy().tolist()       ):
																								__UpperCamelCase :   Optional[Any]									=token_type_ids_example[:, 0].tolist()[i]
																								__UpperCamelCase :   str									=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 :   Union[str, Any]									={key: np.array(coords_to_probs[key]       ).mean() > 0.5 for key in coords_to_probs}

														__UpperCamelCase :   Optional[int]									=tf.concat(tuple(lowerCamelCase__       )				,    0       )

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


				def __call__(					self				,    *lowerCamelCase__				,    **lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   Any									=self._args_parser(*lowerCamelCase__				,    **lowerCamelCase__       )

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


				def  __lowercase				(					self				,    lowerCamelCase__=None				,    lowerCamelCase__=None				,    lowerCamelCase__=None				,    **lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   List[Any]									={}
									if padding is not None:
														__UpperCamelCase :   int									=padding
									if truncation is not None:
														__UpperCamelCase :   List[str]									=truncation

									__UpperCamelCase :   Tuple									={}
									if sequential is not None:
														__UpperCamelCase :   List[Any]									=sequential
									return preprocess_params, forward_params, {}


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__=None				,    lowerCamelCase__=True				,    lowerCamelCase__=None       ):




									"""simple docstring"""

									if truncation is None:
														if self.type == "tapas":
																			__UpperCamelCase :   Dict									='drop_rows_to_fit'
														else:
																			__UpperCamelCase :   Any									='do_not_truncate'

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


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__=False       ):




									"""simple docstring"""

									__UpperCamelCase :   Optional[Any]									=model_inputs.pop('table'       )

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







				def  __lowercase				(					self				,    lowerCamelCase__       ):




									"""simple docstring"""

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

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

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

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









from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A_			:Union[str, Any]	    =		{'''configuration_nat''': ['''NAT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''NatConfig''']}


try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							A_			:Optional[Any]	    =		[
							    '''NAT_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''NatForImageClassification''',
							    '''NatModel''',
							    '''NatPreTrainedModel''',
							    '''NatBackbone''',
							]

if TYPE_CHECKING:
							from .configuration_nat import NAT_PRETRAINED_CONFIG_ARCHIVE_MAP, NatConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_nat import (
														    NAT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    NatBackbone,
														    NatForImageClassification,
														    NatModel,
														    NatPreTrainedModel,
														)

else:
							import sys

							A_			:Tuple	    =		_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

