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

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

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

    return tqa_pipeline_inputs







@add_end_docstrings(__SCREAMING_SNAKE_CASE)
class  UpperCAmelCase (       __SCREAMING_SNAKE_CASE):
 _lowerCamelCase :					List[Any]    					=					'table,query'

 def __init__( self :	List[Any],					a_ :	Union[str, Any]=TableQuestionAnsweringArgumentHandler(),					*a_ :	int,					**a_ :	Dict					):





    """simple docstring"""



    super().__init__(*UpperCamelCase__,					**UpperCamelCase__					)
    UpperCamelCase__ 						=						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__ 						=						bool(getattr(self.model.config,					"aggregation_labels",					UpperCamelCase__					)					) and bool(
        getattr(self.model.config,					"num_aggregation_labels",					UpperCamelCase__					)					)
    UpperCamelCase__ 						=						'''tapas''' if hasattr(self.model.config,					"aggregation_labels"					) else None

 def 		lowercase_						( self :	int,					**a_ :	List[str]					):





    """simple docstring"""



    return self.model(**UpperCamelCase__					)

 def 		lowercase_						( self :	str,					**a_ :	List[str]					):





    """simple docstring"""



    if self.framework == "pt":
       UpperCamelCase__ 						=						[]
       UpperCamelCase__ 						=						[]
       UpperCamelCase__ 						=						None
       UpperCamelCase__ 						=						inputs['''input_ids'''].shape[0]

       UpperCamelCase__ 						=						inputs['''input_ids'''].to(self.device					)
       UpperCamelCase__ 						=						inputs['''attention_mask'''].to(self.device					)
       UpperCamelCase__ 						=						inputs['''token_type_ids'''].to(self.device					)
       UpperCamelCase__ 						=						None

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

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

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

             UpperCamelCase__ 						=						torch.from_numpy(UpperCamelCase__					).type(torch.long					).to(self.device					)

          UpperCamelCase__ 						=						input_ids[index]
          UpperCamelCase__ 						=						attention_mask[index]  # shape (seq_len,)
          UpperCamelCase__ 						=						token_type_ids[index]  # shape (seq_len, 7)
          UpperCamelCase__ 						=						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__ 						=						outputs.logits

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

          all_logits.append(UpperCamelCase__					)

          UpperCamelCase__ 						=						torch.distributions.Bernoulli(logits=UpperCamelCase__					)
          UpperCamelCase__ 						=						dist_per_token.probs * attention_mask_example.type(torch.floataa					).to(
              dist_per_token.probs.device					)

          UpperCamelCase__ 						=						collections.defaultdict(UpperCamelCase__					)
          for i, p in enumerate(probabilities.squeeze().tolist()					):
             UpperCamelCase__ 						=						token_type_ids_example[:, 0].tolist()[i]
             UpperCamelCase__ 						=						token_type_ids_example[:, 1].tolist()[i] - 1
             UpperCamelCase__ 						=						token_type_ids_example[:, 2].tolist()[i] - 1
             if col >= 0 and row >= 0 and segment_id == 1:
                coords_to_probs[(col, row)].append(UpperCamelCase__					)

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

       UpperCamelCase__ 						=						torch.cat(tuple(UpperCamelCase__					),					0					)

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

       UpperCamelCase__ 						=						inputs['''input_ids''']
       UpperCamelCase__ 						=						inputs['''attention_mask''']
       UpperCamelCase__ 						=						inputs['''token_type_ids'''].numpy()
       UpperCamelCase__ 						=						None

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

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

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

             UpperCamelCase__ 						=						model_labels

          UpperCamelCase__ 						=						input_ids[index]
          UpperCamelCase__ 						=						attention_mask[index]  # shape (seq_len,)
          UpperCamelCase__ 						=						token_type_ids[index]  # shape (seq_len, 7)
          UpperCamelCase__ 						=						self.model(
              input_ids=np.expand_dims(UpperCamelCase__,					axis=0					),					attention_mask=np.expand_dims(UpperCamelCase__,					axis=0					),					token_type_ids=np.expand_dims(UpperCamelCase__,					axis=0					),					)
          UpperCamelCase__ 						=						outputs.logits

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

          all_logits.append(UpperCamelCase__					)

          UpperCamelCase__ 						=						tfp.distributions.Bernoulli(logits=UpperCamelCase__					)
          UpperCamelCase__ 						=						dist_per_token.probs_parameter() * tf.cast(UpperCamelCase__,					tf.floataa					)

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

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

       UpperCamelCase__ 						=						tf.concat(tuple(UpperCamelCase__					),					0					)

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

 def __call__( self :	List[Any],					*a_ :	Union[str, Any],					**a_ :	List[Any]					):





    """simple docstring"""



    UpperCamelCase__ 						=						self._args_parser(*UpperCamelCase__,					**UpperCamelCase__					)

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

 def 		lowercase_						( self :	Union[str, Any],					a_ :	Optional[int]=None,					a_ :	int=None,					a_ :	str=None,					**a_ :	Optional[int]					):





    """simple docstring"""



    UpperCamelCase__ 						=						{}
    if padding is not None:
       UpperCamelCase__ 						=						padding
    if truncation is not None:
       UpperCamelCase__ 						=						truncation

    UpperCamelCase__ 						=						{}
    if sequential is not None:
       UpperCamelCase__ 						=						sequential
    return preprocess_params, forward_params, {}

 def 		lowercase_						( self :	Union[str, Any],					a_ :	Optional[int],					a_ :	Dict=None,					a_ :	Union[str, Any]=True,					a_ :	Optional[Any]=None					):





    """simple docstring"""



    if truncation is None:
       if self.type == "tapas":
          UpperCamelCase__ 						=						'''drop_rows_to_fit'''
       else:
          UpperCamelCase__ 						=						'''do_not_truncate'''

    UpperCamelCase__ 						=						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__ 						=						self.tokenizer(UpperCamelCase__,					UpperCamelCase__,					return_tensors=self.framework,					truncation=UpperCamelCase__,					padding=UpperCamelCase__					)
    UpperCamelCase__ 						=						table
    return inputs

 def 		lowercase_						( self :	str,					a_ :	int,					a_ :	Dict=False					):





    """simple docstring"""



    UpperCamelCase__ 						=						model_inputs.pop("table"					)

    if self.type == "tapas":
       if sequential:
          UpperCamelCase__ 						=						self.sequential_inference(**UpperCamelCase__					)
       else:
          UpperCamelCase__ 						=						self.batch_inference(**UpperCamelCase__					)
    else:
       UpperCamelCase__ 						=						self.model.generate(**UpperCamelCase__					)
    UpperCamelCase__ 						=						{'''model_inputs''': model_inputs, '''table''': table, '''outputs''': outputs}
    return model_outputs

 def 		lowercase_						( self :	str,					a_ :	Dict					):





    """simple docstring"""



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

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

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

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



'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


__lowercase:		int							=     {
    "configuration_open_llama": ["OPEN_LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP", "OpenLlamaConfig"],
}

try:
       if not is_sentencepiece_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __lowercase:		str							=     ["LlamaTokenizer"]

try:
       if not is_tokenizers_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __lowercase:		List[Any]							=     ["LlamaTokenizerFast"]

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       __lowercase:		Union[str, Any]							=     [
           "OpenLlamaForCausalLM",
           "OpenLlamaModel",
           "OpenLlamaPreTrainedModel",
           "OpenLlamaForSequenceClassification",
       ]


if TYPE_CHECKING:
       from .configuration_open_llama import OPEN_LLAMA_PRETRAINED_CONFIG_ARCHIVE_MAP, OpenLlamaConfig

       try:
              if not is_sentencepiece_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from transformers import LlamaTokenizer

       try:
              if not is_tokenizers_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from transformers import LlamaTokenizerFast

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_open_llama import (
                  OpenLlamaForCausalLM,
                  OpenLlamaForSequenceClassification,
                  OpenLlamaModel,
                  OpenLlamaPreTrainedModel,
              )


else:
       import sys

       __lowercase:		Tuple							=     _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)