



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


					'''simple docstring'''





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

														if table[0].get('''query'''	) is not None and table[0].get('''table'''	) is not None:
																	lowerCamelCase__						:						List[str]						      =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(lowerCamelCase,    lowerCamelCase	) or isinstance(lowerCamelCase,    types.GeneratorType	):
														return table
											else:
														raise ValueError(
														    '''Invalid input. Keyword argument `table` should be either of type `dict` or `list`, but '''
														    F'''is {type(lowerCamelCase	)})'''	)
								else:
											lowerCamelCase__						:						Any						      =[{'''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.'''	)

														lowerCamelCase__						:						int						      =pd.DataFrame(tqa_pipeline_input['''table''']	)

								return tqa_pipeline_inputs





@add_end_docstrings(lowerCAmelCase_  )
class 			__SCREAMING_SNAKE_CASE      ( lowerCAmelCase_  ):


					'''simple docstring'''





					_a       =				'table,query'



					def __init__(			self				:    Optional[int],    lowerCamelCase				:    Dict=TableQuestionAnsweringArgumentHandler(),    *lowerCamelCase				:    int,    **lowerCamelCase				:    Tuple	)->   Dict:
								super().__init__(*lowerCamelCase,    **lowerCamelCase	)
								lowerCamelCase__						:						Optional[Any]						      =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()	)	)

								lowerCamelCase__						:						Tuple						      =bool(getattr(self.model.config,    '''aggregation_labels''',    lowerCamelCase	)	) and bool(
								    getattr(self.model.config,    '''num_aggregation_labels''',    lowerCamelCase	)	)
								lowerCamelCase__						:						int						      ='''tapas''' if hasattr(self.model.config,    '''aggregation_labels'''	) else None



					def 						snake_case			(			self				:    List[str],    **lowerCamelCase				:    Optional[Any]	)->   Optional[Any]:
								return self.model(**lowerCamelCase	)



					def 						snake_case			(			self				:    Tuple,    **lowerCamelCase				:    List[Any]	)->   Dict:
								if self.framework == "pt":
											lowerCamelCase__						:						Optional[Any]						      =[]
											lowerCamelCase__						:						List[str]						      =[]
											lowerCamelCase__						:						Union[str, Any]						      =None
											lowerCamelCase__						:						Any						      =inputs['''input_ids'''].shape[0]

											lowerCamelCase__						:						int						      =inputs['''input_ids'''].to(self.device	)
											lowerCamelCase__						:						str						      =inputs['''attention_mask'''].to(self.device	)
											lowerCamelCase__						:						Union[str, Any]						      =inputs['''token_type_ids'''].to(self.device	)
											lowerCamelCase__						:						int						      =None

											for index in range(lowerCamelCase	):
														# If sequences have already been processed, the token type IDs will be created according to the previous
														# answer.
														if prev_answers is not None:
																	lowerCamelCase__						:						Union[str, Any]						      =token_type_ids_example[:, 3]  # shape (seq_len,)
																	lowerCamelCase__						:						List[Any]						      =np.zeros_like(prev_labels_example.cpu().numpy()	)  # shape (seq_len,)

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

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

																	lowerCamelCase__						:						Optional[Any]						      =torch.from_numpy(lowerCamelCase	).type(torch.long	).to(self.device	)

														lowerCamelCase__						:						Union[str, Any]						      =input_ids[index]
														lowerCamelCase__						:						str						      =attention_mask[index]  # shape (seq_len,)
														lowerCamelCase__						:						str						      =token_type_ids[index]  # shape (seq_len, 7)
														lowerCamelCase__						:						List[Any]						      =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	),    )
														lowerCamelCase__						:						List[str]						      =outputs.logits

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

														all_logits.append(lowerCamelCase	)

														lowerCamelCase__						:						Any						      =torch.distributions.Bernoulli(logits=lowerCamelCase	)
														lowerCamelCase__						:						str						      =dist_per_token.probs * attention_mask_example.type(torch.floataa	).to(
														    dist_per_token.probs.device	)

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

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

											lowerCamelCase__						:						List[str]						      =torch.cat(tuple(lowerCamelCase	),    0	)

											return (logits_batch,) if not self.aggregate else (logits_batch, torch.cat(tuple(lowerCamelCase	),    0	))
								else:
											lowerCamelCase__						:						Union[str, Any]						      =[]
											lowerCamelCase__						:						int						      =[]
											lowerCamelCase__						:						Tuple						      =None
											lowerCamelCase__						:						int						      =inputs['''input_ids'''].shape[0]

											lowerCamelCase__						:						List[str]						      =inputs['''input_ids''']
											lowerCamelCase__						:						Optional[int]						      =inputs['''attention_mask''']
											lowerCamelCase__						:						Any						      =inputs['''token_type_ids'''].numpy()
											lowerCamelCase__						:						Dict						      =None

											for index in range(lowerCamelCase	):
														# If sequences have already been processed, the token type IDs will be created according to the previous
														# answer.
														if prev_answers is not None:
																	lowerCamelCase__						:						Optional[int]						      =token_type_ids_example[:, 3]  # shape (seq_len,)
																	lowerCamelCase__						:						Optional[Any]						      =np.zeros_like(lowerCamelCase,    dtype=np.intaa	)  # shape (seq_len,)

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

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

																	lowerCamelCase__						:						List[Any]						      =model_labels

														lowerCamelCase__						:						Any						      =input_ids[index]
														lowerCamelCase__						:						str						      =attention_mask[index]  # shape (seq_len,)
														lowerCamelCase__						:						Optional[Any]						      =token_type_ids[index]  # shape (seq_len, 7)
														lowerCamelCase__						:						List[Any]						      =self.model(
														    input_ids=np.expand_dims(lowerCamelCase,    axis=0	),    attention_mask=np.expand_dims(lowerCamelCase,    axis=0	),    token_type_ids=np.expand_dims(lowerCamelCase,    axis=0	),    )
														lowerCamelCase__						:						Optional[Any]						      =outputs.logits

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

														all_logits.append(lowerCamelCase	)

														lowerCamelCase__						:						int						      =tfp.distributions.Bernoulli(logits=lowerCamelCase	)
														lowerCamelCase__						:						Tuple						      =dist_per_token.probs_parameter() * tf.cast(lowerCamelCase,    tf.floataa	)

														lowerCamelCase__						:						Optional[int]						      =collections.defaultdict(lowerCamelCase	)
														lowerCamelCase__						:						List[Any]						      =token_type_ids_example
														for i, p in enumerate(tf.squeeze(lowerCamelCase	).numpy().tolist()	):
																	lowerCamelCase__						:						Union[str, Any]						      =token_type_ids_example[:, 0].tolist()[i]
																	lowerCamelCase__						:						List[Any]						      =token_type_ids_example[:, 1].tolist()[i] - 1
																	lowerCamelCase__						:						Any						      =token_type_ids_example[:, 2].tolist()[i] - 1
																	if col >= 0 and row >= 0 and segment_id == 1:
																				coords_to_probs[(col, row)].append(lowerCamelCase	)

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

											lowerCamelCase__						:						List[Any]						      =tf.concat(tuple(lowerCamelCase	),    0	)

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



					def __call__(			self				:    Dict,    *lowerCamelCase				:    int,    **lowerCamelCase				:    str	)->   int:
								lowerCamelCase__						:						List[Any]						      =self._args_parser(*lowerCamelCase,    **lowerCamelCase	)

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



					def 						snake_case			(			self				:    Dict,    lowerCamelCase				:    Dict=None,    lowerCamelCase				:    List[Any]=None,    lowerCamelCase				:    int=None,    **lowerCamelCase				:    int	)->   Optional[int]:
								lowerCamelCase__						:						Union[str, Any]						      ={}
								if padding is not None:
											lowerCamelCase__						:						Union[str, Any]						      =padding
								if truncation is not None:
											lowerCamelCase__						:						Any						      =truncation

								lowerCamelCase__						:						List[str]						      ={}
								if sequential is not None:
											lowerCamelCase__						:						int						      =sequential
								return preprocess_params, forward_params, {}



					def 						snake_case			(			self				:    Tuple,    lowerCamelCase				:    str,    lowerCamelCase				:    int=None,    lowerCamelCase				:    Tuple=True,    lowerCamelCase				:    Union[str, Any]=None	)->   List[str]:
								if truncation is None:
											if self.type == "tapas":
														lowerCamelCase__						:						Tuple						      ='''drop_rows_to_fit'''
											else:
														lowerCamelCase__						:						Union[str, Any]						      ='''do_not_truncate'''

								lowerCamelCase__      ,  lowerCamelCase__						:						List[str]						      =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'''	)
								lowerCamelCase__						:						List[Any]						      =self.tokenizer(lowerCamelCase,    lowerCamelCase,    return_tensors=self.framework,    truncation=lowerCamelCase,    padding=lowerCamelCase	)
								lowerCamelCase__						:						Dict						      =table
								return inputs



					def 						snake_case			(			self				:    Tuple,    lowerCamelCase				:    Optional[int],    lowerCamelCase				:    Dict=False	)->   List[Any]:
								lowerCamelCase__						:						Union[str, Any]						      =model_inputs.pop('''table'''	)

								if self.type == "tapas":
											if sequential:
														lowerCamelCase__						:						List[Any]						      =self.sequential_inference(**lowerCamelCase	)
											else:
														lowerCamelCase__						:						List[Any]						      =self.batch_inference(**lowerCamelCase	)
								else:
											lowerCamelCase__						:						Union[str, Any]						      =self.model.generate(**lowerCamelCase	)
								lowerCamelCase__						:						Optional[int]						      ={'''model_inputs''': model_inputs, '''table''': table, '''outputs''': outputs}
								return model_outputs




					def 						snake_case			(			self				:    Optional[Any],    lowerCamelCase				:    Tuple	)->   int:
								lowerCamelCase__						:						List[str]						      =model_outputs['''model_inputs''']
								lowerCamelCase__						:						Any						      =model_outputs['''table''']
								lowerCamelCase__						:						List[Any]						      =model_outputs['''outputs''']
								if self.type == "tapas":
											if self.aggregate:
														lowerCamelCase__      ,  lowerCamelCase__						:						List[str]						      =outputs[:2]
														lowerCamelCase__						:						Dict						      =self.tokenizer.convert_logits_to_predictions(lowerCamelCase,    lowerCamelCase,    lowerCamelCase	)
														lowerCamelCase__      ,  lowerCamelCase__						:						Optional[int]						      =predictions
														lowerCamelCase__						:						Optional[Any]						      ={i: self.model.config.aggregation_labels[pred] for i, pred in enumerate(lowerCamelCase	)}

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

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

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












"""simple docstring"""





import importlib
from typing import List, Optional, Union

from seqeval.metrics import accuracy_score, classification_report

import datasets


UpperCamelCase_						:					Optional[int]      				=			"""\
@inproceedings{ramshaw-marcus-1995-text,
    title = \"Text Chunking using Transformation-Based Learning\",
    author = \"Ramshaw, Lance  and
      Marcus, Mitch\",
    booktitle = \"Third Workshop on Very Large Corpora\",
    year = \"1995\",
    url = \"https://www.aclweb.org/anthology/W95-0107\",
}
@misc{seqeval,
  title={{seqeval}: A Python framework for sequence labeling evaluation},
  url={https://github.com/chakki-works/seqeval},
  note={Software available from https://github.com/chakki-works/seqeval},
  author={Hiroki Nakayama},
  year={2018},
}
"""

UpperCamelCase_						:					Any      				=			"""\
seqeval is a Python framework for sequence labeling evaluation.
seqeval can evaluate the performance of chunking tasks such as named-entity recognition, part-of-speech tagging, semantic role labeling and so on.

This is well-tested by using the Perl script conlleval, which can be used for
measuring the performance of a system that has processed the CoNLL-2000 shared task data.

seqeval supports following formats:
IOB1
IOB2
IOE1
IOE2
IOBES

See the [README.md] file at https://github.com/chakki-works/seqeval for more information.
"""

UpperCamelCase_						:					int      				=			"""
Produces labelling scores along with its sufficient statistics
from a source against one or more references.

Args:
    predictions: List of List of predicted labels (Estimated targets as returned by a tagger)
    references: List of List of reference labels (Ground truth (correct) target values)
    suffix: True if the IOB prefix is after type, False otherwise. default: False
    scheme: Specify target tagging scheme. Should be one of [\"IOB1\", \"IOB2\", \"IOE1\", \"IOE2\", \"IOBES\", \"BILOU\"].
        default: None
    mode: Whether to count correct entity labels with incorrect I/B tags as true positives or not.
        If you want to only count exact matches, pass mode=\"strict\". default: None.
    sample_weight: Array-like of shape (n_samples,), weights for individual samples. default: None
    zero_division: Which value to substitute as a metric value when encountering zero division. Should be on of 0, 1,
        \"warn\". \"warn\" acts as 0, but the warning is raised.

Returns:
    'scores': dict. Summary of the scores for overall and per type
        Overall:
            'accuracy': accuracy,
            'precision': precision,
            'recall': recall,
            'f1': F1 score, also known as balanced F-score or F-measure,
        Per type:
            'precision': precision,
            'recall': recall,
            'f1': F1 score, also known as balanced F-score or F-measure
Examples:

    >>> predictions = [['O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
    >>> references = [['O', 'O', 'O', 'B-MISC', 'I-MISC', 'I-MISC', 'O'], ['B-PER', 'I-PER', 'O']]
    >>> seqeval = datasets.load_metric(\"seqeval\")
    >>> results = seqeval.compute(predictions=predictions, references=references)
    >>> print(list(results.keys()))
    ['MISC', 'PER', 'overall_precision', 'overall_recall', 'overall_f1', 'overall_accuracy']
    >>> print(results[\"overall_f1\"])
    0.5
    >>> print(results[\"PER\"][\"f1\"])
    1.0
"""


@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION		,	_KWARGS_DESCRIPTION    )
class   _lowercase			(  datasets.Metric    ):


      def _UpperCamelCase    (     self					:						int    ):





            """simple docstring"""







            return datasets.MetricInfo(
                description=_DESCRIPTION				, citation=_CITATION				, homepage='''https://github.com/chakki-works/seqeval'''				, inputs_description=_KWARGS_DESCRIPTION				, features=datasets.Features(
                    {
                        '''predictions''': datasets.Sequence(datasets.Value('''string'''				, id='''label'''    )				, id='''sequence'''    ),
                        '''references''': datasets.Sequence(datasets.Value('''string'''				, id='''label'''    )				, id='''sequence'''    ),
                    }    )				, codebase_urls=['''https://github.com/chakki-works/seqeval''']				, reference_urls=['''https://github.com/chakki-works/seqeval''']				, )







      def _UpperCamelCase    (     self					:						List[Any]				, a					:						List[str]				, a					:						Optional[Any]				, a					:						bool = False				, a					:						Optional[str] = None				, a					:						Optional[str] = None				, a					:						Optional[List[int]] = None				, a					:						Union[str, int] = "warn"				, ):





            """simple docstring"""







            if scheme is not None:
                  try:
                        __snake_case   :     List[str]	      =importlib.import_module('''seqeval.scheme'''    )
                        __snake_case   :     int	      =getattr(a				, a    )
                  except AttributeError:
                        raise ValueError(f'''Scheme should be one of [IOB1, IOB2, IOE1, IOE2, IOBES, BILOU], got {scheme}'''    )
            __snake_case   :     Optional[Any]	      =classification_report(
                y_true=a				, y_pred=a				, suffix=a				, output_dict=a				, scheme=a				, mode=a				, sample_weight=a				, zero_division=a				, )
            report.pop('''macro avg'''    )
            report.pop('''weighted avg'''    )
            __snake_case   :     Union[str, Any]	      =report.pop('''micro avg'''    )

            __snake_case   :     str	      ={
                type_name: {
                    '''precision''': score['''precision'''],
                    '''recall''': score['''recall'''],
                    '''f1''': score['''f1-score'''],
                    '''number''': score['''support'''],
                }
                for type_name, score in report.items()
            }
            __snake_case   :     str	      =overall_score['''precision''']
            __snake_case   :     int	      =overall_score['''recall''']
            __snake_case   :     str	      =overall_score['''f1-score''']
            __snake_case   :     Union[str, Any]	      =accuracy_score(y_true=a				, y_pred=a    )

            return scores


