CoolFace
Datasetpublic

diffusers/community-pipelines-mirror

Community Pipeline Examples For more information about community pipelines, please have a look at this issue. Community pipeline examples consist pipelines that have been added by the community. Please have a look at the following tables to get an overview of all community examples. Click on the Code Example to get a copy-and-paste ready code example that you can try out. If a community pipeline doesn't work as expected, please open an issue and ping the author on it. Please… See the full description on the dataset page: https://huggingface.co/datasets/diffusers/community-pipelines-mirror.

sourceHugging Faceupdated 29d agoView on Hugging Face
9likes22kdownloads
multilingual_stable_diffusion.py411 linesDownload Raw Back to v0.29.0
1import inspect2from typing import Callable, List, Optional, Union3 4import torch5from transformers import (6    CLIPImageProcessor,7    CLIPTextModel,8    CLIPTokenizer,9    MBart50TokenizerFast,10    MBartForConditionalGeneration,11    pipeline,12)13 14from diffusers.configuration_utils import FrozenDict15from diffusers.models import AutoencoderKL, UNet2DConditionModel16from diffusers.pipelines.pipeline_utils import DiffusionPipeline, StableDiffusionMixin17from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput18from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker19from diffusers.schedulers import DDIMScheduler, LMSDiscreteScheduler, PNDMScheduler20from diffusers.utils import deprecate, logging21 22 23logger = logging.get_logger(__name__)  # pylint: disable=invalid-name24 25 26def detect_language(pipe, prompt, batch_size):27    """helper function to detect language(s) of prompt"""28 29    if batch_size == 1:30        preds = pipe(prompt, top_k=1, truncation=True, max_length=128)31        return preds[0]["label"]32    else:33        detected_languages = []34        for p in prompt:35            preds = pipe(p, top_k=1, truncation=True, max_length=128)36            detected_languages.append(preds[0]["label"])37 38        return detected_languages39 40 41def translate_prompt(prompt, translation_tokenizer, translation_model, device):42    """helper function to translate prompt to English"""43 44    encoded_prompt = translation_tokenizer(prompt, return_tensors="pt").to(device)45    generated_tokens = translation_model.generate(**encoded_prompt, max_new_tokens=1000)46    en_trans = translation_tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)47 48    return en_trans[0]49 50 51class MultilingualStableDiffusion(DiffusionPipeline, StableDiffusionMixin):52    r"""53    Pipeline for text-to-image generation using Stable Diffusion in different languages.54 55    This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods the56    library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)57 58    Args:59        detection_pipeline ([`pipeline`]):60            Transformers pipeline to detect prompt's language.61        translation_model ([`MBartForConditionalGeneration`]):62            Model to translate prompt to English, if necessary. Please refer to the63            [model card](https://huggingface.co/docs/transformers/model_doc/mbart) for details.64        translation_tokenizer ([`MBart50TokenizerFast`]):65            Tokenizer of the translation model.66        vae ([`AutoencoderKL`]):67            Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.68        text_encoder ([`CLIPTextModel`]):69            Frozen text-encoder. Stable Diffusion uses the text portion of70            [CLIP](https://huggingface.co/docs/transformers/model_doc/clip#transformers.CLIPTextModel), specifically71            the [clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14) variant.72        tokenizer (`CLIPTokenizer`):73            Tokenizer of class74            [CLIPTokenizer](https://huggingface.co/docs/transformers/v4.21.0/en/model_doc/clip#transformers.CLIPTokenizer).75        unet ([`UNet2DConditionModel`]): Conditional U-Net architecture to denoise the encoded image latents.76        scheduler ([`SchedulerMixin`]):77            A scheduler to be used in combination with `unet` to denoise the encoded image latens. Can be one of78            [`DDIMScheduler`], [`LMSDiscreteScheduler`], or [`PNDMScheduler`].79        safety_checker ([`StableDiffusionSafetyChecker`]):80            Classification module that estimates whether generated images could be considered offensive or harmful.81            Please, refer to the [model card](https://huggingface.co/runwayml/stable-diffusion-v1-5) for details.82        feature_extractor ([`CLIPImageProcessor`]):83            Model that extracts features from generated images to be used as inputs for the `safety_checker`.84    """85 86    def __init__(87        self,88        detection_pipeline: pipeline,89        translation_model: MBartForConditionalGeneration,90        translation_tokenizer: MBart50TokenizerFast,91        vae: AutoencoderKL,92        text_encoder: CLIPTextModel,93        tokenizer: CLIPTokenizer,94        unet: UNet2DConditionModel,95        scheduler: Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler],96        safety_checker: StableDiffusionSafetyChecker,97        feature_extractor: CLIPImageProcessor,98    ):99        super().__init__()100 101        if hasattr(scheduler.config, "steps_offset") and scheduler.config.steps_offset != 1:102            deprecation_message = (103                f"The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`"104                f" should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure "105                "to update the config accordingly as leaving `steps_offset` might led to incorrect results"106                " in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,"107                " it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`"108                " file"109            )110            deprecate("steps_offset!=1", "1.0.0", deprecation_message, standard_warn=False)111            new_config = dict(scheduler.config)112            new_config["steps_offset"] = 1113            scheduler._internal_dict = FrozenDict(new_config)114 115        if safety_checker is None:116            logger.warning(117                f"You have disabled the safety checker for {self.__class__} by passing `safety_checker=None`. Ensure"118                " that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered"119                " results in services or applications open to the public. Both the diffusers team and Hugging Face"120                " strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling"121                " it only for use-cases that involve analyzing network behavior or auditing its results. For more"122                " information, please have a look at https://github.com/huggingface/diffusers/pull/254 ."123            )124 125        self.register_modules(126            detection_pipeline=detection_pipeline,127            translation_model=translation_model,128            translation_tokenizer=translation_tokenizer,129            vae=vae,130            text_encoder=text_encoder,131            tokenizer=tokenizer,132            unet=unet,133            scheduler=scheduler,134            safety_checker=safety_checker,135            feature_extractor=feature_extractor,136        )137 138    @torch.no_grad()139    def __call__(140        self,141        prompt: Union[str, List[str]],142        height: int = 512,143        width: int = 512,144        num_inference_steps: int = 50,145        guidance_scale: float = 7.5,146        negative_prompt: Optional[Union[str, List[str]]] = None,147        num_images_per_prompt: Optional[int] = 1,148        eta: float = 0.0,149        generator: Optional[torch.Generator] = None,150        latents: Optional[torch.Tensor] = None,151        output_type: Optional[str] = "pil",152        return_dict: bool = True,153        callback: Optional[Callable[[int, int, torch.Tensor], None]] = None,154        callback_steps: int = 1,155        **kwargs,156    ):157        r"""158        Function invoked when calling the pipeline for generation.159 160        Args:161            prompt (`str` or `List[str]`):162                The prompt or prompts to guide the image generation. Can be in different languages.163            height (`int`, *optional*, defaults to 512):164                The height in pixels of the generated image.165            width (`int`, *optional*, defaults to 512):166                The width in pixels of the generated image.167            num_inference_steps (`int`, *optional*, defaults to 50):168                The number of denoising steps. More denoising steps usually lead to a higher quality image at the169                expense of slower inference.170            guidance_scale (`float`, *optional*, defaults to 7.5):171                Guidance scale as defined in [Classifier-Free Diffusion Guidance](https://arxiv.org/abs/2207.12598).172                `guidance_scale` is defined as `w` of equation 2. of [Imagen173                Paper](https://arxiv.org/pdf/2205.11487.pdf). Guidance scale is enabled by setting `guidance_scale >174                1`. Higher guidance scale encourages to generate images that are closely linked to the text `prompt`,175                usually at the expense of lower image quality.176            negative_prompt (`str` or `List[str]`, *optional*):177                The prompt or prompts not to guide the image generation. Ignored when not using guidance (i.e., ignored178                if `guidance_scale` is less than `1`).179            num_images_per_prompt (`int`, *optional*, defaults to 1):180                The number of images to generate per prompt.181            eta (`float`, *optional*, defaults to 0.0):182                Corresponds to parameter eta (η) in the DDIM paper: https://arxiv.org/abs/2010.02502. Only applies to183                [`schedulers.DDIMScheduler`], will be ignored for others.184            generator (`torch.Generator`, *optional*):185                A [torch generator](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make generation186                deterministic.187            latents (`torch.Tensor`, *optional*):188                Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image189                generation. Can be used to tweak the same generation with different prompts. If not provided, a latents190                tensor will ge generated by sampling using the supplied random `generator`.191            output_type (`str`, *optional*, defaults to `"pil"`):192                The output format of the generate image. Choose between193                [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.194            return_dict (`bool`, *optional*, defaults to `True`):195                Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a196                plain tuple.197            callback (`Callable`, *optional*):198                A function that will be called every `callback_steps` steps during inference. The function will be199                called with the following arguments: `callback(step: int, timestep: int, latents: torch.Tensor)`.200            callback_steps (`int`, *optional*, defaults to 1):201                The frequency at which the `callback` function will be called. If not specified, the callback will be202                called at every step.203 204        Returns:205            [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] or `tuple`:206            [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] if `return_dict` is True, otherwise a `tuple.207            When returning a tuple, the first element is a list with the generated images, and the second element is a208            list of `bool`s denoting whether the corresponding generated image likely represents "not-safe-for-work"209            (nsfw) content, according to the `safety_checker`.210        """211        if isinstance(prompt, str):212            batch_size = 1213        elif isinstance(prompt, list):214            batch_size = len(prompt)215        else:216            raise ValueError(f"`prompt` has to be of type `str` or `list` but is {type(prompt)}")217 218        if height % 8 != 0 or width % 8 != 0:219            raise ValueError(f"`height` and `width` have to be divisible by 8 but are {height} and {width}.")220 221        if (callback_steps is None) or (222            callback_steps is not None and (not isinstance(callback_steps, int) or callback_steps <= 0)223        ):224            raise ValueError(225                f"`callback_steps` has to be a positive integer but is {callback_steps} of type"226                f" {type(callback_steps)}."227            )228 229        # detect language and translate if necessary230        prompt_language = detect_language(self.detection_pipeline, prompt, batch_size)231        if batch_size == 1 and prompt_language != "en":232            prompt = translate_prompt(prompt, self.translation_tokenizer, self.translation_model, self.device)233 234        if isinstance(prompt, list):235            for index in range(batch_size):236                if prompt_language[index] != "en":237                    p = translate_prompt(238                        prompt[index], self.translation_tokenizer, self.translation_model, self.device239                    )240                    prompt[index] = p241 242        # get prompt text embeddings243        text_inputs = self.tokenizer(244            prompt,245            padding="max_length",246            max_length=self.tokenizer.model_max_length,247            return_tensors="pt",248        )249        text_input_ids = text_inputs.input_ids250 251        if text_input_ids.shape[-1] > self.tokenizer.model_max_length:252            removed_text = self.tokenizer.batch_decode(text_input_ids[:, self.tokenizer.model_max_length :])253            logger.warning(254                "The following part of your input was truncated because CLIP can only handle sequences up to"255                f" {self.tokenizer.model_max_length} tokens: {removed_text}"256            )257            text_input_ids = text_input_ids[:, : self.tokenizer.model_max_length]258        text_embeddings = self.text_encoder(text_input_ids.to(self.device))[0]259 260        # duplicate text embeddings for each generation per prompt, using mps friendly method261        bs_embed, seq_len, _ = text_embeddings.shape262        text_embeddings = text_embeddings.repeat(1, num_images_per_prompt, 1)263        text_embeddings = text_embeddings.view(bs_embed * num_images_per_prompt, seq_len, -1)264 265        # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)266        # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`267        # corresponds to doing no classifier free guidance.268        do_classifier_free_guidance = guidance_scale > 1.0269        # get unconditional embeddings for classifier free guidance270        if do_classifier_free_guidance:271            uncond_tokens: List[str]272            if negative_prompt is None:273                uncond_tokens = [""] * batch_size274            elif type(prompt) is not type(negative_prompt):275                raise TypeError(276                    f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !="277                    f" {type(prompt)}."278                )279            elif isinstance(negative_prompt, str):280                # detect language and translate it if necessary281                negative_prompt_language = detect_language(self.detection_pipeline, negative_prompt, batch_size)282                if negative_prompt_language != "en":283                    negative_prompt = translate_prompt(284                        negative_prompt, self.translation_tokenizer, self.translation_model, self.device285                    )286                if isinstance(negative_prompt, str):287                    uncond_tokens = [negative_prompt]288            elif batch_size != len(negative_prompt):289                raise ValueError(290                    f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:"291                    f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"292                    " the batch size of `prompt`."293                )294            else:295                # detect language and translate it if necessary296                if isinstance(negative_prompt, list):297                    negative_prompt_languages = detect_language(self.detection_pipeline, negative_prompt, batch_size)298                    for index in range(batch_size):299                        if negative_prompt_languages[index] != "en":300                            p = translate_prompt(301                                negative_prompt[index], self.translation_tokenizer, self.translation_model, self.device302                            )303                            negative_prompt[index] = p304                uncond_tokens = negative_prompt305 306            max_length = text_input_ids.shape[-1]307            uncond_input = self.tokenizer(308                uncond_tokens,309                padding="max_length",310                max_length=max_length,311                truncation=True,312                return_tensors="pt",313            )314            uncond_embeddings = self.text_encoder(uncond_input.input_ids.to(self.device))[0]315 316            # duplicate unconditional embeddings for each generation per prompt, using mps friendly method317            seq_len = uncond_embeddings.shape[1]318            uncond_embeddings = uncond_embeddings.repeat(1, num_images_per_prompt, 1)319            uncond_embeddings = uncond_embeddings.view(batch_size * num_images_per_prompt, seq_len, -1)320 321            # For classifier free guidance, we need to do two forward passes.322            # Here we concatenate the unconditional and text embeddings into a single batch323            # to avoid doing two forward passes324            text_embeddings = torch.cat([uncond_embeddings, text_embeddings])325 326        # get the initial random noise unless the user supplied it327 328        # Unlike in other pipelines, latents need to be generated in the target device329        # for 1-to-1 results reproducibility with the CompVis implementation.330        # However this currently doesn't work in `mps`.331        latents_shape = (batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8)332        latents_dtype = text_embeddings.dtype333        if latents is None:334            if self.device.type == "mps":335                # randn does not work reproducibly on mps336                latents = torch.randn(latents_shape, generator=generator, device="cpu", dtype=latents_dtype).to(337                    self.device338                )339            else:340                latents = torch.randn(latents_shape, generator=generator, device=self.device, dtype=latents_dtype)341        else:342            if latents.shape != latents_shape:343                raise ValueError(f"Unexpected latents shape, got {latents.shape}, expected {latents_shape}")344            latents = latents.to(self.device)345 346        # set timesteps347        self.scheduler.set_timesteps(num_inference_steps)348 349        # Some schedulers like PNDM have timesteps as arrays350        # It's more optimized to move all timesteps to correct device beforehand351        timesteps_tensor = self.scheduler.timesteps.to(self.device)352 353        # scale the initial noise by the standard deviation required by the scheduler354        latents = latents * self.scheduler.init_noise_sigma355 356        # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature357        # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.358        # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502359        # and should be between [0, 1]360        accepts_eta = "eta" in set(inspect.signature(self.scheduler.step).parameters.keys())361        extra_step_kwargs = {}362        if accepts_eta:363            extra_step_kwargs["eta"] = eta364 365        for i, t in enumerate(self.progress_bar(timesteps_tensor)):366            # expand the latents if we are doing classifier free guidance367            latent_model_input = torch.cat([latents] * 2) if do_classifier_free_guidance else latents368            latent_model_input = self.scheduler.scale_model_input(latent_model_input, t)369 370            # predict the noise residual371            noise_pred = self.unet(latent_model_input, t, encoder_hidden_states=text_embeddings).sample372 373            # perform guidance374            if do_classifier_free_guidance:375                noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)376                noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)377 378            # compute the previous noisy sample x_t -> x_t-1379            latents = self.scheduler.step(noise_pred, t, latents, **extra_step_kwargs).prev_sample380 381            # call the callback, if provided382            if callback is not None and i % callback_steps == 0:383                step_idx = i // getattr(self.scheduler, "order", 1)384                callback(step_idx, t, latents)385 386        latents = 1 / 0.18215 * latents387        image = self.vae.decode(latents).sample388 389        image = (image / 2 + 0.5).clamp(0, 1)390 391        # we always cast to float32 as this does not cause significant overhead and is compatible with bfloat16392        image = image.cpu().permute(0, 2, 3, 1).float().numpy()393 394        if self.safety_checker is not None:395            safety_checker_input = self.feature_extractor(self.numpy_to_pil(image), return_tensors="pt").to(396                self.device397            )398            image, has_nsfw_concept = self.safety_checker(399                images=image, clip_input=safety_checker_input.pixel_values.to(text_embeddings.dtype)400            )401        else:402            has_nsfw_concept = None403 404        if output_type == "pil":405            image = self.numpy_to_pil(image)406 407        if not return_dict:408            return (image, has_nsfw_concept)409 410        return StableDiffusionPipelineOutput(images=image, nsfw_content_detected=has_nsfw_concept)411