Instructions to use alwold/stable-diffusion-2-inpainting with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use alwold/stable-diffusion-2-inpainting with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import AutoPipelineForInpainting from diffusers.utils import load_image # switch to "mps" for apple devices pipe = AutoPipelineForInpainting.from_pretrained("alwold/stable-diffusion-2-inpainting", dtype=torch.float16, device_map="cuda") 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 = load_image(img_url).resize((1024, 1024)) mask_image = load_image(mask_url).resize((1024, 1024)) prompt = "a tiger sitting on a park bench" generator = torch.Generator(device="cuda").manual_seed(0) image = pipe( prompt=prompt, image=image, mask_image=mask_image, guidance_scale=8.0, num_inference_steps=20, # steps between 15 and 30 work well for us strength=0.99, # make sure to use `strength` below 1.0 generator=generator, ).images[0] - Notebooks
- Google Colab
- Kaggle
Download handler.py from alwold/stable-diffusion-2-inpainting: direct link, hf CLI and curl.
- Browser
- Download file 1.08 kB
-
https://huggingface.co/alwold/stable-diffusion-2-inpainting/resolve/main/handler.py
- Command line
-
hf download hf://alwold/stable-diffusion-2-inpainting/handler.py
-
curl -L -o handler.py https://huggingface.co/alwold/stable-diffusion-2-inpainting/resolve/main/handler.py
1.08 kB
| from typing import Dict, List, Any | |
| from diffusers import DiffusionPipeline, EulerAncestralDiscreteScheduler | |
| import torch | |
| import base64 | |
| from PIL import Image | |
| import io | |
| class EndpointHandler(): | |
| def __init__(self, path=""): | |
| scheduler = EulerAncestralDiscreteScheduler.from_pretrained(path, subfolder="scheduler") | |
| self.pipeline = DiffusionPipeline.from_pretrained(path, scheduler=scheduler, torch_dtype=torch.float32) | |
| if torch.cuda.is_available(): | |
| self.pipeline.to("cuda") | |
| def __call__(self, data: Dict[str, any]) -> List[Dict[str, Any]]: | |
| mask_bytes = base64.b64decode(data["inputs"]["mask"]) | |
| mask = Image.open(io.BytesIO(mask_bytes)).convert('RGB') | |
| image_bytes = base64.b64decode(data["inputs"]["image"]) | |
| image = Image.open(io.BytesIO(image_bytes)).convert('RGB') | |
| prompt = "a picture of a person with a nice haircut" | |
| new_image = self.pipeline(prompt=prompt, image=image, mask_image=mask, height=512, width=512, num_inference_steps=20).images[0] | |
| return [{"image": new_image}] | |