Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,25 @@
|
|
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
|
10 |
-
#os.environ.get('HF_TOKEN_SD')
|
11 |
|
12 |
-
#from diffusers import StableDiffusionPipeline
|
13 |
from diffusers import StableDiffusionImg2ImgPipeline
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
#pipeline = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
YOUR_TOKEN = MY_SECRET_TOKEN
|
22 |
|
23 |
device="cpu"
|
24 |
-
model_id_or_path="CompVis/stable-diffusion-v1-4"
|
25 |
|
26 |
#prompt_pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=YOUR_TOKEN)
|
27 |
#prompt_pipe.to(device)
|
28 |
|
29 |
-
img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
30 |
-
model_id_or_path,
|
31 |
-
revision="fp16",
|
32 |
-
torch_dtype=torch.float16,
|
33 |
-
use_auth_token=YOUR_TOKEN
|
34 |
-
)
|
35 |
img_pipe.to(device)
|
36 |
|
37 |
source_img = gr.Image(source="upload", type="filepath", label="init_img | 512*512 px")
|
@@ -47,16 +35,13 @@ def resize(value,img):
|
|
47 |
return img
|
48 |
|
49 |
|
50 |
-
def infer(
|
51 |
-
|
52 |
-
|
53 |
source_image = resize(512, source_img)
|
54 |
source_image.save('source.png')
|
55 |
-
|
56 |
-
images_list = img_pipe([prompt] * 2, init_image=source_image, strength=strength, guidance_scale=guide, num_inference_steps=steps)
|
57 |
images = []
|
58 |
safe_image = Image.open(r"unsafe.png")
|
59 |
-
|
60 |
for i, image in enumerate(images_list["sample"]):
|
61 |
if(images_list["nsfw_content_detected"][i]):
|
62 |
images.append(safe_image)
|
@@ -64,19 +49,12 @@ def infer(source_img, prompt, guide, steps, seed, strength):
|
|
64 |
images.append(image)
|
65 |
return images
|
66 |
|
67 |
-
print("Great
|
68 |
-
|
69 |
-
title="Stable Diffusion(SD) Img2Img Experiment"
|
70 |
-
description="<p style='text-align: center;'>Stable Diffusion Img2Img example using CUDA/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.DJ-stable-diffusion-img2img&left_color=#66ccff&right_color=#33bbdd' style='display: inline-block'/></b></p>"
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
gr.Slider(2, 15, value = 7, label = 'Guidence Scale'),
|
75 |
-
gr.Slider(10, 50, value = 25, step = 1, label = 'Number of Iterations'),
|
76 |
-
gr.Slider(label = "Seed", minimum = 0, maximum = 2147483647, step = 1, randomize = True),
|
77 |
-
gr.Slider(label='Strength', minimum = 0, maximum = 1, step = .05, value = .75)],
|
78 |
-
outputs=gallery,title=title,description=description, allow_flagging="manual", flagging_dir="flagged").queue(max_size=100).launch(enable_queue=True)
|
79 |
|
|
|
80 |
#from torch import autocast
|
81 |
#import requests
|
82 |
#import torch
|
|
|
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 StableDiffusionImg2ImgPipeline
|
12 |
|
13 |
+
print("hello sylvain")
|
|
|
|
|
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("CompVis/stable-diffusion-v1-4", 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")
|
|
|
35 |
return img
|
36 |
|
37 |
|
38 |
+
def infer(prompt, source_img):
|
39 |
+
|
|
|
40 |
source_image = resize(512, source_img)
|
41 |
source_image.save('source.png')
|
42 |
+
images_list = img_pipe([prompt] * 2, init_image=source_image, strength=0.75)
|
|
|
43 |
images = []
|
44 |
safe_image = Image.open(r"unsafe.png")
|
|
|
45 |
for i, image in enumerate(images_list["sample"]):
|
46 |
if(images_list["nsfw_content_detected"][i]):
|
47 |
images.append(safe_image)
|
|
|
49 |
images.append(image)
|
50 |
return images
|
51 |
|
52 |
+
print("Great sylvain ! Everything is working fine !")
|
|
|
|
|
|
|
53 |
|
54 |
+
title="Img2Img Stable Diffusion CPU"
|
55 |
+
description="Img2Img Stable Diffusion example using CPU and HF token. <br />Warning: Slow process... ~5/10 min inference time. <b>NSFW filter enabled.</b>"
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
gr.Interface(fn=infer, inputs=["text", source_img], outputs=gallery,title=title,description=description).queue(max_size=100).launch(enable_queue=True)
|
58 |
#from torch import autocast
|
59 |
#import requests
|
60 |
#import torch
|