





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 			__A(    lowerCamelCase_			):

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





            """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(_lowercase,							_lowercase   ) and table.get('''query'''   ) is not None and table.get('''table'''   ) is not None:
                          _UpperCamelCase														=    [table]
                   elif isinstance(_lowercase,							_lowercase   ) and len(_lowercase   ) > 0:
                          if not all(isinstance(_lowercase,							_lowercase   ) for d in table   ):
                                 raise ValueError(
                                     F'''Keyword argument `table` should be a list of dict, but is {(type(_lowercase   ) 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(_lowercase,							_lowercase   ) or isinstance(_lowercase,							types.GeneratorType   ):
                          return table
                   else:
                          raise ValueError(
                              '''Invalid input. Keyword argument `table` should be either of type `dict` or `list`, but '''
                              F'''is {type(_lowercase   )})'''   )
            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(lowerCamelCase_			)
class 			__A(    lowerCamelCase_			):
     __A							=							"table,query"

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





            """simple docstring"""

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

     def 						_UpperCamelCase  (   self,							**A   ):





            """simple docstring"""

            return self.model(**_lowercase   )

     def 						_UpperCamelCase  (   self,							**A   ):





            """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(_lowercase   ):
                          # 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(_lowercase   ).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(_lowercase   )

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

                          _UpperCamelCase														=    collections.defaultdict(_lowercase   )
                          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(_lowercase   )

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

                   _UpperCamelCase														=    torch.cat(tuple(_lowercase   ),							0   )

                   return (logits_batch,) if not self.aggregate else (logits_batch, torch.cat(tuple(_lowercase   ),							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(_lowercase   ):
                          # 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(_lowercase,							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(_lowercase,							axis=0   ),							attention_mask=np.expand_dims(_lowercase,							axis=0   ),							token_type_ids=np.expand_dims(_lowercase,							axis=0   ),							)
                          _UpperCamelCase														=    outputs.logits

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

                          all_logits.append(_lowercase   )

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

                          _UpperCamelCase														=    collections.defaultdict(_lowercase   )
                          _UpperCamelCase														=    token_type_ids_example
                          for i, p in enumerate(tf.squeeze(_lowercase   ).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(_lowercase   )

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

                   _UpperCamelCase														=    tf.concat(tuple(_lowercase   ),							0   )

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

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





            """simple docstring"""

            _UpperCamelCase														=    self._args_parser(*_lowercase,							**_lowercase   )

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

     def 						_UpperCamelCase  (   self,							A=None,							A=None,							A=None,							**A   ):





            """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 						_UpperCamelCase  (   self,							A,							A=None,							A=True,							A=None   ):





            """simple docstring"""

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

            _UpperCamelCase							,		_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(_lowercase,							_lowercase,							return_tensors=self.framework,							truncation=_lowercase,							padding=_lowercase   )
            _UpperCamelCase														=    table
            return inputs

     def 						_UpperCamelCase  (   self,							A,							A=False   ):





            """simple docstring"""

            _UpperCamelCase														=    model_inputs.pop('''table'''   )

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



     def 						_UpperCamelCase  (   self,							A   ):





            """simple docstring"""

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

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

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

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







import hashlib
import importlib.util
import json
import os
import pathlib
from types import ModuleType

import pytest
import requests

__lowerCamelCase     :					Optional[Any]				  =						pathlib.Path.cwd().joinpath('''project_euler''')
__lowerCamelCase     :					Dict				  =						pathlib.Path.cwd().joinpath(
    '''scripts''', '''project_euler_answers.json'''
)

with open(PROJECT_EULER_ANSWERS_PATH) as file_handle:
   __lowerCamelCase     :					dict[str, str]				  =						json.load(file_handle)



def       __SCREAMING_SNAKE_CASE    (   __UpperCamelCase       :			pathlib.Path      )				->   ModuleType:







  """simple docstring"""





  SCREAMING_SNAKE_CASE__     				= importlib.util.spec_from_file_location(file_path.name					,	str(__UpperCamelCase      )      )
  SCREAMING_SNAKE_CASE__     				= importlib.util.module_from_spec(__UpperCamelCase      )  # type: ignore
  spec.loader.exec_module(__UpperCamelCase      )  # type: ignore
  return module



def       __SCREAMING_SNAKE_CASE    (   )				->   list[pathlib.Path]:







  """simple docstring"""





  SCREAMING_SNAKE_CASE__     				= []
  for problem_dir_path in PROJECT_EULER_DIR_PATH.iterdir():
    if problem_dir_path.is_file() or problem_dir_path.name.startswith("""_"""      ):
      continue
    for file_path in problem_dir_path.iterdir():
      if file_path.suffix != ".py" or file_path.name.startswith(("""_""", """test""")      ):
        continue
      solution_file_paths.append(__UpperCamelCase      )
  return solution_file_paths



def       __SCREAMING_SNAKE_CASE    (   )				->   str:







  """simple docstring"""





  with open(os.environ["""GITHUB_EVENT_PATH"""]      ) as file:
    SCREAMING_SNAKE_CASE__     				= json.load(__UpperCamelCase      )
  return event["pull_request"]["url"] + "/files"



def       __SCREAMING_SNAKE_CASE    (   )				->   list[pathlib.Path]:







  """simple docstring"""





  SCREAMING_SNAKE_CASE__     				= []
  SCREAMING_SNAKE_CASE__     				= {
      """Accept""": """application/vnd.github.v3+json""",
      """Authorization""": """token """ + os.environ["""GITHUB_TOKEN"""],
  }
  SCREAMING_SNAKE_CASE__     				= requests.get(get_files_url()					,	headers=__UpperCamelCase      ).json()
  for file in files:
    SCREAMING_SNAKE_CASE__     				= pathlib.Path.cwd().joinpath(file["""filename"""]      )
    if (
        filepath.suffix != ".py"
        or filepath.name.startswith(("""_""", """test""")      )
        or not filepath.name.startswith("""sol"""      )
    ):
      continue
    solution_file_paths.append(__UpperCamelCase      )
  return solution_file_paths



def       __SCREAMING_SNAKE_CASE    (   )				->   list[pathlib.Path]:







  """simple docstring"""





  if os.environ.get("""CI"""      ) and os.environ.get("""GITHUB_EVENT_NAME"""      ) == "pull_request":
    # Return only if there are any, otherwise default to all solutions
    if filepaths := added_solution_file_path():
      return filepaths
  return all_solution_file_paths()






@pytest.mark.parametrize(
    """solution_path"""					,	collect_solution_file_paths()					,	ids=lambda __UpperCamelCase      : f"""{path.parent.name}/{path.name}"""					,	)
def       __SCREAMING_SNAKE_CASE    (   __UpperCamelCase       :			pathlib.Path      )				->   None:







  """simple docstring"""





  SCREAMING_SNAKE_CASE__     				= solution_path.parent.name[8:].zfill(3      )
  SCREAMING_SNAKE_CASE__     				= PROBLEM_ANSWERS[problem_number]
  SCREAMING_SNAKE_CASE__     				= convert_path_to_module(__UpperCamelCase      )
  SCREAMING_SNAKE_CASE__     				= str(solution_module.solution()      )  # type: ignore
  SCREAMING_SNAKE_CASE__     				= hashlib.shaaaa(answer.encode()      ).hexdigest()
  assert (
      answer == expected
  ), f"""Expected solution to {problem_number} to have hash {expected}, got {answer}"""
