TrLOX commited on
Commit
210deb3
1 Parent(s): 6d6864e

Create new file

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.Image(source="upload", type="filepath", label="init_img | 512*512 px")
27
+ gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[2], height="auto")
28
+
29
+ def resize(value,img):
30
+ #baseheight = value
31
+ img = Image.open(img)
32
+ #hpercent = (baseheight/float(img.size[1]))
33
+ #wsize = int((float(img.size[0])*float(hpercent)))
34
+ #img = img.resize((wsize,baseheight), Image.Resampling.LANCZOS)
35
+ img = img.resize((value,value), Image.Resampling.LANCZOS)
36
+ return img
37
+
38
+
39
+ def infer(prompt, source_img):
40
+
41
+ source_image = resize(512, source_img)
42
+ source_image.save('source.png')
43
+ images_list = img_pipe([prompt] * 2, init_image=source_image, strength=0.75)
44
+ images = []
45
+ safe_image = Image.open(r"unsafe.png")
46
+ for i, image in enumerate(images_list["sample"]):
47
+ if(images_list["nsfw_content_detected"][i]):
48
+ images.append(safe_image)
49
+ else:
50
+ images.append(image)
51
+ return images
52
+
53
+ print("Great sylvain ! Everything is working fine !")
54
+
55
+ title="Img2Img Stable Diffusion CPU"
56
+ description="Img2Img Stable Diffusion example using CPU and HF token. <br />Warning: Slow process... ~5/10 min inference time. <b>NSFW filter enabled.</b>"
57
+
58
+ gr.Interface(fn=infer, inputs=["text", source_img], outputs=gallery,title=title,description=description).queue(max_size=100).launch(enable_queue=True)