


"""simple docstring"""






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


SCREAMING_SNAKE_CASE__				:  Union[str, Any]						= {
    "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        A_					(					UpperCAmelCase__				)     ->     List[str]:
   a   :	int						=    task_cls()
   a   :	Optional[int]						=    asdict(UpperCAmelCase__				)
   a   :	Any						=    task_template_from_dict(UpperCAmelCase__				)
   assert task == reloaded


class 	A_       :





  """simple docstring"""

  def 							lowercase_     (						self						)				->					int:
     a   :	List[Any]						=    LanguageModeling(text_column='input_text'						)
     assert {"input_text": "text"} == task.column_mapping






  def 							lowercase_     (						self						)				->					Optional[int]:
     a   :	Optional[int]						=    Features({'text': Value('string'						)}						)
     a   :	int						=    {'text_column': 'input_text'}
     a   :	int						=    LanguageModeling.from_dict(__UpperCAmelCase						)
     assert "language-modeling" == task.task
     assert input_schema == task.input_schema



class 	A_       (     _UpperCAmelCase   ):





  """simple docstring"""

  def 							lowercase_     (						self						)				->					Optional[int]:
     a   :	List[Any]						=    sorted(['pos', 'neg']						)
  def 							lowercase_     (						self						)				->					List[Any]:
     a   :	str						=    TextClassification(text_column='input_text'  ,					label_column='input_label'						)
     self.assertDictEqual({'input_text': 'text', 'input_label': 'labels'}  ,					task.column_mapping						)
  def 							lowercase_     (						self						)				->					Any:
     a   :	Tuple						=    Features({'text': Value('string'						)}						)
     # Labels are cast to tuple during `TextClassification.__post_init__`, so we do the same here
     a   :	Dict						=    Features({'labels': ClassLabel}						)
     a   :	Tuple						=    {'text_column': 'input_text', 'label_column': 'input_labels'}
     a   :	Dict						=    TextClassification.from_dict(__UpperCAmelCase						)
     self.assertEqual('text-classification'  ,					task.task						)
     self.assertEqual(__UpperCAmelCase  ,					task.input_schema						)
     self.assertEqual(__UpperCAmelCase  ,					task.label_schema						)






  def 							lowercase_     (						self						)				->					int:
     a   :	str						=    TextClassification(text_column='input_text'  ,					label_column='input_label'						)
     self.assertEqual(task.label_schema['labels']  ,					__UpperCAmelCase						)
     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 	A_       (     _UpperCAmelCase   ):





  """simple docstring"""

  def 							lowercase_     (						self						)				->					str:
     a   :	List[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 							lowercase_     (						self						)				->					Optional[int]:
     a   :	int						=    Features({'question': Value('string'						), 'context': Value('string'						)}						)
     a   :	Any						=    Features(
         {
             'answers': Sequence(
                 {
                     'text': Value('string'						),
                     'answer_start': Value('int32'						),
                 }						)
         }						)
     a   :	Dict						=    {
         'context_column': 'input_input_context',
         'question_column': 'input_question',
         'answers_column': 'input_answers',
     }
     a   :	str						=    QuestionAnsweringExtractive.from_dict(__UpperCAmelCase						)
     self.assertEqual('question-answering-extractive'  ,					task.task						)
     self.assertEqual(__UpperCAmelCase  ,					task.input_schema						)
     self.assertEqual(__UpperCAmelCase  ,					task.label_schema						)



class 	A_       (     _UpperCAmelCase   ):





  """simple docstring"""

  def 							lowercase_     (						self						)				->					Union[str, Any]:
     a   :	List[Any]						=    Summarization(text_column='input_text'  ,					summary_column='input_summary'						)
     self.assertDictEqual({'input_text': 'text', 'input_summary': 'summary'}  ,					task.column_mapping						)






  def 							lowercase_     (						self						)				->					List[Any]:
     a   :	Union[str, Any]						=    Features({'text': Value('string'						)}						)
     a   :	List[str]						=    Features({'summary': Value('string'						)}						)
     a   :	Optional[Any]						=    {'text_column': 'input_text', 'summary_column': 'input_summary'}
     a   :	Optional[Any]						=    Summarization.from_dict(__UpperCAmelCase						)
     self.assertEqual('summarization'  ,					task.task						)
     self.assertEqual(__UpperCAmelCase  ,					task.input_schema						)
     self.assertEqual(__UpperCAmelCase  ,					task.label_schema						)



class 	A_       (     _UpperCAmelCase   ):





  """simple docstring"""

  def 							lowercase_     (						self						)				->					Optional[int]:
     a   :	List[Any]						=    AutomaticSpeechRecognition(audio_column='input_audio'  ,					transcription_column='input_transcription'						)
     self.assertDictEqual({'input_audio': 'audio', 'input_transcription': 'transcription'}  ,					task.column_mapping						)






  def 							lowercase_     (						self						)				->					Union[str, Any]:
     a   :	List[str]						=    Features({'audio': Audio()}						)
     a   :	Optional[int]						=    Features({'transcription': Value('string'						)}						)
     a   :	Tuple						=    {
         'audio_column': 'input_audio',
         'transcription_column': 'input_transcription',
     }
     a   :	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 	A_       (     _UpperCAmelCase   ):





  """simple docstring"""

  def 							lowercase_     (						self						)				->					str:
     a   :	Any						=    sorted(['pos', 'neg']						)
  def 							lowercase_     (						self						)				->					Union[str, Any]:
     a   :	Union[str, Any]						=    AudioClassification(audio_column='input_audio'  ,					label_column='input_label'						)
     self.assertDictEqual({'input_audio': 'audio', 'input_label': 'labels'}  ,					task.column_mapping						)
  def 							lowercase_     (						self						)				->					List[Any]:
     a   :	int						=    Features({'audio': Audio()}						)
     a   :	Dict						=    Features({'labels': ClassLabel}						)
     a   :	List[str]						=    {
         'audio_column': 'input_image',
         'label_column': 'input_label',
     }
     a   :	List[Any]						=    AudioClassification.from_dict(__UpperCAmelCase						)
     self.assertEqual('audio-classification'  ,					task.task						)
     self.assertEqual(__UpperCAmelCase  ,					task.input_schema						)
     self.assertEqual(__UpperCAmelCase  ,					task.label_schema						)






  def 							lowercase_     (						self						)				->					Union[str, Any]:
     a   :	Optional[int]						=    AudioClassification(audio_column='input_audio'  ,					label_column='input_label'						)
     self.assertEqual(task.label_schema['labels']  ,					__UpperCAmelCase						)
     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 	A_       (     _UpperCAmelCase   ):





  """simple docstring"""

  def 							lowercase_     (						self						)				->					Tuple:
     a   :	Tuple						=    sorted(['pos', 'neg']						)
  def 							lowercase_     (						self						)				->					Optional[int]:
     a   :	Any						=    ImageClassification(image_column='input_image'  ,					label_column='input_label'						)
     self.assertDictEqual({'input_image': 'image', 'input_label': 'labels'}  ,					task.column_mapping						)
  def 							lowercase_     (						self						)				->					str:
     a   :	Any						=    Features({'image': Image()}						)
     a   :	Any						=    Features({'labels': ClassLabel}						)
     a   :	List[Any]						=    {
         'image_column': 'input_image',
         'label_column': 'input_label',
     }
     a   :	List[str]						=    ImageClassification.from_dict(__UpperCAmelCase						)
     self.assertEqual('image-classification'  ,					task.task						)
     self.assertEqual(__UpperCAmelCase  ,					task.input_schema						)
     self.assertEqual(__UpperCAmelCase  ,					task.label_schema						)






  def 							lowercase_     (						self						)				->					List[str]:
     a   :	List[Any]						=    ImageClassification(image_column='input_image'  ,					label_column='input_label'						)
     self.assertEqual(task.label_schema['labels']  ,					__UpperCAmelCase						)
     a   :	Dict						=    task.align_with_features(Features({'input_label': ClassLabel(names=self.labels						)}						)						)
     self.assertEqual(task.label_schema['labels']  ,					ClassLabel(names=self.labels						)						)



class 	A_       (     _UpperCAmelCase   ):





  """simple docstring"""

  def 							lowercase_     (						self						)				->					Optional[Any]:
     a   :	str						=    DatasetInfo(task_templates=QuestionAnsweringExtractive()						)
     a   :	List[Any]						=    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						):
        a   :	Optional[Any]						=    deepcopy(__UpperCAmelCase						)
        a   :	int						=    0
        return out

     a   :	List[str]						=    dataset.map(__UpperCAmelCase						)
     assert mapped_dataset.info.task_templates == dataset.info.task_templates
     # reload from cache
     a   :	List[str]						=    dataset.map(__UpperCAmelCase						)
     assert mapped_dataset.info.task_templates == dataset.info.task_templates

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






  def 							lowercase_     (						self						)				->					str:
     a   :	Union[str, Any]						=    Features({'text': Value('string'						), 'label': ClassLabel(names=('pos', 'neg')						)}						)
     a   :	Tuple						=    TextClassification(text_column='text'  ,					label_column='label'						)
     a   :	List[Any]						=    DatasetInfo(features=__UpperCAmelCase  ,					task_templates=__UpperCAmelCase						)
     a   :	Tuple						=    Dataset.from_dict({'text': ['A sentence.'], 'label': ['pos']}  ,					info=__UpperCAmelCase						)

     def process(__UpperCAmelCase						):
        return example

     a   :	str						=    dataset.remove_columns('label'						)
     a   :	Any						=    modified_dataset.map(__UpperCAmelCase						)
     assert mapped_dataset.info.task_templates == []









"""simple docstring"""






import re
import tempfile
import unittest
from pathlib import Path

from datasets.utils.metadata import DatasetMetadata




def        A_					(					UpperCAmelCase__				)     ->     str:
   a   :	Optional[int]						=    min(re.search('^ +'       ,   UpperCAmelCase__				).end() if t.startswith(' '				) else 0 for t in string.splitlines()				)
   return "\n".join([line[indent_level:] for line in string.splitlines() if indent_level < len(UpperCAmelCase__				)]				)


SCREAMING_SNAKE_CASE__				:  Union[str, Any]						= "\\n---\nlanguage:\n- zh\n- en\ntask_ids:\n- sentiment-classification\n---\n# Begin of markdown\n\nSome cool dataset card\n"

SCREAMING_SNAKE_CASE__				:  List[str]						= "\\n---\n---\n# Begin of markdown\n\nSome cool dataset card\n"


SCREAMING_SNAKE_CASE__				:  Dict						= "\\n# Begin of markdown\n\nSome cool dataset card\n"


class 	A_       (     unittest.TestCase   ):





  """simple docstring"""

  def 							lowercase_     (						self						)				->					Any:
     with tempfile.TemporaryDirectory() as tmp_dir:
        a   :	Optional[Any]						=    Path(__UpperCAmelCase						) / 'README.md'
        with open(__UpperCAmelCase  ,					'w+'						) as readme_file:
           readme_file.write(__UpperCAmelCase						)
        a   :	str						=    DatasetMetadata.from_readme(__UpperCAmelCase						)
        self.assertDictEqual(__UpperCAmelCase  ,					{'language': ['zh', 'en'], 'task_ids': ['sentiment-classification']}						)

        with open(__UpperCAmelCase  ,					'w+'						) as readme_file:
           readme_file.write(__UpperCAmelCase						)
        a   :	Optional[Any]						=    DatasetMetadata.from_readme(__UpperCAmelCase						)
        self.assertDictEqual(__UpperCAmelCase  ,					{}						)

        with open(__UpperCAmelCase  ,					'w+'						) as readme_file:
           readme_file.write(__UpperCAmelCase						)
        a   :	str						=    DatasetMetadata.from_readme(__UpperCAmelCase						)
        self.assertEqual(__UpperCAmelCase  ,					{}						)






  def 							lowercase_     (						self						)				->					Tuple:
     a   :	List[str]						=    _dedent(
         '\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - question-answering\n            task_ids:\n            - open-domain-qa\n            '						)
     assert DatasetMetadata.from_yaml_string(__UpperCAmelCase						)

     a   :	Optional[Any]						=    _dedent(
         '\\n            annotations_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - question-answering\n            task_ids:\n            - open-domain-qa\n            task_ids:\n            - open-domain-qa\n            '						)
     with self.assertRaises(__UpperCAmelCase						):
        DatasetMetadata.from_yaml_string(__UpperCAmelCase						)

     a   :	List[Any]						=    _dedent(
         '\\n            annotations_creators:\n            - found\n            language_creators:\n            - found\n            language:\n            - en\n            license:\n            - unknown\n            multilinguality:\n            - monolingual\n            pretty_name: Test Dataset\n            size_categories:\n            - 10K<n<100K\n            source_datasets:\n            - extended|other-yahoo-webscope-l6\n            task_categories:\n            - text-classification\n            task_ids:\n            - multi-class-classification\n            paperswithcode_id:\n            - squad\n            configs:\n            - en\n            train-eval-index:\n            - config: en\n              task: text-classification\n              task_id: multi_class_classification\n              splits:\n                train_split: train\n                eval_split: test\n              col_mapping:\n                text: text\n                label: target\n              metrics:\n                - type: accuracy\n                  name: Accuracy\n            extra_gated_prompt: |\n              By clicking on “Access repository” below, you also agree to ImageNet Terms of Access:\n              [RESEARCHER_FULLNAME] (the "Researcher") has requested permission to use the ImageNet database (the "Database") at Princeton University and Stanford University. In exchange for such permission, Researcher hereby agrees to the following terms and conditions:\n              1. Researcher shall use the Database only for non-commercial research and educational purposes.\n            extra_gated_fields:\n              Company: text\n              Country: text\n              I agree to use this model for non-commerical use ONLY: checkbox\n            '						)
     assert DatasetMetadata.from_yaml_string(__UpperCAmelCase						)





