Spaces:
Running
on
Zero
Running
on
Zero
Changed Generator Device
Browse files
app.py
CHANGED
@@ -1,161 +1,161 @@
|
|
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 |
-
print(image_pil)
|
54 |
-
|
55 |
-
style_image = load_image(image_pil)
|
56 |
-
|
57 |
-
generator = torch.Generator(device=
|
58 |
-
image = pipeline(
|
59 |
-
prompt=prompt,
|
60 |
-
ip_adapter_image=style_image,
|
61 |
-
negative_prompt=n_prompt,
|
62 |
-
guidance_scale=guidance_scale,
|
63 |
-
num_inference_steps=num_inference_steps,
|
64 |
-
generator=generator,
|
65 |
-
)
|
66 |
-
return image
|
67 |
-
|
68 |
-
|
69 |
-
# Description
|
70 |
-
title = r"""
|
71 |
-
<h1 align="center">InstantStyle</h1>
|
72 |
-
"""
|
73 |
-
|
74 |
-
description = r"""
|
75 |
-
How to use:<br>
|
76 |
-
1. Upload a style image.
|
77 |
-
2. Set stylization mode, only use style block by default.
|
78 |
-
2. Enter a text prompt, as done in normal text-to-image models.
|
79 |
-
3. Click the <b>Submit</b> button to begin customization.
|
80 |
-
4. Share your stylized photo with your friends and enjoy! π
|
81 |
-
|
82 |
-
|
83 |
-
Advanced usage:<br>
|
84 |
-
1. Click advanced options.
|
85 |
-
2. Upload another source image for image-based stylization using ControlNet.
|
86 |
-
3. Enter negative content prompt to avoid content leakage.
|
87 |
-
"""
|
88 |
-
|
89 |
-
article = r"""
|
90 |
-
---
|
91 |
-
```bibtex
|
92 |
-
@article{wang2024instantstyle,
|
93 |
-
title={InstantStyle: Free Lunch towards Style-Preserving in Text-to-Image Generation},
|
94 |
-
author={Wang, Haofan and Wang, Qixun and Bai, Xu and Qin, Zekui and Chen, Anthony},
|
95 |
-
journal={arXiv preprint arXiv:2404.02733},
|
96 |
-
year={2024}
|
97 |
-
}
|
98 |
-
```
|
99 |
-
"""
|
100 |
-
|
101 |
-
block = gr.Blocks().queue(max_size=10, api_open=True)
|
102 |
-
with block:
|
103 |
-
|
104 |
-
# description
|
105 |
-
gr.Markdown(title)
|
106 |
-
gr.Markdown(description)
|
107 |
-
|
108 |
-
with gr.Tabs():
|
109 |
-
with gr.Row():
|
110 |
-
with gr.Column():
|
111 |
-
|
112 |
-
with gr.Row():
|
113 |
-
with gr.Column():
|
114 |
-
image_pil = gr.Image(label="Style Image", type="pil")
|
115 |
-
|
116 |
-
target = gr.Radio(["Load only style blocks", "Load only layout blocks","Load style+layout block", "Load original IP-Adapter"],
|
117 |
-
value="Load only style blocks",
|
118 |
-
label="Style mode")
|
119 |
-
|
120 |
-
prompt = gr.Textbox(label="Prompt",
|
121 |
-
value="a cat, masterpiece, best quality, high quality")
|
122 |
-
|
123 |
-
scale = gr.Slider(minimum=0,maximum=2.0, step=0.01,value=1.0, label="Scale")
|
124 |
-
|
125 |
-
with gr.Accordion(open=False, label="Advanced Options"):
|
126 |
-
|
127 |
-
control_scale = gr.Slider(minimum=0,maximum=1.0, step=0.01,value=0.5, label="Controlnet conditioning scale")
|
128 |
-
|
129 |
-
n_prompt = gr.Textbox(label="Neg Prompt", value="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry")
|
130 |
-
guidance_scale = gr.Slider(minimum=1,maximum=15.0, step=0.01,value=5.0, label="guidance scale")
|
131 |
-
num_inference_steps = gr.Slider(minimum=5,maximum=50.0, step=1.0,value=20, label="num inference steps")
|
132 |
-
seed = gr.Slider(minimum=-1000000,maximum=1000000,value=1, step=1, label="Seed Value")
|
133 |
-
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
134 |
-
|
135 |
-
generate_button = gr.Button("Generate Image")
|
136 |
-
|
137 |
-
with gr.Column():
|
138 |
-
generated_image = gr.Gallery(label="Generated Image")
|
139 |
-
|
140 |
-
generate_button.click(
|
141 |
-
fn=randomize_seed_fn,
|
142 |
-
inputs=[seed, randomize_seed],
|
143 |
-
outputs=seed,
|
144 |
-
queue=False,
|
145 |
-
api_name=False,
|
146 |
-
).then(
|
147 |
-
fn=create_image,
|
148 |
-
inputs=[image_pil,
|
149 |
-
prompt,
|
150 |
-
n_prompt,
|
151 |
-
scale,
|
152 |
-
control_scale,
|
153 |
-
guidance_scale,
|
154 |
-
num_inference_steps,
|
155 |
-
seed,
|
156 |
-
target],
|
157 |
-
outputs=[generated_image])
|
158 |
-
|
159 |
-
gr.Markdown(article)
|
160 |
-
|
161 |
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 |
+
print(image_pil)
|
54 |
+
|
55 |
+
style_image = load_image(image_pil)
|
56 |
+
|
57 |
+
generator = torch.Generator(device=device).manual_seed(randomize_seed_fn(seed, False))
|
58 |
+
image = pipeline(
|
59 |
+
prompt=prompt,
|
60 |
+
ip_adapter_image=style_image,
|
61 |
+
negative_prompt=n_prompt,
|
62 |
+
guidance_scale=guidance_scale,
|
63 |
+
num_inference_steps=num_inference_steps,
|
64 |
+
generator=generator,
|
65 |
+
)
|
66 |
+
return image
|
67 |
+
|
68 |
+
|
69 |
+
# Description
|
70 |
+
title = r"""
|
71 |
+
<h1 align="center">InstantStyle</h1>
|
72 |
+
"""
|
73 |
+
|
74 |
+
description = r"""
|
75 |
+
How to use:<br>
|
76 |
+
1. Upload a style image.
|
77 |
+
2. Set stylization mode, only use style block by default.
|
78 |
+
2. Enter a text prompt, as done in normal text-to-image models.
|
79 |
+
3. Click the <b>Submit</b> button to begin customization.
|
80 |
+
4. Share your stylized photo with your friends and enjoy! π
|
81 |
+
|
82 |
+
|
83 |
+
Advanced usage:<br>
|
84 |
+
1. Click advanced options.
|
85 |
+
2. Upload another source image for image-based stylization using ControlNet.
|
86 |
+
3. Enter negative content prompt to avoid content leakage.
|
87 |
+
"""
|
88 |
+
|
89 |
+
article = r"""
|
90 |
+
---
|
91 |
+
```bibtex
|
92 |
+
@article{wang2024instantstyle,
|
93 |
+
title={InstantStyle: Free Lunch towards Style-Preserving in Text-to-Image Generation},
|
94 |
+
author={Wang, Haofan and Wang, Qixun and Bai, Xu and Qin, Zekui and Chen, Anthony},
|
95 |
+
journal={arXiv preprint arXiv:2404.02733},
|
96 |
+
year={2024}
|
97 |
+
}
|
98 |
+
```
|
99 |
+
"""
|
100 |
+
|
101 |
+
block = gr.Blocks().queue(max_size=10, api_open=True)
|
102 |
+
with block:
|
103 |
+
|
104 |
+
# description
|
105 |
+
gr.Markdown(title)
|
106 |
+
gr.Markdown(description)
|
107 |
+
|
108 |
+
with gr.Tabs():
|
109 |
+
with gr.Row():
|
110 |
+
with gr.Column():
|
111 |
+
|
112 |
+
with gr.Row():
|
113 |
+
with gr.Column():
|
114 |
+
image_pil = gr.Image(label="Style Image", type="pil")
|
115 |
+
|
116 |
+
target = gr.Radio(["Load only style blocks", "Load only layout blocks","Load style+layout block", "Load original IP-Adapter"],
|
117 |
+
value="Load only style blocks",
|
118 |
+
label="Style mode")
|
119 |
+
|
120 |
+
prompt = gr.Textbox(label="Prompt",
|
121 |
+
value="a cat, masterpiece, best quality, high quality")
|
122 |
+
|
123 |
+
scale = gr.Slider(minimum=0,maximum=2.0, step=0.01,value=1.0, label="Scale")
|
124 |
+
|
125 |
+
with gr.Accordion(open=False, label="Advanced Options"):
|
126 |
+
|
127 |
+
control_scale = gr.Slider(minimum=0,maximum=1.0, step=0.01,value=0.5, label="Controlnet conditioning scale")
|
128 |
+
|
129 |
+
n_prompt = gr.Textbox(label="Neg Prompt", value="text, watermark, lowres, low quality, worst quality, deformed, glitch, low contrast, noisy, saturation, blurry")
|
130 |
+
guidance_scale = gr.Slider(minimum=1,maximum=15.0, step=0.01,value=5.0, label="guidance scale")
|
131 |
+
num_inference_steps = gr.Slider(minimum=5,maximum=50.0, step=1.0,value=20, label="num inference steps")
|
132 |
+
seed = gr.Slider(minimum=-1000000,maximum=1000000,value=1, step=1, label="Seed Value")
|
133 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
134 |
+
|
135 |
+
generate_button = gr.Button("Generate Image")
|
136 |
+
|
137 |
+
with gr.Column():
|
138 |
+
generated_image = gr.Gallery(label="Generated Image")
|
139 |
+
|
140 |
+
generate_button.click(
|
141 |
+
fn=randomize_seed_fn,
|
142 |
+
inputs=[seed, randomize_seed],
|
143 |
+
outputs=seed,
|
144 |
+
queue=False,
|
145 |
+
api_name=False,
|
146 |
+
).then(
|
147 |
+
fn=create_image,
|
148 |
+
inputs=[image_pil,
|
149 |
+
prompt,
|
150 |
+
n_prompt,
|
151 |
+
scale,
|
152 |
+
control_scale,
|
153 |
+
guidance_scale,
|
154 |
+
num_inference_steps,
|
155 |
+
seed,
|
156 |
+
target],
|
157 |
+
outputs=[generated_image])
|
158 |
+
|
159 |
+
gr.Markdown(article)
|
160 |
+
|
161 |
block.launch(show_error=True)
|