File size: 1,403 Bytes
98f4d1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)

source_img = gr.Image(source="upload", type="numpy", tool="sketch", elem_id="source_container");

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.LANCZOS)
  return img

def predict(source_img, prompt):
    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, image=src, mask_image=mask, strength=0.75, num_inference_steps=10).images[0]   
    
    return image


title="Stable Diffusion 2.0 Inpainting CPU"
description="Inpainting with Stable Diffusion 2.0 <br />Warning: Slow process... ~5-10 min inference time.<br> <b>Please use 512*512 or 768x768 square .png image as input to avoid memory error!!!</b>"
gr.Interface(fn=predict, inputs=[source_img, "text"], outputs='image', title=title, description=description).launch(debug=True)