
"""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


UpperCamelCase      :							Optional[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": [5_1_5]},
}






@pytest.mark.parametrize(
    'task_cls'							,   [
        AudioClassification,
        AutomaticSpeechRecognition,
        ImageClassification,
        LanguageModeling,
        QuestionAnsweringExtractive,
        Summarization,
        TextClassification,
    ]							,   )
def 				A		(							snake_case :int						) ->      str:
 __UpperCamelCase							    =			task_cls()
 __UpperCamelCase							    =			asdict(snake_case						)
 __UpperCamelCase							    =			task_template_from_dict(snake_case						)
 assert task == reloaded

class __lowerCAmelCase						:





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			LanguageModeling(text_column='input_text' )
      assert {"input_text": "text"} == task.column_mapping







     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			Features({'text': Value('string' )} )
      __UpperCamelCase							    =			{'text_column': 'input_text'}
      __UpperCamelCase							    =			LanguageModeling.from_dict(__UpperCAmelCase )
      assert "language-modeling" == task.task
      assert input_schema == task.input_schema


class __lowerCAmelCase						( __SCREAMING_SNAKE_CASE     ):





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			sorted(['pos', 'neg'] )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			TextClassification(text_column='input_text'      ,	label_column='input_label' )
      self.assertDictEqual({'input_text': 'text', 'input_label': 'labels'}      ,	task.column_mapping )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			Features({'text': Value('string' )} )
      # Labels are cast to tuple during `TextClassification.__post_init__`, so we do the same here
      __UpperCamelCase							    =			Features({'labels': ClassLabel} )
      __UpperCamelCase							    =			{'text_column': 'input_text', 'label_column': 'input_labels'}
      __UpperCamelCase							    =			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 ):

      '''simple docstring'''




      __UpperCamelCase							    =			TextClassification(text_column='input_text'      ,	label_column='input_label' )
      self.assertEqual(task.label_schema['labels']      ,	__UpperCAmelCase )
      __UpperCamelCase							    =			task.align_with_features(Features({'input_label': ClassLabel(names=self.labels )} ) )
      self.assertEqual(task.label_schema['labels']      ,	ClassLabel(names=self.labels ) )


class __lowerCAmelCase						( __SCREAMING_SNAKE_CASE     ):





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			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 ):

      '''simple docstring'''




      __UpperCamelCase							    =			Features({'question': Value('string' ), 'context': Value('string' )} )
      __UpperCamelCase							    =			Features(
          {
              'answers': Sequence(
                  {
                      'text': Value('string' ),
                      'answer_start': Value('int32' ),
                  } )
          } )
      __UpperCamelCase							    =			{
          'context_column': 'input_input_context',
          'question_column': 'input_question',
          'answers_column': 'input_answers',
      }
      __UpperCamelCase							    =			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						( __SCREAMING_SNAKE_CASE     ):





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			Summarization(text_column='input_text'      ,	summary_column='input_summary' )
      self.assertDictEqual({'input_text': 'text', 'input_summary': 'summary'}      ,	task.column_mapping )







     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			Features({'text': Value('string' )} )
      __UpperCamelCase							    =			Features({'summary': Value('string' )} )
      __UpperCamelCase							    =			{'text_column': 'input_text', 'summary_column': 'input_summary'}
      __UpperCamelCase							    =			Summarization.from_dict(__UpperCAmelCase )
      self.assertEqual('summarization'      ,	task.task )
      self.assertEqual(__UpperCAmelCase      ,	task.input_schema )
      self.assertEqual(__UpperCAmelCase      ,	task.label_schema )


class __lowerCAmelCase						( __SCREAMING_SNAKE_CASE     ):





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			AutomaticSpeechRecognition(audio_column='input_audio'      ,	transcription_column='input_transcription' )
      self.assertDictEqual({'input_audio': 'audio', 'input_transcription': 'transcription'}      ,	task.column_mapping )







     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			Features({'audio': Audio()} )
      __UpperCamelCase							    =			Features({'transcription': Value('string' )} )
      __UpperCamelCase							    =			{
          'audio_column': 'input_audio',
          'transcription_column': 'input_transcription',
      }
      __UpperCamelCase							    =			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						( __SCREAMING_SNAKE_CASE     ):





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			sorted(['pos', 'neg'] )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			AudioClassification(audio_column='input_audio'      ,	label_column='input_label' )
      self.assertDictEqual({'input_audio': 'audio', 'input_label': 'labels'}      ,	task.column_mapping )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			Features({'audio': Audio()} )
      __UpperCamelCase							    =			Features({'labels': ClassLabel} )
      __UpperCamelCase							    =			{
          'audio_column': 'input_image',
          'label_column': 'input_label',
      }
      __UpperCamelCase							    =			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 ):

      '''simple docstring'''




      __UpperCamelCase							    =			AudioClassification(audio_column='input_audio'      ,	label_column='input_label' )
      self.assertEqual(task.label_schema['labels']      ,	__UpperCAmelCase )
      __UpperCamelCase							    =			task.align_with_features(Features({'input_label': ClassLabel(names=self.labels )} ) )
      self.assertEqual(task.label_schema['labels']      ,	ClassLabel(names=self.labels ) )


class __lowerCAmelCase						( __SCREAMING_SNAKE_CASE     ):





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			sorted(['pos', 'neg'] )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			ImageClassification(image_column='input_image'      ,	label_column='input_label' )
      self.assertDictEqual({'input_image': 'image', 'input_label': 'labels'}      ,	task.column_mapping )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			Features({'image': Image()} )
      __UpperCamelCase							    =			Features({'labels': ClassLabel} )
      __UpperCamelCase							    =			{
          'image_column': 'input_image',
          'label_column': 'input_label',
      }
      __UpperCamelCase							    =			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 ):

      '''simple docstring'''




      __UpperCamelCase							    =			ImageClassification(image_column='input_image'      ,	label_column='input_label' )
      self.assertEqual(task.label_schema['labels']      ,	__UpperCAmelCase )
      __UpperCamelCase							    =			task.align_with_features(Features({'input_label': ClassLabel(names=self.labels )} ) )
      self.assertEqual(task.label_schema['labels']      ,	ClassLabel(names=self.labels ) )


class __lowerCAmelCase						( __SCREAMING_SNAKE_CASE     ):





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			DatasetInfo(task_templates=QuestionAnsweringExtractive() )
      __UpperCamelCase							    =			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 ):
       __UpperCamelCase							    =			deepcopy(__UpperCAmelCase )
       __UpperCamelCase							    =			0
       return out

      __UpperCamelCase							    =			dataset.map(__UpperCAmelCase )
      assert mapped_dataset.info.task_templates == dataset.info.task_templates
      # reload from cache
      __UpperCamelCase							    =			dataset.map(__UpperCAmelCase )
      assert mapped_dataset.info.task_templates == dataset.info.task_templates

      __UpperCamelCase							    =			dataset.map(__UpperCAmelCase )
      assert mapped_dataset.info.task_templates == []
      # reload from cache
      __UpperCamelCase							    =			dataset.map(__UpperCAmelCase )
      assert mapped_dataset.info.task_templates == []







     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			Features({'text': Value('string' ), 'label': ClassLabel(names=('pos', 'neg') )} )
      __UpperCamelCase							    =			TextClassification(text_column='text'      ,	label_column='label' )
      __UpperCamelCase							    =			DatasetInfo(features=__UpperCAmelCase      ,	task_templates=__UpperCAmelCase )
      __UpperCamelCase							    =			Dataset.from_dict({'text': ['A sentence.'], 'label': ['pos']}      ,	info=__UpperCAmelCase )

      def process(__UpperCAmelCase ):
       return example

      __UpperCamelCase							    =			dataset.remove_columns('label' )
      __UpperCamelCase							    =			modified_dataset.map(__UpperCAmelCase )
      assert mapped_dataset.info.task_templates == []





"""simple docstring"""

import random
import unittest

import torch

from diffusers import IFSuperResolutionPipeline
from diffusers.utils import floats_tensor
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import skip_mps, torch_device

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin
from . import IFPipelineTesterMixin

@skip_mps
class __lowerCAmelCase						( __SCREAMING_SNAKE_CASE     ,    __SCREAMING_SNAKE_CASE     ,    unittest.TestCase     ):
     lowercase		  =		IFSuperResolutionPipeline
     lowercase		  =		TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"width", "height"}
     lowercase		  =		TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
     lowercase		  =		PipelineTesterMixin.required_optional_params - {"latents"}





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      return self._get_superresolution_dummy_components()





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase=0 ):

      '''simple docstring'''




      if str(__UpperCAmelCase ).startswith('mps' ):
       __UpperCamelCase							    =			torch.manual_seed(__UpperCAmelCase )
      else:
       __UpperCamelCase							    =			torch.Generator(device=__UpperCAmelCase ).manual_seed(__UpperCAmelCase )

      __UpperCamelCase							    =			floats_tensor((1, 3, 32, 32)      ,	rng=random.Random(__UpperCAmelCase ) ).to(__UpperCAmelCase )

      __UpperCamelCase							    =			{
          'prompt': 'A painting of a squirrel eating a burger',
          'image': image,
          'generator': generator,
          'num_inference_steps': 2,
          'output_type': 'numpy',
      }

      return inputs





     @unittest.skipIf(
         torch_device != 'cuda' or not is_xformers_available()      ,	reason='XFormers attention is only available with CUDA and `xformers` installed'      ,	)
     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3 )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      self._test_save_load_optional_components()





     @unittest.skipIf(torch_device != 'cuda'      ,	reason='float16 requires CUDA' )
     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      super().test_save_load_floataa(expected_max_diff=1E-1 )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      self._test_attention_slicing_forward_pass(expected_max_diff=1E-2 )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      self._test_save_load_local()







     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      self._test_inference_batch_single_identical(
          expected_max_diff=1E-2      ,	)



