






'''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				:			int   ,   __UpperCAmelCase				:			Union[str, Any]=None   ,   __UpperCAmelCase				:			Dict=None   ,   **__UpperCAmelCase				:			int	):





									'''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:
																					_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(lowerCamelCase_		)
class      _UpperCAmelCase    (  lowerCamelCase_		):




			"""simple docstring"""

			snake_case					       =		"""table,query"""

			def __init__(	self				:			Tuple   ,   __UpperCAmelCase				:			Optional[int]=TableQuestionAnsweringArgumentHandler()   ,   *__UpperCAmelCase				:			str   ,   **__UpperCAmelCase				:			Union[str, Any]	):





									'''simple docstring'''





									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 		lowerCAmelCase				(	self				:			Union[str, Any]   ,   **__UpperCAmelCase				:			Any	):





									'''simple docstring'''





									return self.model(**__UpperCAmelCase	)

			def 		lowerCAmelCase				(	self				:			List[str]   ,   **__UpperCAmelCase				:			Any	):





									'''simple docstring'''





									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				:			Optional[int]   ,   *__UpperCAmelCase				:			Any   ,   **__UpperCAmelCase				:			Dict	):





									'''simple docstring'''





									_A      =			self._args_parser(*__UpperCAmelCase   ,   **__UpperCAmelCase	)

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

			def 		lowerCAmelCase				(	self				:			str   ,   __UpperCAmelCase				:			List[Any]=None   ,   __UpperCAmelCase				:			Tuple=None   ,   __UpperCAmelCase				:			Optional[Any]=None   ,   **__UpperCAmelCase				:			int	):





									'''simple docstring'''





									_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 		lowerCAmelCase				(	self				:			str   ,   __UpperCAmelCase				:			Union[str, Any]   ,   __UpperCAmelCase				:			List[Any]=None   ,   __UpperCAmelCase				:			Dict=True   ,   __UpperCAmelCase				:			List[Any]=None	):





									'''simple docstring'''





									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 		lowerCAmelCase				(	self				:			List[Any]   ,   __UpperCAmelCase				:			Dict   ,   __UpperCAmelCase				:			Union[str, Any]=False	):





									'''simple docstring'''





									_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 		lowerCAmelCase				(	self				:			Optional[Any]   ,   __UpperCAmelCase				:			Tuple	):





									'''simple docstring'''





									_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'''




def       __lowercase		(    __lowercase ) ->		int:

      '''simple docstring'''


      _A      =			str(__lowercase ).strip()
      if not bin_string:
            raise ValueError("Empty string was passed to the function" )
      _A      =			bin_string[0] == "-"
      if is_negative:
            _A      =			bin_string[1:]
      if not all(char in "01" for char in bin_string ):
            raise ValueError("Non-binary value was passed to the function" )
      _A      =			0
      for char in bin_string:
            _A      =			2 * decimal_number + int(__lowercase )
      return -decimal_number if is_negative else decimal_number


if __name__ == "__main__":
    from doctest import testmod

    testmod()





