



import inspect
from typing import Callable, List, Optional, Union

import torch
from transformers import (
    CLIPImageProcessor,
    CLIPTextModel,
    CLIPTokenizer,
    WhisperForConditionalGeneration,
    WhisperProcessor,
)

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


_lowerCAmelCase	:   Union[str, Any]    =   logging.get_logger(__name__)  # pylint: disable=invalid-name


class 	__snake_case   ( SCREAMING_SNAKE_CASE ):
		def __init__(							self    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_    ,):







						"""simple docstring"""
						super().__init__()

						if safety_checker is None:
										logger.warning(
										    f'You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure'
										    ' that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered'
										    ' results in services or applications open to the public. Both the diffusers team and Hugging Face'
										    ' strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling'
										    ' it only for use-cases that involve analyzing network behavior or auditing its results. For more'
										    ' information, please have a look at https://github.com/huggingface/diffusers/pull/254 .'						)

						self.register_modules(
						    speech_model=a_    ,speech_processor=a_    ,vae=a_    ,text_encoder=a_    ,tokenizer=a_    ,unet=a_    ,scheduler=a_    ,feature_extractor=a_    ,)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_ = "auto"						):







						"""simple docstring"""
						if slice_size == "auto":
										lowerCAmelCase__		  =  self.unet.config.attention_head_dim // 2
						self.unet.set_attention_slice(a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







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



		@torch.no_grad()
		def __call__(							self    ,a_    ,a_=1_6000    ,a_ = 512    ,a_ = 512    ,a_ = 50    ,a_ = 7.5    ,a_ = None    ,a_ = 1    ,a_ = 0.0    ,a_ = None    ,a_ = None    ,a_ = "pil"    ,a_ = True    ,a_ = None    ,a_ = 1    ,**a_    ,):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.speech_processor.feature_extractor(
						    a_    ,return_tensors='pt'    ,sampling_rate=a_						).input_features.to(self.device						)
						lowerCAmelCase__		  =  self.speech_model.generate(a_    ,max_length=48_0000						)

						lowerCAmelCase__		  =  self.speech_processor.tokenizer.batch_decode(a_    ,skip_special_tokens=a_    ,normalize=a_						)[
						    0
						]

						if isinstance(a_    ,a_						):
										lowerCAmelCase__		  =  1
						elif isinstance(a_    ,a_						):
										lowerCAmelCase__		  =  len(a_						)
						else:
										raise ValueError(f'`prompt` has to be of type `str` or `list` but is {type(a_						)}'						)

						if height % 8 != 0 or width % 8 != 0:
										raise ValueError(f'`height` and `width` have to be divisible by 8 but are {height} and {width}.'						)

						if (callback_steps is None) or (
						    callback_steps is not None and (not isinstance(a_    ,a_						) or callback_steps <= 0)
						):
										raise ValueError(
										    f'`callback_steps` has to be a positive integer but is {callback_steps} of type'
										    f' {type(a_						)}.'						)

						# get prompt text embeddings
						lowerCAmelCase__		  =  self.tokenizer(
						    a_    ,padding='max_length'    ,max_length=self.tokenizer.model_max_length    ,return_tensors='pt'    ,)
						lowerCAmelCase__		  =  text_inputs.input_ids

						if text_input_ids.shape[-1] > self.tokenizer.model_max_length:
										lowerCAmelCase__		  =  self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :]						)
										logger.warning(
										    'The following part of your input was truncated because CLIP can only handle sequences up to'
										    f' {self.tokenizer.model_max_length} tokens: {removed_text}'						)
										lowerCAmelCase__		  =  text_input_ids[:, : self.tokenizer.model_max_length]
						lowerCAmelCase__		  =  self.text_encoder(text_input_ids.to(self.device						)						)[0]

						# duplicate text embeddings for each generation per prompt, using mps friendly method
						lowerCAmelCase__  ,   lowerCAmelCase__  ,   lowerCAmelCase__		  =  text_embeddings.shape
						lowerCAmelCase__		  =  text_embeddings.repeat(1    ,a_    ,1						)
						lowerCAmelCase__		  =  text_embeddings.view(bs_embed * num_images_per_prompt    ,a_    ,-1						)

						# here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
						# of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
						# corresponds to doing no classifier free guidance.
						lowerCAmelCase__		  =  guidance_scale > 1.0
						# get unconditional embeddings for classifier free guidance
						if do_classifier_free_guidance:
										lowerCAmelCase__		  =  42
										if negative_prompt is None:
														lowerCAmelCase__		  =  [''] * batch_size
										elif type(a_						) is not type(a_						):
														raise TypeError(
														    f'`negative_prompt` should be the same type to `prompt`, but got {type(a_						)} !='
														    f' {type(a_						)}.'						)
										elif isinstance(a_    ,a_						):
														lowerCAmelCase__		  =  [negative_prompt]
										elif batch_size != len(a_						):
														raise ValueError(
														    f'`negative_prompt`: {negative_prompt} has batch size {len(a_						)}, but `prompt`:'
														    f' {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches'
														    ' the batch size of `prompt`.'						)
										else:
														lowerCAmelCase__		  =  negative_prompt

										lowerCAmelCase__		  =  text_input_ids.shape[-1]
										lowerCAmelCase__		  =  self.tokenizer(
										    a_    ,padding='max_length'    ,max_length=a_    ,truncation=a_    ,return_tensors='pt'    ,)
										lowerCAmelCase__		  =  self.text_encoder(uncond_input.input_ids.to(self.device						)						)[0]

										# duplicate unconditional embeddings for each generation per prompt, using mps friendly method
										lowerCAmelCase__		  =  uncond_embeddings.shape[1]
										lowerCAmelCase__		  =  uncond_embeddings.repeat(1    ,a_    ,1						)
										lowerCAmelCase__		  =  uncond_embeddings.view(batch_size * num_images_per_prompt    ,a_    ,-1						)

										# For classifier free guidance, we need to do two forward passes.
										# Here we concatenate the unconditional and text embeddings into a single batch
										# to avoid doing two forward passes
										lowerCAmelCase__		  =  torch.cat([uncond_embeddings, text_embeddings]						)

						# get the initial random noise unless the user supplied it

						# Unlike in other pipelines, latents need to be generated in the target device
						# for 1-to-1 results reproducibility with the CompVis implementation.
						# However this currently doesn't work in `mps`.
						lowerCAmelCase__		  =  (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8)
						lowerCAmelCase__		  =  text_embeddings.dtype
						if latents is None:
										if self.device.type == "mps":
														# randn does not exist on mps
														lowerCAmelCase__		  =  torch.randn(a_    ,generator=a_    ,device='cpu'    ,dtype=a_						).to(
														    self.device						)
										else:
														lowerCAmelCase__		  =  torch.randn(a_    ,generator=a_    ,device=self.device    ,dtype=a_						)
						else:
										if latents.shape != latents_shape:
														raise ValueError(f'Unexpected latents shape, got {latents.shape}, expected {latents_shape}'						)
										lowerCAmelCase__		  =  latents.to(self.device						)

						# set timesteps
						self.scheduler.set_timesteps(a_						)

						# Some schedulers like PNDM have timesteps as arrays
						# It's more optimized to move all timesteps to correct device beforehand
						lowerCAmelCase__		  =  self.scheduler.timesteps.to(self.device						)

						# scale the initial noise by the standard deviation required by the scheduler
						lowerCAmelCase__		  =  latents * self.scheduler.init_noise_sigma

						# prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
						# eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
						# eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
						# and should be between [0, 1]
						lowerCAmelCase__		  =  'eta' in set(inspect.signature(self.scheduler.step						).parameters.keys()						)
						lowerCAmelCase__		  =  {}
						if accepts_eta:
										lowerCAmelCase__		  =  eta

						for i, t in enumerate(self.progress_bar(a_						)						):
										# expand the latents if we are doing classifier free guidance
										lowerCAmelCase__		  =  torch.cat([latents] * 2						) if do_classifier_free_guidance else latents
										lowerCAmelCase__		  =  self.scheduler.scale_model_input(a_    ,a_						)

										# predict the noise residual
										lowerCAmelCase__		  =  self.unet(a_    ,a_    ,encoder_hidden_states=a_						).sample

										# perform guidance
										if do_classifier_free_guidance:
														lowerCAmelCase__  ,   lowerCAmelCase__		  =  noise_pred.chunk(2						)
														lowerCAmelCase__		  =  noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

										# compute the previous noisy sample x_t -> x_t-1
										lowerCAmelCase__		  =  self.scheduler.step(a_    ,a_    ,a_    ,**a_						).prev_sample

										# call the callback, if provided
										if callback is not None and i % callback_steps == 0:
														callback(a_    ,a_    ,a_						)

						lowerCAmelCase__		  =  1 / 0.18215 * latents
						lowerCAmelCase__		  =  self.vae.decode(a_						).sample

						lowerCAmelCase__		  =  (image / 2 + 0.5).clamp(0    ,1						)

						# we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16
						lowerCAmelCase__		  =  image.cpu().permute(0    ,2    ,3    ,1						).float().numpy()

						if output_type == "pil":
										lowerCAmelCase__		  =  self.numpy_to_pil(a_						)

						if not return_dict:
										return image

						return StableDiffusionPipelineOutput(images=a_    ,nsfw_content_detected=a_						)





import itertools
from dataclasses import dataclass
from typing import Any, Callable, Dict, List, Optional, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast
from datasets.utils.py_utils import Literal


_lowerCAmelCase	:   Any    =   datasets.utils.logging.get_logger(__name__)

_lowerCAmelCase	:   Any    =   ["names", "prefix"]
_lowerCAmelCase	:   str    =   ["warn_bad_lines", "error_bad_lines", "mangle_dupe_cols"]
_lowerCAmelCase	:   List[str]    =   ["encoding_errors", "on_bad_lines"]
_lowerCAmelCase	:   int    =   ["date_format"]


@dataclass
class 	__snake_case   ( datasets.BuilderConfig ):
		SCREAMING_SNAKE_CASE__								=	","
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	"infer"
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	False
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	False
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	"."
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	'"'
		SCREAMING_SNAKE_CASE__								=	0
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	0
		SCREAMING_SNAKE_CASE__								=	True
		SCREAMING_SNAKE_CASE__								=	False
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	10000
		SCREAMING_SNAKE_CASE__								=	None
		SCREAMING_SNAKE_CASE__								=	"strict"
		SCREAMING_SNAKE_CASE__								=	"error"
		SCREAMING_SNAKE_CASE__								=	None
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						if self.delimiter is not None:
										lowerCAmelCase__		  =  self.delimiter
						if self.column_names is not None:
										lowerCAmelCase__		  =  self.column_names



		@property
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  {
						    'sep': self.sep,
						    'header': self.header,
						    'names': self.names,
						    'index_col': self.index_col,
						    'usecols': self.usecols,
						    'prefix': self.prefix,
						    'mangle_dupe_cols': self.mangle_dupe_cols,
						    'engine': self.engine,
						    'converters': self.converters,
						    'true_values': self.true_values,
						    'false_values': self.false_values,
						    'skipinitialspace': self.skipinitialspace,
						    'skiprows': self.skiprows,
						    'nrows': self.nrows,
						    'na_values': self.na_values,
						    'keep_default_na': self.keep_default_na,
						    'na_filter': self.na_filter,
						    'verbose': self.verbose,
						    'skip_blank_lines': self.skip_blank_lines,
						    'thousands': self.thousands,
						    'decimal': self.decimal,
						    'lineterminator': self.lineterminator,
						    'quotechar': self.quotechar,
						    'quoting': self.quoting,
						    'escapechar': self.escapechar,
						    'comment': self.comment,
						    'encoding': self.encoding,
						    'dialect': self.dialect,
						    'error_bad_lines': self.error_bad_lines,
						    'warn_bad_lines': self.warn_bad_lines,
						    'skipfooter': self.skipfooter,
						    'doublequote': self.doublequote,
						    'memory_map': self.memory_map,
						    'float_precision': self.float_precision,
						    'chunksize': self.chunksize,
						    'encoding_errors': self.encoding_errors,
						    'on_bad_lines': self.on_bad_lines,
						    'date_format': self.date_format,
						}

						# some kwargs must not be passed if they don't have a default value
						# some others are deprecated and we can also not pass them if they are the default value
						for pd_read_csv_parameter in _PANDAS_READ_CSV_NO_DEFAULT_PARAMETERS + _PANDAS_READ_CSV_DEPRECATED_PARAMETERS:
										if pd_read_csv_kwargs[pd_read_csv_parameter] == getattr(CsvConfig()    ,a_						):
														del pd_read_csv_kwargs[pd_read_csv_parameter]

        # Remove 2.0 new arguments
						if not (datasets.config.PANDAS_VERSION.major >= 2):
										for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_2_0_0_PARAMETERS:
														del pd_read_csv_kwargs[pd_read_csv_parameter]

        # Remove 1.3 new arguments
						if not (datasets.config.PANDAS_VERSION.major >= 1 and datasets.config.PANDAS_VERSION.minor >= 3):
										for pd_read_csv_parameter in _PANDAS_READ_CSV_NEW_1_3_0_PARAMETERS:
														del pd_read_csv_kwargs[pd_read_csv_parameter]

						return pd_read_csv_kwargs



class 	__snake_case   ( datasets.ArrowBasedBuilder ):
		SCREAMING_SNAKE_CASE__								=	CsvConfig
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						return datasets.DatasetInfo(features=self.config.features						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_						):







						"""simple docstring"""
						if not self.config.data_files:
										raise ValueError(f'At least one data file must be specified, but got data_files={self.config.data_files}'						)
						lowerCAmelCase__		  =  dl_manager.download_and_extract(self.config.data_files						)
						if isinstance(a_    ,(str, list, tuple)						):
										lowerCAmelCase__		  =  data_files
										if isinstance(a_    ,a_						):
														lowerCAmelCase__		  =  [files]
										lowerCAmelCase__		  =  [dl_manager.iter_files(a_						) for file in files]
										return [datasets.SplitGenerator(name=datasets.Split.TRAIN    ,gen_kwargs={'files': files}						)]
						lowerCAmelCase__		  =  []
						for split_name, files in data_files.items():
										if isinstance(a_    ,a_						):
														lowerCAmelCase__		  =  [files]
										lowerCAmelCase__		  =  [dl_manager.iter_files(a_						) for file in files]
										splits.append(datasets.SplitGenerator(name=a_    ,gen_kwargs={'files': files}						)						)
						return splits
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_						):







						"""simple docstring"""
						if self.config.features is not None:
										lowerCAmelCase__		  =  self.config.features.arrow_schema
										if all(not require_storage_cast(a_						) for feature in self.config.features.values()						):
														# cheaper cast
														lowerCAmelCase__		  =  pa.Table.from_arrays([pa_table[field.name] for field in schema]    ,schema=a_						)
										else:
														# more expensive cast; allows str <-> int/float or str to Audio for example
														lowerCAmelCase__		  =  table_cast(a_    ,a_						)
						return pa_table



		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.config.features.arrow_schema if self.config.features else None
						# dtype allows reading an int column as str
						lowerCAmelCase__		  =  (
						    {
						        name: dtype.to_pandas_dtype() if not require_storage_cast(a_						) else object
						        for name, dtype, feature in zip(schema.names    ,schema.types    ,self.config.features.values()						)
						    }
						    if schema is not None
						    else None
						)
						for file_idx, file in enumerate(itertools.chain.from_iterable(a_						)						):
										lowerCAmelCase__		  =  pd.read_csv(a_    ,iterator=a_    ,dtype=a_    ,**self.config.pd_read_csv_kwargs						)
										try:
														for batch_idx, df in enumerate(a_						):
																		lowerCAmelCase__		  =  pa.Table.from_pandas(a_						)
																		# Uncomment for debugging (will print the Arrow table size and elements)
																		# logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
																		# logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
																		yield (file_idx, batch_idx), self._cast_table(a_						)
										except ValueError as e:
														logger.error(f'Failed to read file \'{file}\' with error {type(a_						)}: {e}'						)
														raise
