Spaces:
Runtime error
Runtime error
first
Browse files- .gitattributes +3 -0
- .gitignore +2 -0
- app.py +161 -0
- examples/init.jpeg +3 -0
- examples/qrcode.png +3 -0
- requirements.txt +7 -0
.gitattributes
CHANGED
@@ -32,3 +32,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
37 |
+
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
__pycache__
|
2 |
+
venv
|
app.py
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
from diffusers import (
|
5 |
+
StableDiffusionControlNetImg2ImgPipeline,
|
6 |
+
ControlNetModel,
|
7 |
+
DDIMScheduler,
|
8 |
+
)
|
9 |
+
from diffusers.utils import load_image
|
10 |
+
from PIL import Image
|
11 |
+
|
12 |
+
controlnet = ControlNetModel.from_pretrained(
|
13 |
+
"DionTimmer/controlnet_qrcode-control_v1p_sd15", torch_dtype=torch.float16
|
14 |
+
)
|
15 |
+
|
16 |
+
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
17 |
+
"runwayml/stable-diffusion-v1-5",
|
18 |
+
controlnet=controlnet,
|
19 |
+
safety_checker=None,
|
20 |
+
torch_dtype=torch.float16,
|
21 |
+
)
|
22 |
+
|
23 |
+
pipe.enable_xformers_memory_efficient_attention()
|
24 |
+
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)
|
25 |
+
pipe.enable_model_cpu_offload()
|
26 |
+
|
27 |
+
|
28 |
+
def resize_for_condition_image(input_image: Image.Image, resolution: int):
|
29 |
+
input_image = input_image.convert("RGB")
|
30 |
+
W, H = input_image.size
|
31 |
+
k = float(resolution) / min(H, W)
|
32 |
+
H *= k
|
33 |
+
W *= k
|
34 |
+
H = int(round(H / 64.0)) * 64
|
35 |
+
W = int(round(W / 64.0)) * 64
|
36 |
+
img = input_image.resize((W, H), resample=Image.LANCZOS)
|
37 |
+
return img
|
38 |
+
|
39 |
+
|
40 |
+
def inference(
|
41 |
+
init_image: Image.Image,
|
42 |
+
qrcode_image: Image.Image,
|
43 |
+
prompt: str,
|
44 |
+
negative_prompt: str,
|
45 |
+
guidance_scale: float = 10.0,
|
46 |
+
controlnet_conditioning_scale: float = 2.0,
|
47 |
+
strength: float = 0.8,
|
48 |
+
seed: int = -1,
|
49 |
+
num_inference_steps: int = 50,
|
50 |
+
):
|
51 |
+
init_image = resize_for_condition_image(init_image, 768)
|
52 |
+
qrcode_image = resize_for_condition_image(qrcode_image, 768)
|
53 |
+
|
54 |
+
generator = torch.manual_seed(seed) if seed != -1 else torch.Generator()
|
55 |
+
|
56 |
+
out = pipe(
|
57 |
+
prompt=prompt,
|
58 |
+
negative_prompt=negative_prompt,
|
59 |
+
image=init_image, # type: ignore
|
60 |
+
control_image=qrcode_image, # type: ignore
|
61 |
+
width=768, # type: ignore
|
62 |
+
height=768, # type: ignore
|
63 |
+
guidance_scale=guidance_scale,
|
64 |
+
controlnet_conditioning_scale=controlnet_conditioning_scale, # type: ignore
|
65 |
+
generator=generator,
|
66 |
+
strength=strength,
|
67 |
+
num_inference_steps=num_inference_steps,
|
68 |
+
) # type: ignore
|
69 |
+
return out.images[0]
|
70 |
+
|
71 |
+
|
72 |
+
with gr.Blocks() as blocks:
|
73 |
+
gr.Markdown(
|
74 |
+
"""# AI QR Code Generator
|
75 |
+
|
76 |
+
model by: https://huggingface.co/DionTimmer/controlnet_qrcode-control_v1p_sd15
|
77 |
+
"""
|
78 |
+
)
|
79 |
+
|
80 |
+
with gr.Row():
|
81 |
+
with gr.Column():
|
82 |
+
init_image = gr.Image(label="Init Image", type="pil")
|
83 |
+
qr_code_image = gr.Image(label="QR Code Image", type="pil")
|
84 |
+
prompt = gr.Textbox(label="Prompt")
|
85 |
+
negative_prompt = gr.Textbox(
|
86 |
+
label="Negative Prompt",
|
87 |
+
value="ugly, disfigured, low quality, blurry, nsfw",
|
88 |
+
)
|
89 |
+
with gr.Accordion(label="Params"):
|
90 |
+
guidance_scale = gr.Slider(
|
91 |
+
minimum=0.0,
|
92 |
+
maximum=50.0,
|
93 |
+
step=0.1,
|
94 |
+
value=10.0,
|
95 |
+
label="Guidance Scale",
|
96 |
+
)
|
97 |
+
controlnet_conditioning_scale = gr.Slider(
|
98 |
+
minimum=0.0,
|
99 |
+
maximum=5.0,
|
100 |
+
step=0.1,
|
101 |
+
value=2.0,
|
102 |
+
label="Controlnet Conditioning Scale",
|
103 |
+
)
|
104 |
+
strength = gr.Slider(
|
105 |
+
minimum=0.0, maximum=1.0, step=0.1, value=0.8, label="Strength"
|
106 |
+
)
|
107 |
+
seed = gr.Slider(
|
108 |
+
minimum=-1,
|
109 |
+
maximum=9999999999,
|
110 |
+
step=1,
|
111 |
+
value=2313123,
|
112 |
+
label="Seed",
|
113 |
+
randomize=True,
|
114 |
+
)
|
115 |
+
run_btn = gr.Button("Run")
|
116 |
+
with gr.Column():
|
117 |
+
result_image = gr.Image(label="Result Image")
|
118 |
+
run_btn.click(
|
119 |
+
inference,
|
120 |
+
inputs=[
|
121 |
+
init_image,
|
122 |
+
qr_code_image,
|
123 |
+
prompt,
|
124 |
+
negative_prompt,
|
125 |
+
guidance_scale,
|
126 |
+
controlnet_conditioning_scale,
|
127 |
+
strength,
|
128 |
+
seed,
|
129 |
+
],
|
130 |
+
outputs=[result_image],
|
131 |
+
)
|
132 |
+
|
133 |
+
gr.Examples(
|
134 |
+
examples=[
|
135 |
+
[
|
136 |
+
"./examples/init.jpeg",
|
137 |
+
"./examples/qrcode.png",
|
138 |
+
"crisp QR code prominently displayed on a billboard amidst the bustling skyline of New York City, with iconic landmarks subtly featured in the background.",
|
139 |
+
"ugly, disfigured, low quality, blurry, nsfw",
|
140 |
+
10.0,
|
141 |
+
2.0,
|
142 |
+
0.8,
|
143 |
+
2313123,
|
144 |
+
]
|
145 |
+
],
|
146 |
+
fn=inference,
|
147 |
+
inputs=[
|
148 |
+
init_image,
|
149 |
+
qr_code_image,
|
150 |
+
prompt,
|
151 |
+
negative_prompt,
|
152 |
+
guidance_scale,
|
153 |
+
controlnet_conditioning_scale,
|
154 |
+
strength,
|
155 |
+
seed,
|
156 |
+
],
|
157 |
+
outputs=[result_image],
|
158 |
+
)
|
159 |
+
|
160 |
+
blocks.queue()
|
161 |
+
blocks.launch()
|
examples/init.jpeg
ADDED
Git LFS Details
|
examples/qrcode.png
ADDED
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
diffusers
|
2 |
+
transformers
|
3 |
+
accelerate
|
4 |
+
torch
|
5 |
+
xformers
|
6 |
+
gradio
|
7 |
+
Pillow
|