




import random
import unittest

import numpy as np
import torch

from diffusers import DDIMScheduler, LDMSuperResolutionPipeline, UNetaDModel, VQModel
from diffusers.utils import PIL_INTERPOLATION, floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch


enable_full_determinism()
class       _UpperCamelCase   (					unittest.TestCase		):
      @property
      def 						UpperCAmelCase_    (      self :Union[str, Any]   )  ->      List[Any]:
       UpperCAmelCase__							=    1
       UpperCAmelCase__							=    3
       UpperCAmelCase__							=    (32, 32)

       UpperCAmelCase__							=    floats_tensor((batch_size, num_channels) + sizes			,   rng=random.Random(0   )   ).to(lowerCamelCase   )
       return image
      @property
      def 						UpperCAmelCase_    (      self :List[str]   )  ->      Union[str, Any]:
       torch.manual_seed(0   )
       UpperCAmelCase__							=    UNetaDModel(
           block_out_channels=(32, 64)			,   layers_per_block=2			,   sample_size=32			,   in_channels=6			,   out_channels=3			,   down_block_types=("DownBlock2D", "AttnDownBlock2D")			,   up_block_types=("AttnUpBlock2D", "UpBlock2D")			,   )
       return model
      @property
      def 						UpperCAmelCase_    (      self :Any   )  ->      Tuple:
       torch.manual_seed(0   )
       UpperCAmelCase__							=    VQModel(
           block_out_channels=[32, 64]			,   in_channels=3			,   out_channels=3			,   down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]			,   up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]			,   latent_channels=3			,   )
       return model
      def 						UpperCAmelCase_    (      self :Union[str, Any]   )  ->      Tuple:
       UpperCAmelCase__							=    "cpu"
       UpperCAmelCase__							=    self.dummy_uncond_unet
       UpperCAmelCase__							=    DDIMScheduler()
       UpperCAmelCase__							=    self.dummy_vq_model

       UpperCAmelCase__							=    LDMSuperResolutionPipeline(unet=lowerCamelCase			,   vqvae=lowerCamelCase			,   scheduler=lowerCamelCase   )
       ldm.to(lowerCamelCase   )
       ldm.set_progress_bar_config(disable=lowerCamelCase   )

       UpperCAmelCase__							=    self.dummy_image.to(lowerCamelCase   )

       UpperCAmelCase__							=    torch.Generator(device=lowerCamelCase   ).manual_seed(0   )
       UpperCAmelCase__							=    ldm(image=lowerCamelCase			,   generator=lowerCamelCase			,   num_inference_steps=2			,   output_type="numpy"   ).images

       UpperCAmelCase__							=    image[0, -3:, -3:, -1]

       assert image.shape == (1, 64, 64, 3)
       UpperCAmelCase__							=    np.array([0.86_78, 0.82_45, 0.63_81, 0.68_30, 0.43_85, 0.55_99, 0.46_41, 0.62_01, 0.51_50]   )

       assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1e-2







      @unittest.skipIf(torch_device != "cuda"			,   "This test requires a GPU"   )
      def 						UpperCAmelCase_    (      self :Dict   )  ->      int:
       UpperCAmelCase__							=    self.dummy_uncond_unet
       UpperCAmelCase__							=    DDIMScheduler()
       UpperCAmelCase__							=    self.dummy_vq_model

       # put models in fp16
       UpperCAmelCase__							=    unet.half()
       UpperCAmelCase__							=    vqvae.half()

       UpperCAmelCase__							=    LDMSuperResolutionPipeline(unet=lowerCamelCase			,   vqvae=lowerCamelCase			,   scheduler=lowerCamelCase   )
       ldm.to(lowerCamelCase   )
       ldm.set_progress_bar_config(disable=lowerCamelCase   )

       UpperCAmelCase__							=    self.dummy_image.to(lowerCamelCase   )

       UpperCAmelCase__							=    ldm(lowerCamelCase			,   num_inference_steps=2			,   output_type="numpy"   ).images

       assert image.shape == (1, 64, 64, 3)



@slow
@require_torch
class       _UpperCamelCase   (					unittest.TestCase		):
      def 						UpperCAmelCase_    (      self :Dict   )  ->      List[str]:
       UpperCAmelCase__							=    load_image(
           "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
           "/vq_diffusion/teddy_bear_pool.png"   )
       UpperCAmelCase__							=    init_image.resize((64, 64)			,   resample=PIL_INTERPOLATION["lanczos"]   )

       UpperCAmelCase__							=    LDMSuperResolutionPipeline.from_pretrained("duongna/ldm-super-resolution"			,   device_map="auto"   )
       ldm.set_progress_bar_config(disable=lowerCamelCase   )

       UpperCAmelCase__							=    torch.manual_seed(0   )
       UpperCAmelCase__							=    ldm(image=lowerCamelCase			,   generator=lowerCamelCase			,   num_inference_steps=20			,   output_type="numpy"   ).images

       UpperCAmelCase__							=    image[0, -3:, -3:, -1]

       assert image.shape == (1, 256, 256, 3)
       UpperCAmelCase__							=    np.array([0.76_44, 0.76_79, 0.76_42, 0.76_33, 0.76_66, 0.75_60, 0.74_25, 0.72_57, 0.69_07]   )

       assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1e-2











from typing import Optional

from ..utils.logging import get_logger
from .audio_classificiation import AudioClassification
from .automatic_speech_recognition import AutomaticSpeechRecognition
from .base import TaskTemplate
from .image_classification import ImageClassification
from .language_modeling import LanguageModeling
from .question_answering import QuestionAnsweringExtractive
from .summarization import Summarization
from .text_classification import TextClassification


_lowerCAmelCase    : Any		= [
    "AutomaticSpeechRecognition",
    "AudioClassification",
    "ImageClassification",
    "LanguageModeling",
    "QuestionAnsweringExtractive",
    "Summarization",
    "TaskTemplate",
    "TextClassification",
]

_lowerCAmelCase    : Dict		= get_logger(__name__)


_lowerCAmelCase    : Optional[Any]		= {
    AutomaticSpeechRecognition.task: AutomaticSpeechRecognition,
    AudioClassification.task: AudioClassification,
    ImageClassification.task: ImageClassification,
    LanguageModeling.task: LanguageModeling,
    QuestionAnsweringExtractive.task: QuestionAnsweringExtractive,
    Summarization.task: Summarization,
    TextClassification.task: TextClassification,
}



def     lowerCAmelCase		(		_lowerCAmelCase				: dict						):




 """simple docstring"""







 UpperCAmelCase__							=    task_template_dict.get("task"						)
 if task_name is None:
  logger.warning(F'''Couldn\'t find template for task \'{task_name}\'. Available templates: {list(_lowerCAmelCase						)}'''						)
  return None
 UpperCAmelCase__							=    NAMEaTEMPLATE.get(_lowerCAmelCase						)
 return template.from_dict(_lowerCAmelCase						)





