Spaces:
Running
on
Zero
Running
on
Zero
Upload 3 files
Browse files- app.py +159 -265
- requirements.txt +1 -11
app.py
CHANGED
@@ -1,266 +1,160 @@
|
|
1 |
-
import sys
|
2 |
-
sys.path.append('./')
|
3 |
-
|
4 |
-
|
5 |
-
import
|
6 |
-
import
|
7 |
-
import
|
8 |
-
|
9 |
-
|
10 |
-
from
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
)
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
image_cv2 = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
|
161 |
-
return image_cv2
|
162 |
-
|
163 |
-
# Description
|
164 |
-
title = r"""
|
165 |
-
<h1 align="center">InstantStyle</h1>
|
166 |
-
"""
|
167 |
-
|
168 |
-
description = r"""
|
169 |
-
How to use:<br>
|
170 |
-
1. Upload a style image.
|
171 |
-
2. Set stylization mode, only use style block by default.
|
172 |
-
2. Enter a text prompt, as done in normal text-to-image models.
|
173 |
-
3. Click the <b>Submit</b> button to begin customization.
|
174 |
-
4. Share your stylized photo with your friends and enjoy! 😊
|
175 |
-
|
176 |
-
|
177 |
-
Advanced usage:<br>
|
178 |
-
1. Click advanced options.
|
179 |
-
2. Upload another source image for image-based stylization using ControlNet.
|
180 |
-
3. Enter negative content prompt to avoid content leakage.
|
181 |
-
"""
|
182 |
-
|
183 |
-
article = r"""
|
184 |
-
---
|
185 |
-
```bibtex
|
186 |
-
@article{wang2024instantstyle,
|
187 |
-
title={InstantStyle: Free Lunch towards Style-Preserving in Text-to-Image Generation},
|
188 |
-
author={Wang, Haofan and Wang, Qixun and Bai, Xu and Qin, Zekui and Chen, Anthony},
|
189 |
-
journal={arXiv preprint arXiv:2404.02733},
|
190 |
-
year={2024}
|
191 |
-
}
|
192 |
-
```
|
193 |
-
"""
|
194 |
-
|
195 |
-
block = gr.Blocks().queue(max_size=10, api_open=True)
|
196 |
-
with block:
|
197 |
-
|
198 |
-
# description
|
199 |
-
gr.Markdown(title)
|
200 |
-
gr.Markdown(description)
|
201 |
-
|
202 |
-
with gr.Tabs():
|
203 |
-
with gr.Row():
|
204 |
-
with gr.Column():
|
205 |
-
|
206 |
-
with gr.Row():
|
207 |
-
with gr.Column():
|
208 |
-
image_pil = gr.Image(label="Style Image", type="numpy")
|
209 |
-
|
210 |
-
target = gr.Radio(["Load only style blocks", "Load style+layout block", "Load original IP-Adapter"],
|
211 |
-
value="Load only style blocks",
|
212 |
-
label="Style mode")
|
213 |
-
|
214 |
-
prompt = gr.Textbox(label="Prompt",
|
215 |
-
value="a cat, masterpiece, best quality, high quality")
|
216 |
-
|
217 |
-
scale = gr.Slider(minimum=0,maximum=2.0, step=0.01,value=1.0, label="Scale")
|
218 |
-
|
219 |
-
with gr.Accordion(open=False, label="Advanced Options"):
|
220 |
-
|
221 |
-
with gr.Column():
|
222 |
-
src_image_pil = gr.Image(label="Source Image (optional)", type='pil')
|
223 |
-
control_scale = gr.Slider(minimum=0,maximum=1.0, step=0.01,value=0.5, label="Controlnet conditioning scale")
|
224 |
-
|
225 |
-
n_prompt = gr.Textbox(label="Neg Prompt", value="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry")
|
226 |
-
|
227 |
-
neg_content_prompt = gr.Textbox(label="Neg Content Prompt", value="")
|
228 |
-
neg_content_scale = gr.Slider(minimum=0, maximum=1.0, step=0.01,value=0.5, label="Neg Content Scale")
|
229 |
-
|
230 |
-
guidance_scale = gr.Slider(minimum=1,maximum=15.0, step=0.01,value=5.0, label="guidance scale")
|
231 |
-
num_samples= gr.Slider(minimum=1,maximum=4.0, step=1.0,value=1.0, label="num samples")
|
232 |
-
num_inference_steps = gr.Slider(minimum=5,maximum=50.0, step=1.0,value=20, label="num inference steps")
|
233 |
-
seed = gr.Slider(minimum=-1000000,maximum=1000000,value=1, step=1, label="Seed Value")
|
234 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
235 |
-
|
236 |
-
generate_button = gr.Button("Generate Image")
|
237 |
-
|
238 |
-
with gr.Column():
|
239 |
-
generated_image = gr.Gallery(label="Generated Image")
|
240 |
-
|
241 |
-
generate_button.click(
|
242 |
-
fn=randomize_seed_fn,
|
243 |
-
inputs=[seed, randomize_seed],
|
244 |
-
outputs=seed,
|
245 |
-
queue=False,
|
246 |
-
api_name=False,
|
247 |
-
).then(
|
248 |
-
fn=create_image,
|
249 |
-
inputs=[image_pil,
|
250 |
-
src_image_pil,
|
251 |
-
prompt,
|
252 |
-
n_prompt,
|
253 |
-
scale,
|
254 |
-
control_scale,
|
255 |
-
guidance_scale,
|
256 |
-
num_samples,
|
257 |
-
num_inference_steps,
|
258 |
-
seed,
|
259 |
-
target,
|
260 |
-
neg_content_prompt,
|
261 |
-
neg_content_scale],
|
262 |
-
outputs=[generated_image])
|
263 |
-
|
264 |
-
gr.Markdown(article)
|
265 |
-
|
266 |
block.launch(show_error=True)
|
|
|
1 |
+
import sys
|
2 |
+
sys.path.append('./')
|
3 |
+
|
4 |
+
import torch
|
5 |
+
import random
|
6 |
+
import spaces
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
from diffusers import AutoPipelineForText2Image
|
10 |
+
from diffusers.utils import load_image
|
11 |
+
|
12 |
+
# global variable
|
13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
+
dtype = torch.float16 if str(device).__contains__("cuda") else torch.float32
|
15 |
+
|
16 |
+
def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
17 |
+
if randomize_seed:
|
18 |
+
seed = random.randint(0, 2000)
|
19 |
+
return seed
|
20 |
+
|
21 |
+
pipeline = AutoPipelineForText2Image.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=dtype).to(device)
|
22 |
+
pipeline.load_ip_adapter("h94/IP-Adapter", subfolder="sdxl_models", weight_name="ip-adapter_sdxl.bin")
|
23 |
+
|
24 |
+
@spaces.GPU(enable_queue=True)
|
25 |
+
def create_image(image_pil,
|
26 |
+
prompt,
|
27 |
+
n_prompt,
|
28 |
+
scale,
|
29 |
+
control_scale,
|
30 |
+
guidance_scale,
|
31 |
+
num_inference_steps,
|
32 |
+
seed,
|
33 |
+
target="Load only style blocks",
|
34 |
+
):
|
35 |
+
|
36 |
+
|
37 |
+
if target !="Load original IP-Adapter":
|
38 |
+
if target=="Load only style blocks":
|
39 |
+
scale = {
|
40 |
+
"up": {"block_0": [0.0, control_scale, 0.0]},
|
41 |
+
}
|
42 |
+
elif target=="Load only layout blocks":
|
43 |
+
scale = {
|
44 |
+
"down": {"block_2": [0.0, control_scale]},
|
45 |
+
}
|
46 |
+
elif target == "Load style+layout block":
|
47 |
+
scale = {
|
48 |
+
"down": {"block_2": [0.0, control_scale]},
|
49 |
+
"up": {"block_0": [0.0, control_scale, 0.0]},
|
50 |
+
}
|
51 |
+
pipeline.set_ip_adapter_scale(scale)
|
52 |
+
|
53 |
+
|
54 |
+
style_image = load_image(image_pil)
|
55 |
+
|
56 |
+
generator = torch.Generator(device="cpu").manual_seed(randomize_seed_fn(seed, False))
|
57 |
+
image = pipeline(
|
58 |
+
prompt=prompt,
|
59 |
+
ip_adapter_image=style_image,
|
60 |
+
negative_prompt=n_prompt,
|
61 |
+
guidance_scale=guidance_scale,
|
62 |
+
num_inference_steps=num_inference_steps,
|
63 |
+
generator=generator,
|
64 |
+
)
|
65 |
+
return image
|
66 |
+
|
67 |
+
|
68 |
+
# Description
|
69 |
+
title = r"""
|
70 |
+
<h1 align="center">InstantStyle</h1>
|
71 |
+
"""
|
72 |
+
|
73 |
+
description = r"""
|
74 |
+
How to use:<br>
|
75 |
+
1. Upload a style image.
|
76 |
+
2. Set stylization mode, only use style block by default.
|
77 |
+
2. Enter a text prompt, as done in normal text-to-image models.
|
78 |
+
3. Click the <b>Submit</b> button to begin customization.
|
79 |
+
4. Share your stylized photo with your friends and enjoy! 😊
|
80 |
+
|
81 |
+
|
82 |
+
Advanced usage:<br>
|
83 |
+
1. Click advanced options.
|
84 |
+
2. Upload another source image for image-based stylization using ControlNet.
|
85 |
+
3. Enter negative content prompt to avoid content leakage.
|
86 |
+
"""
|
87 |
+
|
88 |
+
article = r"""
|
89 |
+
---
|
90 |
+
```bibtex
|
91 |
+
@article{wang2024instantstyle,
|
92 |
+
title={InstantStyle: Free Lunch towards Style-Preserving in Text-to-Image Generation},
|
93 |
+
author={Wang, Haofan and Wang, Qixun and Bai, Xu and Qin, Zekui and Chen, Anthony},
|
94 |
+
journal={arXiv preprint arXiv:2404.02733},
|
95 |
+
year={2024}
|
96 |
+
}
|
97 |
+
```
|
98 |
+
"""
|
99 |
+
|
100 |
+
block = gr.Blocks().queue(max_size=10, api_open=True)
|
101 |
+
with block:
|
102 |
+
|
103 |
+
# description
|
104 |
+
gr.Markdown(title)
|
105 |
+
gr.Markdown(description)
|
106 |
+
|
107 |
+
with gr.Tabs():
|
108 |
+
with gr.Row():
|
109 |
+
with gr.Column():
|
110 |
+
|
111 |
+
with gr.Row():
|
112 |
+
with gr.Column():
|
113 |
+
image_pil = gr.Image(label="Style Image", type="numpy")
|
114 |
+
|
115 |
+
target = gr.Radio(["Load only style blocks", "Load only layout blocks","Load style+layout block", "Load original IP-Adapter"],
|
116 |
+
value="Load only style blocks",
|
117 |
+
label="Style mode")
|
118 |
+
|
119 |
+
prompt = gr.Textbox(label="Prompt",
|
120 |
+
value="a cat, masterpiece, best quality, high quality")
|
121 |
+
|
122 |
+
scale = gr.Slider(minimum=0,maximum=2.0, step=0.01,value=1.0, label="Scale")
|
123 |
+
|
124 |
+
with gr.Accordion(open=False, label="Advanced Options"):
|
125 |
+
|
126 |
+
control_scale = gr.Slider(minimum=0,maximum=1.0, step=0.01,value=0.5, label="Controlnet conditioning scale")
|
127 |
+
|
128 |
+
n_prompt = gr.Textbox(label="Neg Prompt", value="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry")
|
129 |
+
guidance_scale = gr.Slider(minimum=1,maximum=15.0, step=0.01,value=5.0, label="guidance scale")
|
130 |
+
num_inference_steps = gr.Slider(minimum=5,maximum=50.0, step=1.0,value=20, label="num inference steps")
|
131 |
+
seed = gr.Slider(minimum=-1000000,maximum=1000000,value=1, step=1, label="Seed Value")
|
132 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
133 |
+
|
134 |
+
generate_button = gr.Button("Generate Image")
|
135 |
+
|
136 |
+
with gr.Column():
|
137 |
+
generated_image = gr.Gallery(label="Generated Image")
|
138 |
+
|
139 |
+
generate_button.click(
|
140 |
+
fn=randomize_seed_fn,
|
141 |
+
inputs=[seed, randomize_seed],
|
142 |
+
outputs=seed,
|
143 |
+
queue=False,
|
144 |
+
api_name=False,
|
145 |
+
).then(
|
146 |
+
fn=create_image,
|
147 |
+
inputs=[image_pil,
|
148 |
+
prompt,
|
149 |
+
n_prompt,
|
150 |
+
scale,
|
151 |
+
control_scale,
|
152 |
+
guidance_scale,
|
153 |
+
num_inference_steps,
|
154 |
+
seed,
|
155 |
+
target],
|
156 |
+
outputs=[generated_image])
|
157 |
+
|
158 |
+
gr.Markdown(article)
|
159 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
block.launch(show_error=True)
|
requirements.txt
CHANGED
@@ -1,16 +1,6 @@
|
|
1 |
diffusers>=0.25.1
|
2 |
torch>=2.0.0
|
3 |
-
torchvision>=0.15.1
|
4 |
transformers>=4.37.1
|
5 |
-
accelerate
|
6 |
-
safetensors
|
7 |
-
einops
|
8 |
spaces>=0.19.4
|
9 |
-
omegaconf
|
10 |
-
peft
|
11 |
huggingface-hub>=0.20.2
|
12 |
-
|
13 |
-
gradio==4.38.0
|
14 |
-
controlnet_aux
|
15 |
-
gdown
|
16 |
-
peft
|
|
|
1 |
diffusers>=0.25.1
|
2 |
torch>=2.0.0
|
|
|
3 |
transformers>=4.37.1
|
|
|
|
|
|
|
4 |
spaces>=0.19.4
|
|
|
|
|
5 |
huggingface-hub>=0.20.2
|
6 |
+
gradio
|
|
|
|
|
|
|
|