


"""simple docstring"""







from typing import Callable, List, Optional, Union

import PIL
import torch
from transformers import (
    CLIPImageProcessor,
    CLIPSegForImageSegmentation,
    CLIPSegProcessor,
    CLIPTextModel,
    CLIPTokenizer,
)

from diffusers import DiffusionPipeline
from diffusers.configuration_utils import FrozenDict
from diffusers.models import AutoencoderKL, UNetaDConditionModel
from diffusers.pipelines.stable_diffusion import StableDiffusionInpaintPipeline
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler
from diffusers.utils import deprecate, is_accelerate_available, logging


lowerCAmelCase__				    =       logging.get_logger(__name__)  # pylint: disable=invalid-name


class 				SCREAMING_SNAKE_CASE__    (		lowercase    ):
       """simple docstring"""


       def __init__(						self		,					snake_case__		,					snake_case__		,					snake_case__		,					snake_case__		,					snake_case__		,					snake_case__		,					snake_case__		,					snake_case__		,					snake_case__		,					):






         """simple docstring"""





         super().__init__()

         if hasattr(scheduler.config		,					"steps_offset"						) and scheduler.config.steps_offset != 1:
           lowerCAmelCase					:					Dict								=							(
               f"""The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`"""
               f""" should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure """
               "to update the config accordingly as leaving `steps_offset` might led to incorrect results"
               " in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,"
               " it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`"
               " file"
           )
           deprecate("steps_offset!=1"		,					"1.0.0"		,					snake_case__		,					standard_warn=snake_case__						)
           lowerCAmelCase					:					List[Any]								=							dict(scheduler.config						)
           lowerCAmelCase					:					Dict								=							1
           lowerCAmelCase					:					Tuple								=							FrozenDict(snake_case__						)

         if hasattr(scheduler.config		,					"skip_prk_steps"						) and scheduler.config.skip_prk_steps is False:
           lowerCAmelCase					:					Any								=							(
               f"""The configuration file of this scheduler: {scheduler} has not set the configuration"""
               " `skip_prk_steps`. `skip_prk_steps` should be set to True in the configuration file. Please make"
               " sure to update the config accordingly as not setting `skip_prk_steps` in the config might lead to"
               " incorrect results in future versions. If you have downloaded this checkpoint from the Hugging Face"
               " Hub, it would be very nice if you could open a Pull request for the"
               " `scheduler/scheduler_config.json` file"
           )
           deprecate("skip_prk_steps not set"		,					"1.0.0"		,					snake_case__		,					standard_warn=snake_case__						)
           lowerCAmelCase					:					str								=							dict(scheduler.config						)
           lowerCAmelCase					:					Tuple								=							True
           lowerCAmelCase					:					Dict								=							FrozenDict(snake_case__						)

         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(
             segmentation_model=snake_case__		,					segmentation_processor=snake_case__		,					vae=snake_case__		,					text_encoder=snake_case__		,					tokenizer=snake_case__		,					unet=snake_case__		,					scheduler=snake_case__		,					safety_checker=snake_case__		,					feature_extractor=snake_case__		,					)

       def 	lowercase__							(						self		,					snake_case__ = "auto"						):






         """simple docstring"""





         if slice_size == "auto":
           # half the attention head size is usually a good trade-off between
           # speed and memory
           lowerCAmelCase					:					str								=							self.unet.config.attention_head_dim // 2
         self.unet.set_attention_slice(snake_case__						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         self.enable_attention_slicing(snake_case__						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         if is_accelerate_available():
           from accelerate import cpu_offload
         else:
           raise ImportError("Please install accelerate via `pip install accelerate`"						)

         lowerCAmelCase					:					int								=							torch.device("cuda"						)

         for cpu_offloaded_model in [self.unet, self.text_encoder, self.vae, self.safety_checker]:
           if cpu_offloaded_model is not None:
             cpu_offload(snake_case__		,					snake_case__						)

       @property
       # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
       def 	lowercase__							(						self						):






         """simple docstring"""





         if self.device != torch.device("meta"						) or not hasattr(self.unet		,					"_hf_hook"						):
           return self.device
         for module in self.unet.modules():
           if (
               hasattr(snake_case__		,					"_hf_hook"						)
               and hasattr(module._hf_hook		,					"execution_device"						)
               and module._hf_hook.execution_device is not None
           ):
             return torch.device(module._hf_hook.execution_device						)
         return self.device







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






         """simple docstring"""





         lowerCAmelCase					:					int								=							self.segmentation_processor(
             text=[text]		,					images=[image]		,					padding="max_length"		,					return_tensors="pt"						).to(self.device						)
         lowerCAmelCase					:					Dict								=							self.segmentation_model(**snake_case__						)
         lowerCAmelCase					:					str								=							torch.sigmoid(outputs.logits						).cpu().detach().unsqueeze(-1						).numpy()
         lowerCAmelCase					:					Optional[Any]								=							self.numpy_to_pil(snake_case__						)[0].resize(image.size						)

         # Run inpainting pipeline with the generated mask
         lowerCAmelCase					:					Any								=							StableDiffusionInpaintPipeline(
             vae=self.vae		,					text_encoder=self.text_encoder		,					tokenizer=self.tokenizer		,					unet=self.unet		,					scheduler=self.scheduler		,					safety_checker=self.safety_checker		,					feature_extractor=self.feature_extractor		,					)
         return inpainting_pipeline(
             prompt=snake_case__		,					image=snake_case__		,					mask_image=snake_case__		,					height=snake_case__		,					width=snake_case__		,					num_inference_steps=snake_case__		,					guidance_scale=snake_case__		,					negative_prompt=snake_case__		,					num_images_per_prompt=snake_case__		,					eta=snake_case__		,					generator=snake_case__		,					latents=snake_case__		,					output_type=snake_case__		,					return_dict=snake_case__		,					callback=snake_case__		,					callback_steps=snake_case__		,					)









"""simple docstring"""







import multiprocessing
from typing import TYPE_CHECKING, Optional, Union

from .. import Dataset, Features, config
from ..formatting import query_table
from ..packaged_modules.sql.sql import Sql
from ..utils import logging
from .abc import AbstractDatasetInputStream


if TYPE_CHECKING:
     import sqlitea

     import sqlalchemy


class 				SCREAMING_SNAKE_CASE__    (		lowercase    ):
       """simple docstring"""


       def __init__(						self		,					snake_case__		,					snake_case__		,					snake_case__ = None		,					snake_case__ = None		,					snake_case__ = False		,					**snake_case__		,					):






         """simple docstring"""





         super().__init__(features=snake_case__		,					cache_dir=snake_case__		,					keep_in_memory=snake_case__		,					**snake_case__						)
         lowerCAmelCase					:					Dict								=							Sql(
             cache_dir=snake_case__		,					features=snake_case__		,					sql=snake_case__		,					con=snake_case__		,					**snake_case__		,					)







       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Tuple								=							None
         lowerCAmelCase					:					Optional[int]								=							None
         lowerCAmelCase					:					str								=							None
         lowerCAmelCase					:					int								=							None

         self.builder.download_and_prepare(
             download_config=snake_case__		,					download_mode=snake_case__		,					verification_mode=snake_case__		,					base_path=snake_case__		,					)

         # Build dataset for splits
         lowerCAmelCase					:					str								=							self.builder.as_dataset(
             split="train"		,					verification_mode=snake_case__		,					in_memory=self.keep_in_memory						)
         return dataset


class 				SCREAMING_SNAKE_CASE__    :
       """simple docstring"""


       def __init__(						self		,					snake_case__		,					snake_case__		,					snake_case__		,					snake_case__ = None		,					snake_case__ = None		,					**snake_case__		,					):






         """simple docstring"""





         if num_proc is not None and num_proc <= 0:
           raise ValueError(f"""num_proc {num_proc} must be an integer > 0."""						)

         lowerCAmelCase					:					Tuple								=							dataset
         lowerCAmelCase					:					Tuple								=							name
         lowerCAmelCase					:					List[str]								=							con
         lowerCAmelCase					:					List[Any]								=							batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
         lowerCAmelCase					:					Optional[int]								=							num_proc
         lowerCAmelCase					:					Optional[int]								=							to_sql_kwargs

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					List[str]								=							self.to_sql_kwargs.pop("sql"		,					snake_case__						)
         lowerCAmelCase					:					List[Any]								=							self.to_sql_kwargs.pop("con"		,					snake_case__						)
         lowerCAmelCase					:					Dict								=							self.to_sql_kwargs.pop("index"		,					snake_case__						)

         lowerCAmelCase					:					Dict								=							self._write(index=snake_case__		,					**self.to_sql_kwargs						)
         return written

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase						,  lowerCAmelCase						,  lowerCAmelCase					:					Optional[int]								=							args
         lowerCAmelCase					:					Optional[int]								=							{**to_sql_kwargs, "if_exists": "append"} if offset > 0 else to_sql_kwargs
         lowerCAmelCase					:					List[Any]								=							query_table(
             table=self.dataset.data		,					key=slice(snake_case__		,					offset + self.batch_size						)		,					indices=self.dataset._indices		,					)
         lowerCAmelCase					:					Tuple								=							batch.to_pandas()
         lowerCAmelCase					:					str								=							df.to_sql(self.name		,					self.con		,					index=snake_case__		,					**snake_case__						)
         return num_rows or len(snake_case__						)







       def 	lowercase__							(						self		,					snake_case__		,					**snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					Tuple								=							0

         if self.num_proc is None or self.num_proc == 1:
           for offset in logging.tqdm(
               range(0		,					len(self.dataset						)		,					self.batch_size						)		,					unit="ba"		,					disable=not logging.is_progress_bar_enabled()		,					desc="Creating SQL from Arrow format"		,					):
             written += self._batch_sql((offset, index, to_sql_kwargs)						)
         else:
           lowerCAmelCase						,  lowerCAmelCase					:					Dict								=							len(self.dataset						), self.batch_size
           with multiprocessing.Pool(self.num_proc						) as pool:
             for num_rows in logging.tqdm(
                 pool.imap(
                     self._batch_sql		,					[(offset, index, to_sql_kwargs) for offset in range(0		,					snake_case__		,					snake_case__						)]		,					)		,					total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size		,					unit="ba"		,					disable=not logging.is_progress_bar_enabled()		,					desc="Creating SQL from Arrow format"		,					):
               written += num_rows

         return written





