
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__						:	Dict	     = {
    '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__				(   a ) ->  Any:
   _A:    Optional[Any]						     =			task_cls()
   _A:    Tuple						     =			asdict(a )
   _A:    Tuple						     =			task_template_from_dict(a )
   assert task == reloaded






class UpperCAmelCase  :





   '''simple docstring'''


   def 					__magic_name__							(   self	:				Optional[int]	):

      """simple docstring"""
      _A:    List[str]						     =			LanguageModeling(text_column='''input_text'''	)
      assert {"input_text": "text"} == task.column_mapping


   def 					__magic_name__							(   self	:				List[Any]	):

      """simple docstring"""
      _A:    List[Any]						     =			Features({'''text''': Value('''string'''	)}	)
      _A:    Optional[Any]						     =			{'''text_column''': '''input_text'''}
      _A:    Optional[Any]						     =			LanguageModeling.from_dict(lowerCAmelCase_	)
      assert "language-modeling" == task.task
      assert input_schema == task.input_schema






class UpperCAmelCase  (		SCREAMING_SNAKE_CASE__  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				Dict	):

      """simple docstring"""
      _A:    Optional[Any]						     =			sorted(['''pos''', '''neg''']	)


   def 					__magic_name__							(   self	:				str	):

      """simple docstring"""
      _A:    List[str]						     =			TextClassification(text_column='''input_text'''     ,     label_column='''input_label'''	)
      self.assertDictEqual({'''input_text''': '''text''', '''input_label''': '''labels'''}     ,     task.column_mapping	)


   def 					__magic_name__							(   self	:				List[str]	):

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


   def 					__magic_name__							(   self	:				Optional[int]	):

      """simple docstring"""
      _A:    Tuple						     =			TextClassification(text_column='''input_text'''     ,     label_column='''input_label'''	)
      self.assertEqual(task.label_schema['''labels''']     ,     lowerCAmelCase_	)
      _A:    Optional[int]						     =			task.align_with_features(Features({'''input_label''': ClassLabel(names=self.labels	)}	)	)
      self.assertEqual(task.label_schema['''labels''']     ,     ClassLabel(names=self.labels	)	)






class UpperCAmelCase  (		SCREAMING_SNAKE_CASE__  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				List[Any]	):

      """simple docstring"""
      _A:    Any						     =			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 					__magic_name__							(   self	:				Any	):

      """simple docstring"""
      _A:    Any						     =			Features({'''question''': Value('''string'''	), '''context''': Value('''string'''	)}	)
      _A:    str						     =			Features(
          {
              '''answers''': Sequence(
                  {
                      '''text''': Value('''string'''	),
                      '''answer_start''': Value('''int32'''	),
                  }	)
          }	)
      _A:    Optional[int]						     =			{
          '''context_column''': '''input_input_context''',
          '''question_column''': '''input_question''',
          '''answers_column''': '''input_answers''',
      }
      _A:    List[str]						     =			QuestionAnsweringExtractive.from_dict(lowerCAmelCase_	)
      self.assertEqual('''question-answering-extractive'''     ,     task.task	)
      self.assertEqual(lowerCAmelCase_     ,     task.input_schema	)
      self.assertEqual(lowerCAmelCase_     ,     task.label_schema	)






class UpperCAmelCase  (		SCREAMING_SNAKE_CASE__  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				Union[str, Any]	):

      """simple docstring"""
      _A:    Any						     =			Summarization(text_column='''input_text'''     ,     summary_column='''input_summary'''	)
      self.assertDictEqual({'''input_text''': '''text''', '''input_summary''': '''summary'''}     ,     task.column_mapping	)


   def 					__magic_name__							(   self	:				str	):

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






class UpperCAmelCase  (		SCREAMING_SNAKE_CASE__  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				Any	):

      """simple docstring"""
      _A:    Union[str, Any]						     =			AutomaticSpeechRecognition(audio_column='''input_audio'''     ,     transcription_column='''input_transcription'''	)
      self.assertDictEqual({'''input_audio''': '''audio''', '''input_transcription''': '''transcription'''}     ,     task.column_mapping	)


   def 					__magic_name__							(   self	:				str	):

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






class UpperCAmelCase  (		SCREAMING_SNAKE_CASE__  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				Optional[int]	):

      """simple docstring"""
      _A:    List[Any]						     =			sorted(['''pos''', '''neg''']	)


   def 					__magic_name__							(   self	:				Tuple	):

      """simple docstring"""
      _A:    int						     =			AudioClassification(audio_column='''input_audio'''     ,     label_column='''input_label'''	)
      self.assertDictEqual({'''input_audio''': '''audio''', '''input_label''': '''labels'''}     ,     task.column_mapping	)


   def 					__magic_name__							(   self	:				int	):

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


   def 					__magic_name__							(   self	:				Optional[int]	):

      """simple docstring"""
      _A:    Optional[Any]						     =			AudioClassification(audio_column='''input_audio'''     ,     label_column='''input_label'''	)
      self.assertEqual(task.label_schema['''labels''']     ,     lowerCAmelCase_	)
      _A:    List[str]						     =			task.align_with_features(Features({'''input_label''': ClassLabel(names=self.labels	)}	)	)
      self.assertEqual(task.label_schema['''labels''']     ,     ClassLabel(names=self.labels	)	)






class UpperCAmelCase  (		SCREAMING_SNAKE_CASE__  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				Dict	):

      """simple docstring"""
      _A:    List[Any]						     =			sorted(['''pos''', '''neg''']	)


   def 					__magic_name__							(   self	:				Union[str, Any]	):

      """simple docstring"""
      _A:    Union[str, Any]						     =			ImageClassification(image_column='''input_image'''     ,     label_column='''input_label'''	)
      self.assertDictEqual({'''input_image''': '''image''', '''input_label''': '''labels'''}     ,     task.column_mapping	)


   def 					__magic_name__							(   self	:				Optional[int]	):

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


   def 					__magic_name__							(   self	:				Tuple	):

      """simple docstring"""
      _A:    int						     =			ImageClassification(image_column='''input_image'''     ,     label_column='''input_label'''	)
      self.assertEqual(task.label_schema['''labels''']     ,     lowerCAmelCase_	)
      _A:    Union[str, Any]						     =			task.align_with_features(Features({'''input_label''': ClassLabel(names=self.labels	)}	)	)
      self.assertEqual(task.label_schema['''labels''']     ,     ClassLabel(names=self.labels	)	)






class UpperCAmelCase  (		SCREAMING_SNAKE_CASE__  ):





   '''simple docstring'''


   def 					__magic_name__							(   self	:				Dict	):

      """simple docstring"""
      _A:    Union[str, Any]						     =			DatasetInfo(task_templates=QuestionAnsweringExtractive()	)
      _A:    Any						     =			Dataset.from_dict({k: [v] for k, v in SAMPLE_QUESTION_ANSWERING_EXTRACTIVE.items()}     ,     info=lowerCAmelCase_	)
      assert isinstance(dataset.info.task_templates     ,     lowerCAmelCase_	)
      assert len(dataset.info.task_templates	) == 1

      def keep_task(lowerCAmelCase_	:				int	):
         return x

      def dont_keep_task(lowerCAmelCase_	:				str	):
         _A:    Tuple						     =			deepcopy(lowerCAmelCase_	)
         _A:    Optional[Any]						     =			0
         return out

      _A:    Any						     =			dataset.map(lowerCAmelCase_	)
      assert mapped_dataset.info.task_templates == dataset.info.task_templates
      # reload from cache
      _A:    List[str]						     =			dataset.map(lowerCAmelCase_	)
      assert mapped_dataset.info.task_templates == dataset.info.task_templates

      _A:    Dict						     =			dataset.map(lowerCAmelCase_	)
      assert mapped_dataset.info.task_templates == []
      # reload from cache
      _A:    str						     =			dataset.map(lowerCAmelCase_	)
      assert mapped_dataset.info.task_templates == []


   def 					__magic_name__							(   self	:				Optional[Any]	):

      """simple docstring"""
      _A:    List[Any]						     =			Features({'''text''': Value('''string'''	), '''label''': ClassLabel(names=('''pos''', '''neg''')	)}	)
      _A:    Dict						     =			TextClassification(text_column='''text'''     ,     label_column='''label'''	)
      _A:    int						     =			DatasetInfo(features=lowerCAmelCase_     ,     task_templates=lowerCAmelCase_	)
      _A:    Dict						     =			Dataset.from_dict({'''text''': ['''A sentence.'''], '''label''': ['''pos''']}     ,     info=lowerCAmelCase_	)

      def process(lowerCAmelCase_	:				List[Any]	):
         return example

      _A:    int						     =			dataset.remove_columns('''label'''	)
      _A:    List[Any]						     =			modified_dataset.map(lowerCAmelCase_	)
      assert mapped_dataset.info.task_templates == []





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


UpperCAmelCase__						:	Any	     = {
    'configuration_decision_transformer': [
        'DECISION_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'DecisionTransformerConfig',
    ],
}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   UpperCAmelCase__						:	Optional[Any]	     = [
       'DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
       'DecisionTransformerGPT2Model',
       'DecisionTransformerGPT2PreTrainedModel',
       'DecisionTransformerModel',
       'DecisionTransformerPreTrainedModel',
   ]


if TYPE_CHECKING:
   from .configuration_decision_transformer import (
       DECISION_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
       DecisionTransformerConfig,
   )

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_decision_transformer import (
          DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
          DecisionTransformerGPTaModel,
          DecisionTransformerGPTaPreTrainedModel,
          DecisionTransformerModel,
          DecisionTransformerPreTrainedModel,
      )


else:
   import sys

   UpperCAmelCase__						:	Optional[Any]	     = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



