File size: 1,950 Bytes
d8b5811
13a751b
2136b2c
13a751b
 
2704d8f
 
 
 
 
 
 
 
 
 
 
 
319b36e
2704d8f
13a751b
649dc8d
2704d8f
 
13a751b
 
 
 
 
 
 
 
0372807
aa9649d
cf29b6f
 
 
281204b
cf29b6f
281204b
2704d8f
59f5e84
2704d8f
 
d8b5811
2704d8f
 
 
 
 
49e4d1e
 
c0d26a6
d989292
f5fe334
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from diffusers import DiffusionPipeline
import gradio as gr
import numpy as np
import imageio
from PIL import Image
from io import BytesIO
import os

MY_SECRET_TOKEN=os.environ.get('HF_TOKEN_SD')


print("hello sylvain")

YOUR_TOKEN=MY_SECRET_TOKEN

device="cpu"

pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-inpainting", use_auth_token=YOUR_TOKEN)
pipe.to(device)

source_img = gr.Image(source="upload", type="numpy", tool="sketch", elem_id="source_container");
gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[2], height="auto")

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):
    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")
    
    images_list = pipe([prompt] * 1, image=src, mask_image=mask, strength=0.75)
    images = []
    safe_image = Image.open(r"unsafe.png")
    for i, image in enumerate(images_list["images"]):
        if(images_list["nsfw_content_detected"][i]):
            images.append(safe_image)
        else:
            images.append(image)    
    return images

custom_css="style.css"
title="InPainting Stable Diffusion CPU"
description="Inpainting Stable Diffusion example using CPU and HF token. <br />Warning: Slow process... ~5/10 min inference time. <b>NSFW filter enabled.</b><br />Please use 512*512 square image as input to avoid memory error !" 
gr.Interface(fn=predict, inputs=[source_img, "text"], outputs=gallery, css=custom_css, title=title, description=description, allow_flagging="manual").launch(enable_queue=True)