CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
huggingface

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: huggingface/notebooks
Path: blob/main/diffusers/in_painting_with_stable_diffusion_using_diffusers.ipynb
Views: 2535
Kernel: Python 3

Open In Colab

In-painting pipeline for Stable Diffusion using 🧨 Diffusers

This notebook shows how to do text-guided in-painting with Stable Diffusion model using 🤗 Hugging Face 🧨 Diffusers library.

For a general introduction to the Stable Diffusion model please refer to this colab.

!pip install -qq -U diffusers==0.11.1 transformers ftfy gradio accelerate

To use private and gated models on 🤗 Hugging Face Hub, login is required. If you are only using a public checkpoint (such as runwayml/stable-diffusion-inpainting in this notebook), you can skip this step.

from huggingface_hub import notebook_login notebook_login()
Login successful Your token has been saved to /root/.huggingface/token
import inspect from typing import List, Optional, Union import numpy as np import torch import PIL import gradio as gr from diffusers import StableDiffusionInpaintPipeline
device = "cuda" model_path = "runwayml/stable-diffusion-inpainting" pipe = StableDiffusionInpaintPipeline.from_pretrained( model_path, torch_dtype=torch.float16, ).to(device)
import requests from io import BytesIO def image_grid(imgs, rows, cols): assert len(imgs) == rows*cols w, h = imgs[0].size grid = PIL.Image.new('RGB', size=(cols*w, rows*h)) grid_w, grid_h = grid.size for i, img in enumerate(imgs): grid.paste(img, box=(i%cols*w, i//cols*h)) return grid def download_image(url): response = requests.get(url) return PIL.Image.open(BytesIO(response.content)).convert("RGB") img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png" mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"
image = download_image(img_url).resize((512, 512)) image
Image in a Jupyter notebook
mask_image = download_image(mask_url).resize((512, 512)) mask_image
Image in a Jupyter notebook
prompt = "a mecha robot sitting on a bench" guidance_scale=7.5 num_samples = 3 generator = torch.Generator(device="cuda").manual_seed(0) # change the seed to get different results images = pipe( prompt=prompt, image=image, mask_image=mask_image, guidance_scale=guidance_scale, generator=generator, num_images_per_prompt=num_samples, ).images
# insert initial image in the list so we can compare side by side images.insert(0, image)
image_grid(images, 1, num_samples + 1)
Image in a Jupyter notebook

Gradio Demo

def predict(dict, prompt): image = dict['image'].convert("RGB").resize((512, 512)) mask_image = dict['mask'].convert("RGB").resize((512, 512)) images = pipe(prompt=prompt, image=image, mask_image=mask_image).images return(images[0])
gr.Interface( predict, title = 'Stable Diffusion In-Painting', inputs=[ gr.Image(source = 'upload', tool = 'sketch', type = 'pil'), gr.Textbox(label = 'prompt') ], outputs = [ gr.Image() ] ).launch(debug=True)
Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch(). Running on public URL: https://e52f060882d60b09.gradio.app This share link expires in 72 hours. For free permanent hosting and GPU upgrades (NEW!), check out Spaces: https://huggingface.co/spaces
Keyboard interruption in main thread... closing server.
(<gradio.routes.App at 0x7eff58b4ef90>, 'http://127.0.0.1:7860/', 'https://e52f060882d60b09.gradio.app')