


'''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__    (       lowerCAmelCase_		):



     """simple docstring"""






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

            '''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(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      ) and table.get('query'      ) is not None and table.get('table'      ) is not None:
                          _lowerCAmelCase			:							Dict      	=				[table]
                   elif isinstance(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      ) and len(_SCREAMING_SNAKE_CASE      ) > 0:
                          if not all(isinstance(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      ) for d in table      ):
                                 raise ValueError(
                                     F'Keyword argument `table` should be a list of dict, but is {(type(_SCREAMING_SNAKE_CASE      ) for d in table)}'      )

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

                          _lowerCAmelCase			:							int      	=				pd.DataFrame(tqa_pipeline_input['table']      )

            return tqa_pipeline_inputs
@add_end_docstrings(lowerCAmelCase_		)
class 		UpperCamelCase__    (       lowerCAmelCase_		):



     """simple docstring"""






     __magic_name__				=			'''table,query'''







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

            '''simple docstring'''

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

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







     def a		(       self					,		**snake_case__      ):

            '''simple docstring'''

            return self.model(**_SCREAMING_SNAKE_CASE      )







     def a		(       self					,		**snake_case__      ):

            '''simple docstring'''

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

                   _lowerCAmelCase			:							List[Any]      	=				inputs['input_ids'].to(self.device      )
                   _lowerCAmelCase			:							List[Any]      	=				inputs['attention_mask'].to(self.device      )
                   _lowerCAmelCase			:							Optional[Any]      	=				inputs['token_type_ids'].to(self.device      )
                   _lowerCAmelCase			:							int      	=				None

                   for index in range(_SCREAMING_SNAKE_CASE      ):
                          # 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			:							Any      	=				token_type_ids_example[:, 3]  # shape (seq_len,)
                                 _lowerCAmelCase			:							Optional[int]      	=				np.zeros_like(prev_labels_example.cpu().numpy()      )  # shape (seq_len,)

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

                                 _lowerCAmelCase			:							Optional[Any]      	=				torch.from_numpy(_SCREAMING_SNAKE_CASE      ).type(torch.long      ).to(self.device      )

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

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

                          all_logits.append(_SCREAMING_SNAKE_CASE      )

                          _lowerCAmelCase			:							Tuple      	=				torch.distributions.Bernoulli(logits=_SCREAMING_SNAKE_CASE      )
                          _lowerCAmelCase			:							Optional[Any]      	=				dist_per_token.probs * attention_mask_example.type(torch.floataa      ).to(
                              dist_per_token.probs.device      )

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

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

                   _lowerCAmelCase			:							Optional[int]      	=				torch.cat(tuple(_SCREAMING_SNAKE_CASE      )					,		0      )

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

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

                   for index in range(_SCREAMING_SNAKE_CASE      ):
                          # 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			:							str      	=				token_type_ids_example[:, 3]  # shape (seq_len,)
                                 _lowerCAmelCase			:							List[str]      	=				np.zeros_like(_SCREAMING_SNAKE_CASE					,		dtype=np.intaa      )  # shape (seq_len,)

                                 _lowerCAmelCase			:							Optional[int]      	=				token_type_ids[index]  # shape (seq_len, 7)
                                 for i in range(model_labels.shape[0]      ):
                                        _lowerCAmelCase			:							Dict      	=				token_type_ids_example[:, 0].tolist()[i]
                                        _lowerCAmelCase			:							Optional[int]      	=				token_type_ids_example[:, 1].tolist()[i] - 1
                                        _lowerCAmelCase			:							Any      	=				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]      	=				model_labels

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

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

                          all_logits.append(_SCREAMING_SNAKE_CASE      )

                          _lowerCAmelCase			:							Dict      	=				tfp.distributions.Bernoulli(logits=_SCREAMING_SNAKE_CASE      )
                          _lowerCAmelCase			:							Any      	=				dist_per_token.probs_parameter() * tf.cast(_SCREAMING_SNAKE_CASE					,		tf.floataa      )

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

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

                   _lowerCAmelCase			:							Tuple      	=				tf.concat(tuple(_SCREAMING_SNAKE_CASE      )					,		0      )

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







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

            '''simple docstring'''

            _lowerCAmelCase			:							str      	=				self._args_parser(*_SCREAMING_SNAKE_CASE					,		**_SCREAMING_SNAKE_CASE      )

            _lowerCAmelCase			:							List[str]      	=				super().__call__(_SCREAMING_SNAKE_CASE					,		**_SCREAMING_SNAKE_CASE      )
            if len(_SCREAMING_SNAKE_CASE      ) == 1:
                   return results[0]
            return results







     def a		(       self					,		snake_case__=None					,		snake_case__=None					,		snake_case__=None					,		**snake_case__      ):

            '''simple docstring'''

            _lowerCAmelCase			:							str      	=				{}
            if padding is not None:
                   _lowerCAmelCase			:							Optional[Any]      	=				padding
            if truncation is not None:
                   _lowerCAmelCase			:							Optional[int]      	=				truncation

            _lowerCAmelCase			:							str      	=				{}
            if sequential is not None:
                   _lowerCAmelCase			:							int      	=				sequential
            return preprocess_params, forward_params, {}







     def a		(       self					,		snake_case__					,		snake_case__=None					,		snake_case__=True					,		snake_case__=None      ):

            '''simple docstring'''

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

            _lowerCAmelCase	,   _lowerCAmelCase			:							List[str]      	=				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(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE					,		return_tensors=self.framework					,		truncation=_SCREAMING_SNAKE_CASE					,		padding=_SCREAMING_SNAKE_CASE      )
            _lowerCAmelCase			:							Tuple      	=				table
            return inputs







     def a		(       self					,		snake_case__					,		snake_case__=False      ):

            '''simple docstring'''

            _lowerCAmelCase			:							Any      	=				model_inputs.pop('table'      )

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







     def a		(       self					,		snake_case__      ):

            '''simple docstring'''

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

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

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

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








import warnings

from .generation import GenerationMixin



class 							_UpperCamelCase    (		lowerCAmelCase_   ):
       # warning at import time
       warnings.warn(
           '''Importing `GenerationMixin` from `src/transformers/generation_utils.py` is deprecated and will '''
           '''be removed in Transformers v5. Import as `from transformers import GenerationMixin` instead.'''     ,  lowerCAmelCase_     ,  )

