
'''simple docstring'''

import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionTextToImagePipeline
from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device


__snake_case:  Tuple								=			False


class  _UpperCAmelCase ( unittest.TestCase    ):

     """simple docstring"""

     pass







@nightly
@require_torch_gpu
class  _UpperCAmelCase ( unittest.TestCase    ):

     """simple docstring"""

     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()


     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Optional[Any]      =							VersatileDiffusionTextToImagePipeline.from_pretrained("""shi-labs/versatile-diffusion""" )
       # remove text_unet
       pipe.remove_unused_weights()
       pipe.to(lowerCAmelCase_ )
       pipe.set_progress_bar_config(disable=lowerCAmelCase_ )

       a_      :	int      =							"A painting of a squirrel eating a burger "
       a_      :	int      =							torch.manual_seed(0 )
       a_      :	List[str]      =							pipe(
           prompt=lowerCAmelCase_							,		generator=lowerCAmelCase_							,		guidance_scale=7.5							,		num_inference_steps=2							,		output_type="""numpy""" ).images

       with tempfile.TemporaryDirectory() as tmpdirname:
         pipe.save_pretrained(lowerCAmelCase_ )
         a_      :	Dict      =							VersatileDiffusionTextToImagePipeline.from_pretrained(lowerCAmelCase_ )
       pipe.to(lowerCAmelCase_ )
       pipe.set_progress_bar_config(disable=lowerCAmelCase_ )

       a_      :	List[str]      =							generator.manual_seed(0 )
       a_      :	int      =							pipe(
           prompt=lowerCAmelCase_							,		generator=lowerCAmelCase_							,		guidance_scale=7.5							,		num_inference_steps=2							,		output_type="""numpy""" ).images

       assert np.abs(image - new_image ).sum() < 1E-5, "Models don't have the same forward pass"






     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       a_      :	Dict      =							VersatileDiffusionTextToImagePipeline.from_pretrained(
           """shi-labs/versatile-diffusion"""							,		torch_dtype=torch.floataa )
       pipe.to(lowerCAmelCase_ )
       pipe.set_progress_bar_config(disable=lowerCAmelCase_ )

       a_      :	Optional[Any]      =							"A painting of a squirrel eating a burger "
       a_      :	Optional[Any]      =							torch.manual_seed(0 )
       a_      :	Any      =							pipe(
           prompt=lowerCAmelCase_							,		generator=lowerCAmelCase_							,		guidance_scale=7.5							,		num_inference_steps=50							,		output_type="""numpy""" ).images

       a_      :	Any      =							image[0, 2_53:2_56, 2_53:2_56, -1]

       assert image.shape == (1, 5_12, 5_12, 3)
       a_      :	int      =							np.array([0.3367, 0.3169, 0.2656, 0.3870, 0.4790, 0.3796, 0.4009, 0.4878, 0.4778] )

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




'''simple docstring'''

import copy
from dataclasses import dataclass, field
from typing import ClassVar, Dict

from ..features import Audio, Features, Value
from .base import TaskTemplate


@dataclass(frozen=lowerCAmelCase__    )
class  _UpperCAmelCase ( lowerCAmelCase__    ):

     """simple docstring"""

     a_    	=					field(default="automatic-speech-recognition"	,metadata={"include_in_asdict_even_if_is_default": True}    )
     a_    	=					Features({"audio": Audio()}    )
     a_    	=					Features({"transcription": Value("string"    )}    )
     a_    	=					"audio"
     a_    	=					"transcription"


     def 							_lowerCAmelCase  (      self							,		lowerCAmelCase_ ):

       '''simple docstring'''

       if self.audio_column not in features:
         raise ValueError(f'''Column {self.audio_column} is not present in features.''' )
       if not isinstance(features[self.audio_column]							,		lowerCAmelCase_ ):
         raise ValueError(f'''Column {self.audio_column} is not an Audio type.''' )
       a_      :	int      =							copy.deepcopy(self )
       a_      :	Optional[int]      =							self.input_schema.copy()
       a_      :	Any      =							features[self.audio_column]
       a_      :	Any      =							input_schema
       return task_template






     @property
     def 							_lowerCAmelCase  (      self ):

       '''simple docstring'''

       return {self.audio_column: "audio", self.transcription_column: "transcription"}


