nightfury commited on
Commit
c01ad90
1 Parent(s): c7d0c87

Create new file

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ #import torch
3
+ #from torch import autocast // only for GPU
4
+
5
+ from PIL import Image
6
+ import numpy as np
7
+ from io import BytesIO
8
+ import os
9
+ MY_SECRET_TOKEN=os.environ.get('HF_TOKEN_SD')
10
+
11
+ #from diffusers import StableDiffusionPipeline
12
+ from diffusers import StableDiffusionImg2ImgPipeline
13
+
14
+ print("hello sylvain")
15
+
16
+ YOUR_TOKEN=MY_SECRET_TOKEN
17
+
18
+ device="cpu"
19
+
20
+ #prompt_pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=YOUR_TOKEN)
21
+ #prompt_pipe.to(device)
22
+
23
+ img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=YOUR_TOKEN)
24
+ img_pipe.to(device)
25
+
26
+ source_img = gr.ImagePaint(type="filepath", elem_id="source_container", label="new gradio color sketch (beta)")
27
+
28
+ gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[1], height="auto")
29
+
30
+ def resize(value,img):
31
+ #baseheight = value
32
+ img = Image.open(img)
33
+ #hpercent = (baseheight/float(img.size[1]))
34
+ #wsize = int((float(img.size[0])*float(hpercent)))
35
+ #img = img.resize((wsize,baseheight), Image.Resampling.LANCZOS)
36
+ img = img.resize((value,value), Image.Resampling.LANCZOS)
37
+ return img
38
+
39
+
40
+ def infer(source_img, prompt):
41
+
42
+ source_image = resize(512, source_img)
43
+ source_image.save('source.png')
44
+ images_list = img_pipe([prompt] * 1, init_image=source_image, strength=0.75)
45
+ images = []
46
+ safe_image = Image.open(r"unsafe.png")
47
+ for i, image in enumerate(images_list["sample"]):
48
+ if(images_list["nsfw_content_detected"][i]):
49
+ images.append(safe_image)
50
+ else:
51
+ images.append(image)
52
+ return images
53
+
54
+ print("Great sylvain ! Everything is working fine !")
55
+
56
+ title="Touch of Paint Stable Diffusion CPU"
57
+ description="Img-2-Img Stable Diffusion example using CPU and the beta color-sketch on uploaded gradio tool. <br />Warning: Slow process... ~5/10 min inference time. <b>NSFW filter enabled.</b>"
58
+ custom_css = "style.css"
59
+
60
+ gr.Interface(fn=infer, inputs=[source_img, "text"], outputs=gallery,title=title,description=description,css=custom_css).queue(max_size=100).launch(enable_queue=True)