





from copy import deepcopy
from unittest.case import TestCase

import pytest

from datasets.arrow_dataset import Dataset
from datasets.features import Audio, ClassLabel, Features, Image, Sequence, Value
from datasets.info import DatasetInfo
from datasets.tasks import (
    AudioClassification,
    AutomaticSpeechRecognition,
    ImageClassification,
    LanguageModeling,
    QuestionAnsweringExtractive,
    Summarization,
    TextClassification,
    task_template_from_dict,
)
from datasets.utils.py_utils import asdict


_UpperCamelCase    =	{
    '''id''': '''5733be284776f41900661182''',
    '''title''': '''University_of_Notre_Dame''',
    '''context''': '''Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.''',
    '''question''': '''To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?''',
    '''answers''': {'''text''': ['''Saint Bernadette Soubirous'''], '''answer_start''': [515]},
}







@pytest.mark.parametrize(
    "task_cls"				,       [
        AudioClassification,
        AutomaticSpeechRecognition,
        ImageClassification,
        LanguageModeling,
        QuestionAnsweringExtractive,
        Summarization,
        TextClassification,
    ]				,       )
def  lowerCAmelCase__(		lowercase    :      Optional[Any]			)	->      int:
		__snake_case				:			Dict            =    task_cls()
		__snake_case				:			int            =    asdict(lowercase			)
		__snake_case				:			List[str]            =    task_template_from_dict(lowercase			)
		assert task == reloaded




class 			_lowerCamelCase	:



						"""simple docstring"""






						def   UpperCAmelCase						(	self				)    ->				Tuple:






								'''simple docstring'''






								__snake_case				:			Dict            =    LanguageModeling(text_column="input_text"				)
								assert {"input_text": "text"} == task.column_mapping







						def   UpperCAmelCase						(	self				)    ->				Dict:






								'''simple docstring'''






								__snake_case				:			Union[str, Any]            =    Features({"text": Value("string"				)}				)
								__snake_case				:			Optional[int]            =    {"text_column": "input_text"}
								__snake_case				:			int            =    LanguageModeling.from_dict(UpperCAmelCase				)
								assert "language-modeling" == task.task
								assert input_schema == task.input_schema




class 			_lowerCamelCase	(      a      ):



						"""simple docstring"""






						def   UpperCAmelCase						(	self				)    ->				Optional[Any]:






								'''simple docstring'''






								__snake_case				:			Dict            =    sorted(["pos", "neg"]				)







						def   UpperCAmelCase						(	self				)    ->				Union[str, Any]:






								'''simple docstring'''






								__snake_case				:			Dict            =    TextClassification(text_column="input_text"			,  label_column="input_label"				)
								self.assertDictEqual({"input_text": "text", "input_label": "labels"}			,  task.column_mapping				)







						def   UpperCAmelCase						(	self				)    ->				Optional[int]:






								'''simple docstring'''






								__snake_case				:			List[str]            =    Features({"text": Value("string"				)}				)
								# Labels are cast to tuple during `TextClassification.__post_init__`, so we do the same here
								__snake_case				:			Dict            =    Features({"labels": ClassLabel}				)
								__snake_case				:			List[Any]            =    {"text_column": "input_text", "label_column": "input_labels"}
								__snake_case				:			int            =    TextClassification.from_dict(UpperCAmelCase				)
								self.assertEqual("text-classification"			,  task.task				)
								self.assertEqual(UpperCAmelCase			,  task.input_schema				)
								self.assertEqual(UpperCAmelCase			,  task.label_schema				)







						def   UpperCAmelCase						(	self				)    ->				Tuple:






								'''simple docstring'''






								__snake_case				:			List[str]            =    TextClassification(text_column="input_text"			,  label_column="input_label"				)
								self.assertEqual(task.label_schema["labels"]			,  UpperCAmelCase				)
								__snake_case				:			int            =    task.align_with_features(Features({"input_label": ClassLabel(names=self.labels				)}				)				)
								self.assertEqual(task.label_schema["labels"]			,  ClassLabel(names=self.labels				)				)




class 			_lowerCamelCase	(      a      ):



						"""simple docstring"""






						def   UpperCAmelCase						(	self				)    ->				Optional[Any]:






								'''simple docstring'''






								__snake_case				:			Dict            =    QuestionAnsweringExtractive(
								    context_column="input_context"			,  question_column="input_question"			,  answers_column="input_answers"				)
								self.assertDictEqual(
								    {"input_context": "context", "input_question": "question", "input_answers": "answers"}			,  task.column_mapping				)







						def   UpperCAmelCase						(	self				)    ->				int:






								'''simple docstring'''






								__snake_case				:			str            =    Features({"question": Value("string"				), "context": Value("string"				)}				)
								__snake_case				:			Any            =    Features(
								    {
								        "answers": Sequence(
								            {
								                "text": Value("string"				),
								                "answer_start": Value("int32"				),
								            }				)
								    }				)
								__snake_case				:			Tuple            =    {
								    "context_column": "input_input_context",
								    "question_column": "input_question",
								    "answers_column": "input_answers",
								}
								__snake_case				:			Any            =    QuestionAnsweringExtractive.from_dict(UpperCAmelCase				)
								self.assertEqual("question-answering-extractive"			,  task.task				)
								self.assertEqual(UpperCAmelCase			,  task.input_schema				)
								self.assertEqual(UpperCAmelCase			,  task.label_schema				)




class 			_lowerCamelCase	(      a      ):



						"""simple docstring"""






						def   UpperCAmelCase						(	self				)    ->				Tuple:






								'''simple docstring'''






								__snake_case				:			List[Any]            =    Summarization(text_column="input_text"			,  summary_column="input_summary"				)
								self.assertDictEqual({"input_text": "text", "input_summary": "summary"}			,  task.column_mapping				)







						def   UpperCAmelCase						(	self				)    ->				List[str]:






								'''simple docstring'''






								__snake_case				:			Union[str, Any]            =    Features({"text": Value("string"				)}				)
								__snake_case				:			Optional[int]            =    Features({"summary": Value("string"				)}				)
								__snake_case				:			str            =    {"text_column": "input_text", "summary_column": "input_summary"}
								__snake_case				:			str            =    Summarization.from_dict(UpperCAmelCase				)
								self.assertEqual("summarization"			,  task.task				)
								self.assertEqual(UpperCAmelCase			,  task.input_schema				)
								self.assertEqual(UpperCAmelCase			,  task.label_schema				)




class 			_lowerCamelCase	(      a      ):



						"""simple docstring"""






						def   UpperCAmelCase						(	self				)    ->				Optional[int]:






								'''simple docstring'''






								__snake_case				:			Any            =    AutomaticSpeechRecognition(audio_column="input_audio"			,  transcription_column="input_transcription"				)
								self.assertDictEqual({"input_audio": "audio", "input_transcription": "transcription"}			,  task.column_mapping				)







						def   UpperCAmelCase						(	self				)    ->				Tuple:






								'''simple docstring'''






								__snake_case				:			List[Any]            =    Features({"audio": Audio()}				)
								__snake_case				:			str            =    Features({"transcription": Value("string"				)}				)
								__snake_case				:			Tuple            =    {
								    "audio_column": "input_audio",
								    "transcription_column": "input_transcription",
								}
								__snake_case				:			List[str]            =    AutomaticSpeechRecognition.from_dict(UpperCAmelCase				)
								self.assertEqual("automatic-speech-recognition"			,  task.task				)
								self.assertEqual(UpperCAmelCase			,  task.input_schema				)
								self.assertEqual(UpperCAmelCase			,  task.label_schema				)




class 			_lowerCamelCase	(      a      ):



						"""simple docstring"""






						def   UpperCAmelCase						(	self				)    ->				str:






								'''simple docstring'''






								__snake_case				:			Union[str, Any]            =    sorted(["pos", "neg"]				)







						def   UpperCAmelCase						(	self				)    ->				List[Any]:






								'''simple docstring'''






								__snake_case				:			str            =    AudioClassification(audio_column="input_audio"			,  label_column="input_label"				)
								self.assertDictEqual({"input_audio": "audio", "input_label": "labels"}			,  task.column_mapping				)







						def   UpperCAmelCase						(	self				)    ->				List[str]:






								'''simple docstring'''






								__snake_case				:			Tuple            =    Features({"audio": Audio()}				)
								__snake_case				:			str            =    Features({"labels": ClassLabel}				)
								__snake_case				:			List[str]            =    {
								    "audio_column": "input_image",
								    "label_column": "input_label",
								}
								__snake_case				:			Tuple            =    AudioClassification.from_dict(UpperCAmelCase				)
								self.assertEqual("audio-classification"			,  task.task				)
								self.assertEqual(UpperCAmelCase			,  task.input_schema				)
								self.assertEqual(UpperCAmelCase			,  task.label_schema				)







						def   UpperCAmelCase						(	self				)    ->				Any:






								'''simple docstring'''






								__snake_case				:			List[Any]            =    AudioClassification(audio_column="input_audio"			,  label_column="input_label"				)
								self.assertEqual(task.label_schema["labels"]			,  UpperCAmelCase				)
								__snake_case				:			Optional[int]            =    task.align_with_features(Features({"input_label": ClassLabel(names=self.labels				)}				)				)
								self.assertEqual(task.label_schema["labels"]			,  ClassLabel(names=self.labels				)				)




class 			_lowerCamelCase	(      a      ):



						"""simple docstring"""






						def   UpperCAmelCase						(	self				)    ->				str:






								'''simple docstring'''






								__snake_case				:			Any            =    sorted(["pos", "neg"]				)







						def   UpperCAmelCase						(	self				)    ->				List[Any]:






								'''simple docstring'''






								__snake_case				:			Any            =    ImageClassification(image_column="input_image"			,  label_column="input_label"				)
								self.assertDictEqual({"input_image": "image", "input_label": "labels"}			,  task.column_mapping				)







						def   UpperCAmelCase						(	self				)    ->				int:






								'''simple docstring'''






								__snake_case				:			int            =    Features({"image": Image()}				)
								__snake_case				:			str            =    Features({"labels": ClassLabel}				)
								__snake_case				:			Any            =    {
								    "image_column": "input_image",
								    "label_column": "input_label",
								}
								__snake_case				:			List[Any]            =    ImageClassification.from_dict(UpperCAmelCase				)
								self.assertEqual("image-classification"			,  task.task				)
								self.assertEqual(UpperCAmelCase			,  task.input_schema				)
								self.assertEqual(UpperCAmelCase			,  task.label_schema				)







						def   UpperCAmelCase						(	self				)    ->				Union[str, Any]:






								'''simple docstring'''






								__snake_case				:			Optional[int]            =    ImageClassification(image_column="input_image"			,  label_column="input_label"				)
								self.assertEqual(task.label_schema["labels"]			,  UpperCAmelCase				)
								__snake_case				:			Optional[int]            =    task.align_with_features(Features({"input_label": ClassLabel(names=self.labels				)}				)				)
								self.assertEqual(task.label_schema["labels"]			,  ClassLabel(names=self.labels				)				)




class 			_lowerCamelCase	(      a      ):



						"""simple docstring"""






						def   UpperCAmelCase						(	self				)    ->				Dict:






								'''simple docstring'''






								__snake_case				:			List[str]            =    DatasetInfo(task_templates=QuestionAnsweringExtractive()				)
								__snake_case				:			Tuple            =    Dataset.from_dict({k: [v] for k, v in SAMPLE_QUESTION_ANSWERING_EXTRACTIVE.items()}			,  info=UpperCAmelCase				)
								assert isinstance(dataset.info.task_templates			,  UpperCAmelCase				)
								assert len(dataset.info.task_templates				) == 1

								def keep_task(UpperCAmelCase				):
										return x

								def dont_keep_task(UpperCAmelCase				):
										__snake_case				:			Any            =    deepcopy(UpperCAmelCase				)
										__snake_case				:			List[str]            =    0
										return out

								__snake_case				:			List[Any]            =    dataset.map(UpperCAmelCase				)
								assert mapped_dataset.info.task_templates == dataset.info.task_templates
								# reload from cache
								__snake_case				:			int            =    dataset.map(UpperCAmelCase				)
								assert mapped_dataset.info.task_templates == dataset.info.task_templates

								__snake_case				:			int            =    dataset.map(UpperCAmelCase				)
								assert mapped_dataset.info.task_templates == []
								# reload from cache
								__snake_case				:			Any            =    dataset.map(UpperCAmelCase				)
								assert mapped_dataset.info.task_templates == []







						def   UpperCAmelCase						(	self				)    ->				str:






								'''simple docstring'''






								__snake_case				:			Tuple            =    Features({"text": Value("string"				), "label": ClassLabel(names=("pos", "neg")				)}				)
								__snake_case				:			Optional[Any]            =    TextClassification(text_column="text"			,  label_column="label"				)
								__snake_case				:			Union[str, Any]            =    DatasetInfo(features=UpperCAmelCase			,  task_templates=UpperCAmelCase				)
								__snake_case				:			str            =    Dataset.from_dict({"text": ["A sentence."], "label": ["pos"]}			,  info=UpperCAmelCase				)

								def process(UpperCAmelCase				):
										return example

								__snake_case				:			Optional[int]            =    dataset.remove_columns("label"				)
								__snake_case				:			Tuple            =    modified_dataset.map(UpperCAmelCase				)
								assert mapped_dataset.info.task_templates == []








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

from ...utils import is_tf_available, is_torch_available, logging


_UpperCamelCase    =	logging.get_logger(__name__)




@dataclass
class 			_lowerCamelCase	:



      """simple docstring"""






      UpperCAmelCase_  : str
      UpperCAmelCase_  : str
      UpperCAmelCase_  : Optional[str]				      =None
      UpperCAmelCase_  : Optional[str]				      =None







      def   UpperCAmelCase						(	self				)    ->				List[Any]:






        '''simple docstring'''






        return json.dumps(dataclasses.asdict(self				)			,  indent=2				) + "\n"




@dataclass(frozen=a      )
class 			_lowerCamelCase	:



      """simple docstring"""






      UpperCAmelCase_  : List[int]
      UpperCAmelCase_  : Optional[List[int]]				      =None
      UpperCAmelCase_  : Optional[List[int]]				      =None
      UpperCAmelCase_  : Optional[Union[int, float]]				      =None







      def   UpperCAmelCase						(	self				)    ->				Tuple:






        '''simple docstring'''






        return json.dumps(dataclasses.asdict(self				)				) + "\n"




class 			_lowerCamelCase	:



      """simple docstring"""






      def   UpperCAmelCase						(	self			,  UpperCAmelCase				)    ->				Dict:






        '''simple docstring'''






        raise NotImplementedError()







      def   UpperCAmelCase						(	self			,  UpperCAmelCase				)    ->				int:






        '''simple docstring'''






        raise NotImplementedError()







      def   UpperCAmelCase						(	self			,  UpperCAmelCase				)    ->				List[str]:






        '''simple docstring'''






        raise NotImplementedError()







      def   UpperCAmelCase						(	self			,  UpperCAmelCase				)    ->				str:






        '''simple docstring'''






        raise NotImplementedError()







      def   UpperCAmelCase						(	self				)    ->				str:






        '''simple docstring'''






        raise NotImplementedError()







      def   UpperCAmelCase						(	self			,  UpperCAmelCase				)    ->				Tuple:






        '''simple docstring'''






        if len(self.get_labels()				) > 1:
          __snake_case				:			Any            =    self.get_labels()[int(example.label				)]
        return example







      @classmethod
      def   UpperCAmelCase						(	cls			,  UpperCAmelCase			,  UpperCAmelCase=None				)    ->				str:






        '''simple docstring'''






        with open(UpperCAmelCase			,  "r"			,  encoding="utf-8-sig"				) as f:
          return list(csv.reader(UpperCAmelCase			,  delimiter="\t"			,  quotechar=UpperCAmelCase				)				)




class 			_lowerCamelCase	(      a      ):



      """simple docstring"""






      def __init__(	self			,  UpperCAmelCase=None			,  UpperCAmelCase=None			,  UpperCAmelCase="classification"			,  UpperCAmelCase=False				)    ->				str:






        '''simple docstring'''






        __snake_case				:			Optional[Any]            =    [] if labels is None else labels
        __snake_case				:			str            =    [] if examples is None else examples
        __snake_case				:			str            =    mode
        __snake_case				:			Tuple            =    verbose







      def __len__(	self				)    ->				Dict:






        '''simple docstring'''






        return len(self.examples				)







      def __getitem__(	self			,  UpperCAmelCase				)    ->				Tuple:






        '''simple docstring'''






        if isinstance(UpperCAmelCase			,  UpperCAmelCase				):
          return SingleSentenceClassificationProcessor(labels=self.labels			,  examples=self.examples[idx]				)
        return self.examples[idx]







      @classmethod
      def   UpperCAmelCase						(	cls			,  UpperCAmelCase			,  UpperCAmelCase=""			,  UpperCAmelCase=0			,  UpperCAmelCase=1			,  UpperCAmelCase=None			,  UpperCAmelCase=False			,  **UpperCAmelCase				)    ->				int:






        '''simple docstring'''






        __snake_case				:			Dict            =    cls(**UpperCAmelCase				)
        processor.add_examples_from_csv(
            UpperCAmelCase			,  split_name=UpperCAmelCase			,  column_label=UpperCAmelCase			,  column_text=UpperCAmelCase			,  column_id=UpperCAmelCase			,  skip_first_row=UpperCAmelCase			,  overwrite_labels=UpperCAmelCase			,  overwrite_examples=UpperCAmelCase			,  )
        return processor







      @classmethod
      def   UpperCAmelCase						(	cls			,  UpperCAmelCase			,  UpperCAmelCase=None			,  **UpperCAmelCase				)    ->				Tuple:






        '''simple docstring'''






        __snake_case				:			Dict            =    cls(**UpperCAmelCase				)
        processor.add_examples(UpperCAmelCase			,  labels=UpperCAmelCase				)
        return processor







      def   UpperCAmelCase						(	self			,  UpperCAmelCase			,  UpperCAmelCase=""			,  UpperCAmelCase=0			,  UpperCAmelCase=1			,  UpperCAmelCase=None			,  UpperCAmelCase=False			,  UpperCAmelCase=False			,  UpperCAmelCase=False			,  )    ->				Dict:






        '''simple docstring'''






        __snake_case				:			Optional[int]            =    self._read_tsv(UpperCAmelCase				)
        if skip_first_row:
          __snake_case				:			str            =    lines[1:]
        __snake_case				:			List[str]            =    []
        __snake_case				:			List[str]            =    []
        __snake_case				:			Optional[int]            =    []
        for i, line in enumerate(UpperCAmelCase				):
          texts.append(line[column_text]				)
          labels.append(line[column_label]				)
          if column_id is not None:
            ids.append(line[column_id]				)
          else:
            __snake_case				:			List[Any]            =    F"""{split_name}-{i}""" if split_name else str(UpperCAmelCase				)
            ids.append(UpperCAmelCase				)

        return self.add_examples(
            UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase			,  overwrite_labels=UpperCAmelCase			,  overwrite_examples=UpperCAmelCase				)







      def   UpperCAmelCase						(	self			,  UpperCAmelCase			,  UpperCAmelCase=None			,  UpperCAmelCase=None			,  UpperCAmelCase=False			,  UpperCAmelCase=False				)    ->				Optional[Any]:






        '''simple docstring'''






        if labels is not None and len(UpperCAmelCase				) != len(UpperCAmelCase				):
          raise ValueError(
              F"""Text and labels have mismatched lengths {len(UpperCAmelCase				)} and {len(UpperCAmelCase				)}"""				)
        if ids is not None and len(UpperCAmelCase				) != len(UpperCAmelCase				):
          raise ValueError(F"""Text and ids have mismatched lengths {len(UpperCAmelCase				)} and {len(UpperCAmelCase				)}"""				)
        if ids is None:
          __snake_case				:			str            =    [None] * len(UpperCAmelCase				)
        if labels is None:
          __snake_case				:			Dict            =    [None] * len(UpperCAmelCase				)
        __snake_case				:			Dict            =    []
        __snake_case				:			Any            =    set()
        for text_or_text_and_label, label, guid in zip(UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase				):
          if isinstance(UpperCAmelCase			,  (tuple, list)				) and label is None:
            __snake_case      ,				__snake_case				:			int            =    text_or_text_and_label
          else:
            __snake_case				:			Dict            =    text_or_text_and_label
          added_labels.add(UpperCAmelCase				)
          examples.append(InputExample(guid=UpperCAmelCase			,  text_a=UpperCAmelCase			,  text_b=UpperCAmelCase			,  label=UpperCAmelCase				)				)

        # Update examples
        if overwrite_examples:
          __snake_case				:			Union[str, Any]            =    examples
        else:
          self.examples.extend(UpperCAmelCase				)

        # Update labels
        if overwrite_labels:
          __snake_case				:			Optional[Any]            =    list(UpperCAmelCase				)
        else:
          __snake_case				:			Dict            =    list(set(self.labels				).union(UpperCAmelCase				)				)

        return self.examples







      def   UpperCAmelCase						(	self			,  UpperCAmelCase			,  UpperCAmelCase=None			,  UpperCAmelCase=False			,  UpperCAmelCase=0			,  UpperCAmelCase=True			,  UpperCAmelCase=None			,  )    ->				str:






        '''simple docstring'''






        if max_length is None:
          __snake_case				:			Tuple            =    tokenizer.max_len

        __snake_case				:			Union[str, Any]            =    {label: i for i, label in enumerate(self.labels				)}

        __snake_case				:			Union[str, Any]            =    []
        for ex_index, example in enumerate(self.examples				):
          if ex_index % 10000 == 0:
            logger.info(F"""Tokenizing example {ex_index}"""				)

          __snake_case				:			Union[str, Any]            =    tokenizer.encode(
              example.text_a			,  add_special_tokens=UpperCAmelCase			,  max_length=min(UpperCAmelCase			,  tokenizer.max_len				)			,  )
          all_input_ids.append(UpperCAmelCase				)

        __snake_case				:			Union[str, Any]            =    max(len(UpperCAmelCase				) for input_ids in all_input_ids				)

        __snake_case				:			str            =    []
        for ex_index, (input_ids, example) in enumerate(zip(UpperCAmelCase			,  self.examples				)				):
          if ex_index % 10000 == 0:
            logger.info(F"""Writing example {ex_index}/{len(self.examples				)}"""				)
          # The mask has 1 for real tokens and 0 for padding tokens. Only real
          # tokens are attended to.
          __snake_case				:			Optional[Any]            =    [1 if mask_padding_with_zero else 0] * len(UpperCAmelCase				)

          # Zero-pad up to the sequence length.
          __snake_case				:			List[str]            =    batch_length - len(UpperCAmelCase				)
          if pad_on_left:
            __snake_case				:			List[Any]            =    ([pad_token] * padding_length) + input_ids
            __snake_case				:			str            =    ([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
          else:
            __snake_case				:			Any            =    input_ids + ([pad_token] * padding_length)
            __snake_case				:			List[str]            =    attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)

          if len(UpperCAmelCase				) != batch_length:
            raise ValueError(F"""Error with input length {len(UpperCAmelCase				)} vs {batch_length}"""				)
          if len(UpperCAmelCase				) != batch_length:
            raise ValueError(F"""Error with input length {len(UpperCAmelCase				)} vs {batch_length}"""				)

          if self.mode == "classification":
            __snake_case				:			str            =    label_map[example.label]
          elif self.mode == "regression":
            __snake_case				:			List[str]            =    float(example.label				)
          else:
            raise ValueError(self.mode				)

          if ex_index < 5 and self.verbose:
            logger.info("*** Example ***"				)
            logger.info(F"""guid: {example.guid}"""				)
            logger.info(F"""input_ids: {" ".join([str(UpperCAmelCase				) for x in input_ids]				)}"""				)
            logger.info(F"""attention_mask: {" ".join([str(UpperCAmelCase				) for x in attention_mask]				)}"""				)
            logger.info(F"""label: {example.label} (id = {label})"""				)

          features.append(InputFeatures(input_ids=UpperCAmelCase			,  attention_mask=UpperCAmelCase			,  label=UpperCAmelCase				)				)

        if return_tensors is None:
          return features
        elif return_tensors == "tf":
          if not is_tf_available():
            raise RuntimeError("return_tensors set to 'tf' but TensorFlow 2.0 can't be imported"				)
          import tensorflow as tf

          def gen():
            for ex in features:
              yield ({"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label)

          __snake_case				:			int            =    tf.data.Dataset.from_generator(
              UpperCAmelCase			,  ({"input_ids": tf.intaa, "attention_mask": tf.intaa}, tf.intaa)			,  ({"input_ids": tf.TensorShape([None]				), "attention_mask": tf.TensorShape([None]				)}, tf.TensorShape([]				))			,  )
          return dataset
        elif return_tensors == "pt":
          if not is_torch_available():
            raise RuntimeError("return_tensors set to 'pt' but PyTorch can't be imported"				)
          import torch
          from torch.utils.data import TensorDataset

          __snake_case				:			int            =    torch.tensor([f.input_ids for f in features]			,  dtype=torch.long				)
          __snake_case				:			int            =    torch.tensor([f.attention_mask for f in features]			,  dtype=torch.long				)
          if self.mode == "classification":
            __snake_case				:			List[Any]            =    torch.tensor([f.label for f in features]			,  dtype=torch.long				)
          elif self.mode == "regression":
            __snake_case				:			List[Any]            =    torch.tensor([f.label for f in features]			,  dtype=torch.float				)

          __snake_case				:			List[Any]            =    TensorDataset(UpperCAmelCase			,  UpperCAmelCase			,  UpperCAmelCase				)
          return dataset
        else:
          raise ValueError("return_tensors should be one of 'tf' or 'pt'"				)

