





'''simple docstring'''







import inspect
import re
from hashlib import shaaaa
from typing import Dict, List

from .arrow import arrow
from .audiofolder import audiofolder
from .csv import csv
from .imagefolder import imagefolder
from .json import json
from .pandas import pandas
from .parquet import parquet
from .sql import sql  # noqa F401
from .text import text





def        lowerCamelCase_      (  SCREAMING_SNAKE_CASE__      :			List[str]   ) ->	str:
						UpperCAmelCase_			:      str     						=  []
						for line in lines:
												UpperCAmelCase_			:      Tuple     						=  re.sub(R'''#.*''',			'''''',			SCREAMING_SNAKE_CASE__   )  # remove comments
												if line:
																		filtered_lines.append(SCREAMING_SNAKE_CASE__   )
						UpperCAmelCase_			:      List[str]     						=  '''\n'''.join(SCREAMING_SNAKE_CASE__   )

						# Make a hash from all this code
						UpperCAmelCase_			:      Dict     						=  full_str.encode('''utf-8'''   )
						return shaaaa(SCREAMING_SNAKE_CASE__   ).hexdigest()


# get importable module names and hash for caching
snake_case_     :       Union[str, Any]						=  {
    "csv": (csv.__name__, _hash_python_lines(inspect.getsource(csv).splitlines())),
    "json": (json.__name__, _hash_python_lines(inspect.getsource(json).splitlines())),
    "pandas": (pandas.__name__, _hash_python_lines(inspect.getsource(pandas).splitlines())),
    "parquet": (parquet.__name__, _hash_python_lines(inspect.getsource(parquet).splitlines())),
    "arrow": (arrow.__name__, _hash_python_lines(inspect.getsource(arrow).splitlines())),
    "text": (text.__name__, _hash_python_lines(inspect.getsource(text).splitlines())),
    "imagefolder": (imagefolder.__name__, _hash_python_lines(inspect.getsource(imagefolder).splitlines())),
    "audiofolder": (audiofolder.__name__, _hash_python_lines(inspect.getsource(audiofolder).splitlines())),
}

# Used to infer the module to use based on the data files extensions
snake_case_     :       str						=  {
    ".csv": ("csv", {}),
    ".tsv": ("csv", {"sep": "\t"}),
    ".json": ("json", {}),
    ".jsonl": ("json", {}),
    ".parquet": ("parquet", {}),
    ".arrow": ("arrow", {}),
    ".txt": ("text", {}),
}
_EXTENSION_TO_MODULE.update({ext: ("imagefolder", {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ("imagefolder", {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext: ("audiofolder", {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ("audiofolder", {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
snake_case_     :       List[Any]						=  {"imagefolder", "audiofolder"}

# Used to filter data files based on extensions given a module name
snake_case_     :       Dict[str, List[str]]						=  {}
for _ext, (_module, _) in _EXTENSION_TO_MODULE.items():
					_MODULE_TO_EXTENSIONS.setdefault(_module, []).append(_ext)

_MODULE_TO_EXTENSIONS["imagefolder"].append(".zip")
_MODULE_TO_EXTENSIONS["audiofolder"].append(".zip")












'''simple docstring'''







from typing import Any, Callable, Dict, List, Optional, Union

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker


snake_case_     :       Union[str, Any]						=  "CompVis/stable-diffusion-v1-1"
snake_case_     :       Dict						=  "CompVis/stable-diffusion-v1-2"
snake_case_     :       Any						=  "CompVis/stable-diffusion-v1-3"
snake_case_     :       str						=  "CompVis/stable-diffusion-v1-4"


class 					__a      (lowerCamelCase     ):

							def __init__(							self							:     Any       ,     __magic_name__							:     AutoencoderKL       ,     __magic_name__							:     CLIPTextModel       ,     __magic_name__							:     CLIPTokenizer       ,     __magic_name__							:     UNetaDConditionModel       ,     __magic_name__							:     Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]       ,     __magic_name__							:     StableDiffusionSafetyChecker       ,     __magic_name__							:     CLIPImageProcessor       ,     __magic_name__							:     bool = True       ,     )					->       str:







													"""simple docstring"""
													super()._init_()

													UpperCAmelCase_			:      Any     						=  StableDiffusionPipeline.from_pretrained(__magic_name__   )
													UpperCAmelCase_			:      Dict     						=  StableDiffusionPipeline.from_pretrained(__magic_name__   )
													UpperCAmelCase_			:      List[Any]     						=  StableDiffusionPipeline.from_pretrained(__magic_name__   )
													UpperCAmelCase_			:      Tuple     						=  StableDiffusionPipeline(
													    vae=__magic_name__       ,     text_encoder=__magic_name__       ,     tokenizer=__magic_name__       ,     unet=__magic_name__       ,     scheduler=__magic_name__       ,     safety_checker=__magic_name__       ,     feature_extractor=__magic_name__       ,     requires_safety_checker=__magic_name__       ,     )

													self.register_modules(pipelinea=self.pipea       ,     pipelinea=self.pipea       ,     pipelinea=self.pipea       ,     pipelinea=self.pipea   )

							@property
							def     UpperCAmelCase__	(							self							:     Tuple   )					->       Dict[str, Any]:







													"""simple docstring"""
													return {k: getattr(self       ,     __magic_name__   ) for k in self.config.keys() if not k.startswith('''_'''   )}

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     Optional[Union[str, int]] = "auto"   )					->       int:







													"""simple docstring"""
													if slice_size == "auto":
																			# half the attention head size is usually a good trade-off between
																			# speed and memory
																			UpperCAmelCase_			:      List[str]     						=  self.unet.config.attention_head_dim // 2
													self.unet.set_attention_slice(__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       List[str]:







													"""simple docstring"""
													self.enable_attention_slicing(__magic_name__   )

							@torch.no_grad()
							def     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     Union[str, List[str]]       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 50       ,     __magic_name__							:     float = 7.5       ,     __magic_name__							:     Optional[Union[str, List[str]]] = None       ,     __magic_name__							:     Optional[int] = 1       ,     __magic_name__							:     float = 0.0       ,     __magic_name__							:     Optional[torch.Generator] = None       ,     __magic_name__							:     Optional[torch.FloatTensor] = None       ,     __magic_name__							:     Optional[str] = "pil"       ,     __magic_name__							:     bool = True       ,     __magic_name__							:     Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,     __magic_name__							:     int = 1       ,     **__magic_name__							:     Tuple       ,     )					->       Optional[int]:







													"""simple docstring"""
													return self.pipea(
													    prompt=__magic_name__       ,     height=__magic_name__       ,     width=__magic_name__       ,     num_inference_steps=__magic_name__       ,     guidance_scale=__magic_name__       ,     negative_prompt=__magic_name__       ,     num_images_per_prompt=__magic_name__       ,     eta=__magic_name__       ,     generator=__magic_name__       ,     latents=__magic_name__       ,     output_type=__magic_name__       ,     return_dict=__magic_name__       ,     callback=__magic_name__       ,     callback_steps=__magic_name__       ,     **__magic_name__       ,     )

							@torch.no_grad()
							def     UpperCAmelCase__	(							self							:     Optional[int]       ,     __magic_name__							:     Union[str, List[str]]       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 50       ,     __magic_name__							:     float = 7.5       ,     __magic_name__							:     Optional[Union[str, List[str]]] = None       ,     __magic_name__							:     Optional[int] = 1       ,     __magic_name__							:     float = 0.0       ,     __magic_name__							:     Optional[torch.Generator] = None       ,     __magic_name__							:     Optional[torch.FloatTensor] = None       ,     __magic_name__							:     Optional[str] = "pil"       ,     __magic_name__							:     bool = True       ,     __magic_name__							:     Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,     __magic_name__							:     int = 1       ,     **__magic_name__							:     Any       ,     )					->       Any:







													"""simple docstring"""
													return self.pipea(
													    prompt=__magic_name__       ,     height=__magic_name__       ,     width=__magic_name__       ,     num_inference_steps=__magic_name__       ,     guidance_scale=__magic_name__       ,     negative_prompt=__magic_name__       ,     num_images_per_prompt=__magic_name__       ,     eta=__magic_name__       ,     generator=__magic_name__       ,     latents=__magic_name__       ,     output_type=__magic_name__       ,     return_dict=__magic_name__       ,     callback=__magic_name__       ,     callback_steps=__magic_name__       ,     **__magic_name__       ,     )

							@torch.no_grad()
							def     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     Union[str, List[str]]       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 50       ,     __magic_name__							:     float = 7.5       ,     __magic_name__							:     Optional[Union[str, List[str]]] = None       ,     __magic_name__							:     Optional[int] = 1       ,     __magic_name__							:     float = 0.0       ,     __magic_name__							:     Optional[torch.Generator] = None       ,     __magic_name__							:     Optional[torch.FloatTensor] = None       ,     __magic_name__							:     Optional[str] = "pil"       ,     __magic_name__							:     bool = True       ,     __magic_name__							:     Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,     __magic_name__							:     int = 1       ,     **__magic_name__							:     Dict       ,     )					->       List[str]:







													"""simple docstring"""
													return self.pipea(
													    prompt=__magic_name__       ,     height=__magic_name__       ,     width=__magic_name__       ,     num_inference_steps=__magic_name__       ,     guidance_scale=__magic_name__       ,     negative_prompt=__magic_name__       ,     num_images_per_prompt=__magic_name__       ,     eta=__magic_name__       ,     generator=__magic_name__       ,     latents=__magic_name__       ,     output_type=__magic_name__       ,     return_dict=__magic_name__       ,     callback=__magic_name__       ,     callback_steps=__magic_name__       ,     **__magic_name__       ,     )

							@torch.no_grad()
							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     Union[str, List[str]]       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 50       ,     __magic_name__							:     float = 7.5       ,     __magic_name__							:     Optional[Union[str, List[str]]] = None       ,     __magic_name__							:     Optional[int] = 1       ,     __magic_name__							:     float = 0.0       ,     __magic_name__							:     Optional[torch.Generator] = None       ,     __magic_name__							:     Optional[torch.FloatTensor] = None       ,     __magic_name__							:     Optional[str] = "pil"       ,     __magic_name__							:     bool = True       ,     __magic_name__							:     Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,     __magic_name__							:     int = 1       ,     **__magic_name__							:     Optional[int]       ,     )					->       str:







													"""simple docstring"""
													return self.pipea(
													    prompt=__magic_name__       ,     height=__magic_name__       ,     width=__magic_name__       ,     num_inference_steps=__magic_name__       ,     guidance_scale=__magic_name__       ,     negative_prompt=__magic_name__       ,     num_images_per_prompt=__magic_name__       ,     eta=__magic_name__       ,     generator=__magic_name__       ,     latents=__magic_name__       ,     output_type=__magic_name__       ,     return_dict=__magic_name__       ,     callback=__magic_name__       ,     callback_steps=__magic_name__       ,     **__magic_name__       ,     )



							@torch.no_grad()
							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     Union[str, List[str]]       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 5_12       ,     __magic_name__							:     int = 50       ,     __magic_name__							:     float = 7.5       ,     __magic_name__							:     Optional[Union[str, List[str]]] = None       ,     __magic_name__							:     Optional[int] = 1       ,     __magic_name__							:     float = 0.0       ,     __magic_name__							:     Optional[torch.Generator] = None       ,     __magic_name__							:     Optional[torch.FloatTensor] = None       ,     __magic_name__							:     Optional[str] = "pil"       ,     __magic_name__							:     bool = True       ,     __magic_name__							:     Optional[Callable[[int, int, torch.FloatTensor], None]] = None       ,     __magic_name__							:     int = 1       ,     **__magic_name__							:     Optional[int]       ,     )					->       List[Any]:







													"""simple docstring"""
													UpperCAmelCase_			:      int     						=  '''cuda''' if torch.cuda.is_available() else '''cpu'''
													self.to(__magic_name__   )

													# Checks if the height and width are divisible by 8 or not
													if height % 8 != 0 or width % 8 != 0:
																			raise ValueError(F"""`height` and `width` must be divisible by 8 but are {height} and {width}."""   )

													# Get first result from Stable Diffusion Checkpoint v1.1
													UpperCAmelCase_			:      Optional[int]     						=  self.textaimg_sda_a(
													    prompt=__magic_name__       ,     height=__magic_name__       ,     width=__magic_name__       ,     num_inference_steps=__magic_name__       ,     guidance_scale=__magic_name__       ,     negative_prompt=__magic_name__       ,     num_images_per_prompt=__magic_name__       ,     eta=__magic_name__       ,     generator=__magic_name__       ,     latents=__magic_name__       ,     output_type=__magic_name__       ,     return_dict=__magic_name__       ,     callback=__magic_name__       ,     callback_steps=__magic_name__       ,     **__magic_name__       ,     )

													# Get first result from Stable Diffusion Checkpoint v1.2
													UpperCAmelCase_			:      int     						=  self.textaimg_sda_a(
													    prompt=__magic_name__       ,     height=__magic_name__       ,     width=__magic_name__       ,     num_inference_steps=__magic_name__       ,     guidance_scale=__magic_name__       ,     negative_prompt=__magic_name__       ,     num_images_per_prompt=__magic_name__       ,     eta=__magic_name__       ,     generator=__magic_name__       ,     latents=__magic_name__       ,     output_type=__magic_name__       ,     return_dict=__magic_name__       ,     callback=__magic_name__       ,     callback_steps=__magic_name__       ,     **__magic_name__       ,     )

													# Get first result from Stable Diffusion Checkpoint v1.3
													UpperCAmelCase_			:      str     						=  self.textaimg_sda_a(
													    prompt=__magic_name__       ,     height=__magic_name__       ,     width=__magic_name__       ,     num_inference_steps=__magic_name__       ,     guidance_scale=__magic_name__       ,     negative_prompt=__magic_name__       ,     num_images_per_prompt=__magic_name__       ,     eta=__magic_name__       ,     generator=__magic_name__       ,     latents=__magic_name__       ,     output_type=__magic_name__       ,     return_dict=__magic_name__       ,     callback=__magic_name__       ,     callback_steps=__magic_name__       ,     **__magic_name__       ,     )

													# Get first result from Stable Diffusion Checkpoint v1.4
													UpperCAmelCase_			:      str     						=  self.textaimg_sda_a(
													    prompt=__magic_name__       ,     height=__magic_name__       ,     width=__magic_name__       ,     num_inference_steps=__magic_name__       ,     guidance_scale=__magic_name__       ,     negative_prompt=__magic_name__       ,     num_images_per_prompt=__magic_name__       ,     eta=__magic_name__       ,     generator=__magic_name__       ,     latents=__magic_name__       ,     output_type=__magic_name__       ,     return_dict=__magic_name__       ,     callback=__magic_name__       ,     callback_steps=__magic_name__       ,     **__magic_name__       ,     )

													# Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result
													return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]]   )





