Upload 3 files
Browse files- README.md +8 -6
- app.py +195 -0
- requirements.txt +9 -0
README.md
CHANGED
@@ -1,13 +1,15 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.42.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
|
|
|
|
|
11 |
---
|
12 |
|
13 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: test
|
3 |
+
emoji: π¨βπ¨
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: green
|
6 |
sdk: gradio
|
7 |
sdk_version: 4.42.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
+
suggested_hardware: t4-medium
|
11 |
+
startup_duration_timeout: 1h
|
12 |
+
disable_embedding: false
|
13 |
---
|
14 |
|
15 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,195 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
import qrcode
|
5 |
+
from pathlib import Path
|
6 |
+
import requests
|
7 |
+
import io
|
8 |
+
import os
|
9 |
+
import spaces
|
10 |
+
import random
|
11 |
+
|
12 |
+
from diffusers import (
|
13 |
+
StableDiffusionXLControlNetPipeline,
|
14 |
+
ControlNetModel,
|
15 |
+
AutoencoderKL,
|
16 |
+
DiffusionPipeline,
|
17 |
+
DDIMScheduler,
|
18 |
+
DPMSolverMultistepScheduler,
|
19 |
+
DEISMultistepScheduler,
|
20 |
+
HeunDiscreteScheduler,
|
21 |
+
EulerDiscreteScheduler,
|
22 |
+
)
|
23 |
+
|
24 |
+
MAX_SEED = 2**32 - 1
|
25 |
+
|
26 |
+
# QR Code generation setup
|
27 |
+
qrcode_generator = qrcode.QRCode(
|
28 |
+
version=1,
|
29 |
+
error_correction=qrcode.ERROR_CORRECT_H,
|
30 |
+
box_size=16,
|
31 |
+
border=4,
|
32 |
+
)
|
33 |
+
|
34 |
+
# SDXL and ControlNet setup
|
35 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
36 |
+
|
37 |
+
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
|
38 |
+
|
39 |
+
controlnet = ControlNetModel.from_pretrained(
|
40 |
+
"AGCobra/1",
|
41 |
+
torch_dtype=torch.float16
|
42 |
+
).to(device)
|
43 |
+
|
44 |
+
pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
45 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
46 |
+
vae=vae,
|
47 |
+
controlnet=controlnet,
|
48 |
+
torch_dtype=torch.float16,
|
49 |
+
use_safetensors=True,
|
50 |
+
variant="fp16",
|
51 |
+
).to(device)
|
52 |
+
|
53 |
+
# Sampler setup
|
54 |
+
SAMPLER_MAP = {
|
55 |
+
"DPM++ Karras SDE": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True, algorithm_type="sde-dpmsolver++"),
|
56 |
+
"DPM++ Karras": lambda config: DPMSolverMultistepScheduler.from_config(config, use_karras=True),
|
57 |
+
"Heun": lambda config: HeunDiscreteScheduler.from_config(config),
|
58 |
+
"Euler": lambda config: EulerDiscreteScheduler.from_config(config),
|
59 |
+
"DDIM": lambda config: DDIMScheduler.from_config(config),
|
60 |
+
"DEIS": lambda config: DEISMultistepScheduler.from_config(config),
|
61 |
+
}
|
62 |
+
|
63 |
+
def resize_for_condition_image(input_image: Image.Image, resolution: int):
|
64 |
+
input_image = input_image.convert("RGB")
|
65 |
+
W, H = input_image.size
|
66 |
+
k = float(resolution) / min(H, W)
|
67 |
+
H *= k
|
68 |
+
W *= k
|
69 |
+
H = int(round(H / 64.0)) * 64
|
70 |
+
W = int(round(W / 64.0)) * 64
|
71 |
+
img = input_image.resize((W, H), resample=Image.LANCZOS)
|
72 |
+
return img
|
73 |
+
|
74 |
+
@spaces.GPU()
|
75 |
+
def inference(
|
76 |
+
qr_code_content: str,
|
77 |
+
prompt: str,
|
78 |
+
negative_prompt: str,
|
79 |
+
guidance_scale: float = 7.5,
|
80 |
+
controlnet_conditioning_scale: float = 1.1,
|
81 |
+
strength: float = 0.9,
|
82 |
+
seed: int = -1,
|
83 |
+
sampler: str = "DPM++ Karras SDE",
|
84 |
+
):
|
85 |
+
if prompt is None or prompt == "":
|
86 |
+
raise gr.Error("Prompt is required")
|
87 |
+
|
88 |
+
if qr_code_content == "":
|
89 |
+
raise gr.Error("QR Code Content is required")
|
90 |
+
|
91 |
+
pipe.scheduler = SAMPLER_MAP[sampler](pipe.scheduler.config)
|
92 |
+
|
93 |
+
if seed == -1:
|
94 |
+
seed = random.randint(0, MAX_SEED)
|
95 |
+
|
96 |
+
# Use a sub-seed for additional randomness
|
97 |
+
subseed = random.randint(0, MAX_SEED)
|
98 |
+
generator = torch.Generator(device=device).manual_seed(seed + subseed)
|
99 |
+
|
100 |
+
print("Generating QR Code from content")
|
101 |
+
qr = qrcode.QRCode(
|
102 |
+
version=1,
|
103 |
+
error_correction=qrcode.constants.ERROR_CORRECT_H,
|
104 |
+
box_size=16,
|
105 |
+
border=4,
|
106 |
+
)
|
107 |
+
qr.add_data(qr_code_content)
|
108 |
+
qr.make(fit=True)
|
109 |
+
|
110 |
+
qrcode_image = qr.make_image(fill_color="black", back_color="white")
|
111 |
+
qrcode_image = resize_for_condition_image(qrcode_image, 1024)
|
112 |
+
|
113 |
+
init_image = qrcode_image
|
114 |
+
|
115 |
+
out = pipe(
|
116 |
+
prompt=prompt,
|
117 |
+
negative_prompt=negative_prompt,
|
118 |
+
image=init_image,
|
119 |
+
control_image=qrcode_image,
|
120 |
+
controlnet_conditioning_scale=float(controlnet_conditioning_scale),
|
121 |
+
guidance_scale=float(guidance_scale),
|
122 |
+
generator=generator,
|
123 |
+
strength=float(strength),
|
124 |
+
num_inference_steps=30,
|
125 |
+
)
|
126 |
+
return out.images[0]
|
127 |
+
|
128 |
+
with gr.Blocks() as demo:
|
129 |
+
with gr.Row():
|
130 |
+
with gr.Column():
|
131 |
+
qr_code_content = gr.Textbox(
|
132 |
+
label="QR Code Content",
|
133 |
+
info="QR Code Content or URL",
|
134 |
+
value="",
|
135 |
+
)
|
136 |
+
|
137 |
+
prompt = gr.Textbox(
|
138 |
+
label="Prompt",
|
139 |
+
info="Prompt that guides the generation towards",
|
140 |
+
)
|
141 |
+
negative_prompt = gr.Textbox(
|
142 |
+
label="Negative Prompt",
|
143 |
+
value="ugly, disfigured, low quality, blurry",
|
144 |
+
)
|
145 |
+
|
146 |
+
with gr.Accordion(
|
147 |
+
label="Advanced Parameters",
|
148 |
+
open=True,
|
149 |
+
):
|
150 |
+
controlnet_conditioning_scale = gr.Slider(
|
151 |
+
minimum=0.0,
|
152 |
+
maximum=2.0,
|
153 |
+
step=0.01,
|
154 |
+
value=1.1,
|
155 |
+
label="Controlnet Conditioning Scale",
|
156 |
+
)
|
157 |
+
strength = gr.Slider(
|
158 |
+
minimum=0.0, maximum=1.0, step=0.01, value=0.9, label="Strength"
|
159 |
+
)
|
160 |
+
guidance_scale = gr.Slider(
|
161 |
+
minimum=0.0,
|
162 |
+
maximum=50.0,
|
163 |
+
step=0.25,
|
164 |
+
value=7.5,
|
165 |
+
label="Guidance Scale",
|
166 |
+
)
|
167 |
+
sampler = gr.Dropdown(choices=list(SAMPLER_MAP.keys()), value="DPM++ Karras SDE", label="Sampler")
|
168 |
+
seed = gr.Slider(
|
169 |
+
minimum=-1,
|
170 |
+
maximum=MAX_SEED,
|
171 |
+
step=1,
|
172 |
+
value=-1,
|
173 |
+
label="Seed",
|
174 |
+
randomize=True,
|
175 |
+
)
|
176 |
+
with gr.Row():
|
177 |
+
run_btn = gr.Button("Run")
|
178 |
+
with gr.Column():
|
179 |
+
result_image = gr.Image(label="Result Image")
|
180 |
+
run_btn.click(
|
181 |
+
inference,
|
182 |
+
inputs=[
|
183 |
+
qr_code_content,
|
184 |
+
prompt,
|
185 |
+
negative_prompt,
|
186 |
+
guidance_scale,
|
187 |
+
controlnet_conditioning_scale,
|
188 |
+
strength,
|
189 |
+
seed,
|
190 |
+
sampler,
|
191 |
+
],
|
192 |
+
outputs=[result_image],
|
193 |
+
)
|
194 |
+
|
195 |
+
demo.queue(max_size=20).launch(share=bool(os.environ.get("SHARE", False)))
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
diffusers
|
2 |
+
transformers
|
3 |
+
accelerate
|
4 |
+
torch
|
5 |
+
xformers
|
6 |
+
gradio
|
7 |
+
Pillow
|
8 |
+
qrcode
|
9 |
+
gradio==4.8.0
|