|
from diffusers import DiffusionPipeline |
|
import gradio as gr |
|
import numpy as np |
|
import imageio |
|
from PIL import Image |
|
import torch |
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-2-inpainting") |
|
pipe.to(device) |
|
|
|
def resize(height,img): |
|
baseheight = height |
|
img = Image.open(img) |
|
hpercent = (baseheight/float(img.size[1])) |
|
wsize = int((float(img.size[0])*float(hpercent))) |
|
img = img.resize((wsize,baseheight), Image.Resampling.LANCZOS) |
|
return img |
|
|
|
def predict(source_img, prompt, negative_prompt, steps): |
|
imageio.imwrite("data.png", source_img["image"]) |
|
imageio.imwrite("data_mask.png", source_img["mask"]) |
|
src = resize(512, "data.png") |
|
src.save("src.png") |
|
mask = resize(512, "data_mask.png") |
|
mask.save("mask.png") |
|
image = pipe(prompt=prompt, negative_prompt=negative_prompt, image=src, mask_image=mask, num_inference_steps=steps).images[0] |
|
return image |
|
|
|
title="Stable Diffusion 2.0 Inpainting CPU" |
|
description="Inpainting with Stable Diffusion 2.0 <br />Warning: Slow process... ~10 min inference time.<br> <b>Please use 512x512 or 768x768 square .png image as input to avoid memory error!!!</b>" |
|
gr.Interface(fn=predict, inputs=[gr.Image(source="upload", type="numpy", tool="sketch", elem_id="source_container"), gr.Textbox(label='What you want the AI to Generate, 77 Token limit'), gr.Textbox(label='What you Do Not want the AI to generate'), gr.Slider(5, 25, 10, step=1, label='Number of Iterations')], outputs='image', title=title, description=description, article = "Code Monkey: <a href=\"https://huggingface.co/Manjushri\">Manjushri</a>").launch(max_threads=True, debug=True) |