


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


_UpperCAmelCase			:  List[Any]     =False







class 				snake_case__(							unittest.TestCase							):







						'''simple docstring'''




						pass







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







						'''simple docstring'''




						def 						lowercase_ (				self							)					->    str:
										# clean up the VRAM after each test
										super().tearDown()
										gc.collect()
										torch.cuda.empty_cache()




						def 						lowercase_ (				self							)					->    List[Any]:
										lowerCAmelCase_ :      str    				=				VersatileDiffusionTextToImagePipeline.from_pretrained('''shi-labs/versatile-diffusion'''							)
										# remove text_unet
										pipe.remove_unused_weights()
										pipe.to(__lowercase							)
										pipe.set_progress_bar_config(disable=__lowercase							)

										lowerCAmelCase_ :      Tuple    				=				'''A painting of a squirrel eating a burger '''
										lowerCAmelCase_ :      List[Any]    				=				torch.manual_seed(0							)
										lowerCAmelCase_ :      Optional[Any]    				=				pipe(
										    prompt=__lowercase   ,						generator=__lowercase   ,						guidance_scale=7.5   ,						num_inference_steps=2   ,						output_type='''numpy'''							).images

										with tempfile.TemporaryDirectory() as tmpdirname:
														pipe.save_pretrained(__lowercase							)
														lowerCAmelCase_ :      Optional[Any]    				=				VersatileDiffusionTextToImagePipeline.from_pretrained(__lowercase							)
										pipe.to(__lowercase							)
										pipe.set_progress_bar_config(disable=__lowercase							)

										lowerCAmelCase_ :      Any    				=				generator.manual_seed(0							)
										lowerCAmelCase_ :      Any    				=				pipe(
										    prompt=__lowercase   ,						generator=__lowercase   ,						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 						lowercase_ (				self							)					->    int:
										lowerCAmelCase_ :      int    				=				VersatileDiffusionTextToImagePipeline.from_pretrained(
										    '''shi-labs/versatile-diffusion'''   ,						torch_dtype=torch.floataa							)
										pipe.to(__lowercase							)
										pipe.set_progress_bar_config(disable=__lowercase							)

										lowerCAmelCase_ :      Dict    				=				'''A painting of a squirrel eating a burger '''
										lowerCAmelCase_ :      List[Any]    				=				torch.manual_seed(0							)
										lowerCAmelCase_ :      Tuple    				=				pipe(
										    prompt=__lowercase   ,						generator=__lowercase   ,						guidance_scale=7.5   ,						num_inference_steps=5_0   ,						output_type='''numpy'''							).images

										lowerCAmelCase_ :      Optional[int]    				=				image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

										assert image.shape == (1, 5_1_2, 5_1_2, 3)
										lowerCAmelCase_ :      str    				=				np.array([0.33_67, 0.31_69, 0.26_56, 0.38_70, 0.47_90, 0.37_96, 0.40_09, 0.48_78, 0.47_78]							)

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



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

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







@dataclass(frozen=UpperCAmelCase__							)
class 				snake_case__(							UpperCAmelCase__							):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__      :  str					=							field(default="""automatic-speech-recognition""",						metadata={"""include_in_asdict_even_if_is_default""": True}							)
						SCREAMING_SNAKE_CASE__      :  ClassVar[Features]					=							Features({"""audio""": Audio()}							)
						SCREAMING_SNAKE_CASE__      :  ClassVar[Features]					=							Features({"""transcription""": Value("""string"""							)}							)
						SCREAMING_SNAKE_CASE__      :  str					=							"audio"
						SCREAMING_SNAKE_CASE__      :  str					=							"transcription"




						def 						lowercase_ (				self   ,						__lowercase							)					->    int:
										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]   ,						__lowercase							):
														raise ValueError(f"""Column {self.audio_column} is not an Audio type."""							)
										lowerCAmelCase_ :      List[str]    				=				copy.deepcopy(self							)
										lowerCAmelCase_ :      Optional[Any]    				=				self.input_schema.copy()
										lowerCAmelCase_ :      Optional[Any]    				=				features[self.audio_column]
										lowerCAmelCase_ :      List[str]    				=				input_schema
										return task_template




						@property
						def 						lowercase_ (				self							)					->    Dict[str, str]:
										return {self.audio_column: "audio", self.transcription_column: "transcription"}