Spaces:
Runtime error
Runtime error
app python
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
15 |
+
YOUR_TOKEN=MY_SECRET_TOKEN
|
16 |
+
|
17 |
+
device="cpu"
|
18 |
+
|
19 |
+
#prompt_pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=YOUR_TOKEN)
|
20 |
+
#prompt_pipe.to(device)
|
21 |
+
|
22 |
+
img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", use_auth_token=YOUR_TOKEN)
|
23 |
+
img_pipe.to(device)
|
24 |
+
|
25 |
+
source_img = gr.Image(source="upload", type="filepath", label="init_img | 512*512 px")
|
26 |
+
gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[1], height="auto")
|
27 |
+
|
28 |
+
def resize(value,img):
|
29 |
+
#baseheight = value
|
30 |
+
img = Image.open(img)
|
31 |
+
#hpercent = (baseheight/float(img.size[1]))
|
32 |
+
#wsize = int((float(img.size[0])*float(hpercent)))
|
33 |
+
#img = img.resize((wsize,baseheight), Image.Resampling.LANCZOS)
|
34 |
+
img = img.resize((value,value), Image.Resampling.LANCZOS)
|
35 |
+
return img
|
36 |
+
|
37 |
+
|
38 |
+
def infer(source_img, prompt, guide, steps, seed, strength):
|
39 |
+
generator = torch.Generator('cpu').manual_seed(seed)
|
40 |
+
|
41 |
+
source_image = resize(512, source_img)
|
42 |
+
source_image.save('source.png')
|
43 |
+
|
44 |
+
images_list = img_pipe([prompt] * 1, init_image=source_image, strength=strength, guidance_scale=guide, num_inference_steps=steps)
|
45 |
+
images = []
|
46 |
+
safe_image = Image.open(r"unsafe.png")
|
47 |
+
|
48 |
+
for i, image in enumerate(images_list["images"]):
|
49 |
+
if(images_list["nsfw_content_detected"][i]):
|
50 |
+
images.append(safe_image)
|
51 |
+
else:
|
52 |
+
images.append(image)
|
53 |
+
return images
|
54 |
+
|
55 |
+
print("Everything is working fine !")
|
56 |
+
|
57 |
+
title="Img2Img Stable Diffusion CPU"
|
58 |
+
description="<p style='text-align: center;'>Img2Img Stable Diffusion example using CPU and HF token. <br />Warning: Slow process... ~5/10 min inference time. <b>NSFW filter enabled. <br /> <img id='visitor-badge' alt='visitor badge' src='https://visitor-badge.glitch.me/badge?page_id=gradio-blocks.stable-diffusion-img2img' style='display: inline-block'/></b></p>"
|
59 |
+
|
60 |
+
gr.Interface(fn=infer, inputs=[source_img,
|
61 |
+
"text",
|
62 |
+
gr.Slider(2, 15, value = 7, label = 'Guidence Scale'),
|
63 |
+
gr.Slider(10, 50, value = 25, step = 1, label = 'Number of Iterations'),
|
64 |
+
gr.Slider(label = "Seed", minimum = 0, maximum = 2147483647, step = 1, randomize = True),
|
65 |
+
gr.Slider(label='Strength', minimum = 0, maximum = 1, step = .05, value = .75)],
|
66 |
+
outputs=gallery,title=title,description=description, allow_flagging="manual", flagging_dir="flagged").queue(max_size=100).launch(enable_queue=True)
|