

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



 def __call__(    self		:    Tuple ,      a__		:    Optional[int]=None ,      a__		:    int=None ,      **a__		:    List[str]			):
      # Returns tqa_pipeline_inputs of shape:
      # [
      #   {"table": pd.DataFrame, "query": List[str]},
      #   ...,
      #   {"table": pd.DataFrame, "query" : List[str]}
      # ]
      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:
                __magic_name__						   =				[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:
                     __magic_name__						   =				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:
           __magic_name__						   =				[{'''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.'''			)

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

      return tqa_pipeline_inputs






@add_end_docstrings(__a	)
class   _SCREAMING_SNAKE_CASE    (    __a	):
 __SCREAMING_SNAKE_CASE     :Dict			       =     """table,query"""



 def __init__(    self		:    List[str] ,      a__		:    List[str]=TableQuestionAnsweringArgumentHandler() ,      *a__		:    int ,      **a__		:    List[Any]			):
      super().__init__(*UpperCAmelCase_ ,      **UpperCAmelCase_			)
      __magic_name__						   =				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()			)			)

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



 def  snake_case__  (    self		:    str ,      **a__		:    int			):
      return self.model(**UpperCAmelCase_			)



 def  snake_case__  (    self		:    List[Any] ,      **a__		:    Optional[int]			):
      if self.framework == "pt":
           __magic_name__						   =				[]
           __magic_name__						   =				[]
           __magic_name__						   =				None
           __magic_name__						   =				inputs['''input_ids'''].shape[0]

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

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

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

                     __magic_name__						   =				torch.from_numpy(UpperCAmelCase_			).type(torch.long			).to(self.device			)

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

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

                all_logits.append(UpperCAmelCase_			)

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

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

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

           __magic_name__						   =				torch.cat(tuple(UpperCAmelCase_			) ,      0			)

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

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

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

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

                     __magic_name__						   =				model_labels

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

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

                all_logits.append(UpperCAmelCase_			)

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

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

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

           __magic_name__						   =				tf.concat(tuple(UpperCAmelCase_			) ,      0			)

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



 def __call__(    self		:    Optional[int] ,      *a__		:    Optional[int] ,      **a__		:    Union[str, Any]			):
      __magic_name__						   =				self._args_parser(*UpperCAmelCase_ ,      **UpperCAmelCase_			)

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



 def  snake_case__  (    self		:    List[Any] ,      a__		:    int=None ,      a__		:    List[Any]=None ,      a__		:    Tuple=None ,      **a__		:    Dict			):
      __magic_name__						   =				{}
      if padding is not None:
           __magic_name__						   =				padding
      if truncation is not None:
           __magic_name__						   =				truncation

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



 def  snake_case__  (    self		:    str ,      a__		:    Any ,      a__		:    int=None ,      a__		:    Union[str, Any]=True ,      a__		:    str=None			):
      if truncation is None:
           if self.type == "tapas":
                __magic_name__						   =				'''drop_rows_to_fit'''
           else:
                __magic_name__						   =				'''do_not_truncate'''

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



 def  snake_case__  (    self		:    List[str] ,      a__		:    Tuple ,      a__		:    List[str]=False			):
      __magic_name__						   =				model_inputs.pop('''table'''			)

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



 def  snake_case__  (    self		:    Tuple ,      a__		:    Optional[int]			):
      __magic_name__						   =				model_outputs['''model_inputs''']
      __magic_name__						   =				model_outputs['''table''']
      __magic_name__						   =				model_outputs['''outputs''']
      if self.type == "tapas":
           if self.aggregate:
                __magic_name__   ,  __magic_name__						   =				outputs[:2]
                __magic_name__						   =				self.tokenizer.convert_logits_to_predictions(UpperCAmelCase_ ,      UpperCAmelCase_ ,      UpperCAmelCase_			)
                __magic_name__   ,  __magic_name__						   =				predictions
                __magic_name__						   =				{i: self.model.config.aggregation_labels[pred] for i, pred in enumerate(UpperCAmelCase_			)}

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

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

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













from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL

from ...utils import BaseOutput, is_invisible_watermark_available, is_torch_available, is_transformers_available

@dataclass
class 					__A   (  a			):
				__A            = 42


if is_transformers_available() and is_torch_available() and is_invisible_watermark_available():
							from .pipeline_stable_diffusion_xl import StableDiffusionXLPipeline
							from .pipeline_stable_diffusion_xl_imgaimg import StableDiffusionXLImgaImgPipeline





