




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






       def __call__(   self ,    _UpperCamelCase=None ,    _UpperCamelCase=None ,    **_UpperCamelCase      )->			Union[str, Any]:
            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:
                      _A				=       [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:
                           _A				=       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:
                 _A				=       [{'''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.'      )

                      _A				=       pd.DataFrame(tqa_pipeline_input['table']      )

            return tqa_pipeline_inputs






@add_end_docstrings(__SCREAMING_SNAKE_CASE  )
class 					lowerCAmelCase_     (    __SCREAMING_SNAKE_CASE  ):
       __UpperCAmelCase				='table,query'






       def __init__(   self ,    _UpperCamelCase=TableQuestionAnsweringArgumentHandler() ,    *_UpperCamelCase ,    **_UpperCamelCase      )->			Dict:
            super().__init__(*UpperCamelCase__ ,    **UpperCamelCase__      )
            _A				=       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()      )      )

            _A				=       bool(getattr(self.model.config ,    'aggregation_labels' ,    UpperCamelCase__      )      ) and bool(
                getattr(self.model.config ,    'num_aggregation_labels' ,    UpperCamelCase__      )      )
            _A				=       '''tapas''' if hasattr(self.model.config ,    'aggregation_labels'      ) else None






       def      UpperCamelCase	(   self ,    **_UpperCamelCase      )->			Optional[Any]:
            return self.model(**UpperCamelCase__      )






       def      UpperCamelCase	(   self ,    **_UpperCamelCase      )->			Optional[int]:







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

                 _A				=       inputs['''input_ids'''].to(self.device      )
                 _A				=       inputs['''attention_mask'''].to(self.device      )
                 _A				=       inputs['''token_type_ids'''].to(self.device      )
                 _A				=       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:
                           _A				=       token_type_ids_example[:, 3]  # shape (seq_len,)
                           _A				=       np.zeros_like(prev_labels_example.cpu().numpy()      )  # shape (seq_len,)

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

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

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

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

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

                      all_logits.append(UpperCamelCase__      )

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

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

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

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

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

                 _A				=       inputs['''input_ids''']
                 _A				=       inputs['''attention_mask''']
                 _A				=       inputs['''token_type_ids'''].numpy()
                 _A				=       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:
                           _A				=       token_type_ids_example[:, 3]  # shape (seq_len,)
                           _A				=       np.zeros_like(UpperCamelCase__ ,    dtype=np.intaa      )  # shape (seq_len,)

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

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

                           _A				=       model_labels

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

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

                      all_logits.append(UpperCamelCase__      )

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

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

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

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

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






       def __call__(   self ,    *_UpperCamelCase ,    **_UpperCamelCase      )->			Tuple:
            _A				=       self._args_parser(*UpperCamelCase__ ,    **UpperCamelCase__      )

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






       def      UpperCamelCase	(   self ,    _UpperCamelCase=None ,    _UpperCamelCase=None ,    _UpperCamelCase=None ,    **_UpperCamelCase      )->			List[Any]:
            _A				=       {}
            if padding is not None:
                 _A				=       padding
            if truncation is not None:
                 _A				=       truncation

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






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase=None ,    _UpperCamelCase=True ,    _UpperCamelCase=None      )->			Tuple:







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

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






       def      UpperCamelCase	(   self ,    _UpperCamelCase ,    _UpperCamelCase=False      )->			Optional[int]:
            _A				=       model_inputs.pop('table'      )

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






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

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

                      answers.append(UpperCamelCase__      )
                 if len(UpperCamelCase__      ) == 0:
                      raise PipelineException('Empty answer'      )
            else:
                 _A				=       [{'''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_tf_available,
    is_torch_available,
    is_vision_available,
)


lowerCAmelCase  =				{
    """configuration_segformer""": ["""SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SegformerConfig""", """SegformerOnnxConfig"""]
}

try:
    if not is_vision_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowerCAmelCase  =				["""SegformerFeatureExtractor"""]
    lowerCAmelCase  =				["""SegformerImageProcessor"""]

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowerCAmelCase  =				[
        """SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """SegformerDecodeHead""",
        """SegformerForImageClassification""",
        """SegformerForSemanticSegmentation""",
        """SegformerLayer""",
        """SegformerModel""",
        """SegformerPreTrainedModel""",
    ]

try:
    if not is_tf_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    lowerCAmelCase  =				[
        """TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
        """TFSegformerDecodeHead""",
        """TFSegformerForImageClassification""",
        """TFSegformerForSemanticSegmentation""",
        """TFSegformerModel""",
        """TFSegformerPreTrainedModel""",
    ]


if TYPE_CHECKING:
    from .configuration_segformer import SEGFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, SegformerConfig, SegformerOnnxConfig

    try:
        if not is_vision_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .feature_extraction_segformer import SegformerFeatureExtractor
        from .image_processing_segformer import SegformerImageProcessor

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_segformer import (
            SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
            SegformerDecodeHead,
            SegformerForImageClassification,
            SegformerForSemanticSegmentation,
            SegformerLayer,
            SegformerModel,
            SegformerPreTrainedModel,
        )
    try:
        if not is_tf_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_tf_segformer import (
            TF_SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
            TFSegformerDecodeHead,
            TFSegformerForImageClassification,
            TFSegformerForSemanticSegmentation,
            TFSegformerModel,
            TFSegformerPreTrainedModel,
        )

else:
    import sys

    lowerCAmelCase  =				_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





