fix errors
Browse files
app.py
CHANGED
@@ -1,15 +1,14 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from diffusers import AuraFlowPipeline
|
4 |
-
import spaces
|
5 |
import numpy as np
|
6 |
|
7 |
pipeline = AuraFlowPipeline.from_pretrained(
|
8 |
"fal/AuraFlow-v0.3",
|
9 |
torch_dtype=torch.float16,
|
10 |
-
variant="fp16",
|
11 |
use_safetensors=True,
|
12 |
-
).to("cuda")
|
13 |
|
14 |
STYLE_PRESETS = {
|
15 |
"None": "",
|
@@ -25,7 +24,6 @@ examples = [
|
|
25 |
{"prompt": "A majestic dragon soaring high above a range of snow-capped mountains under a golden sunset sky", "style": "Comic"},
|
26 |
{"prompt": "A shiba inu on a rocky cliff overlooking a vibrant sunset ocean view", "style": "Photorealistic"},
|
27 |
{"prompt": "A futuristic city skyline glowing with neon lights, towering skyscrapers, and flying cars under a stormy night", "style": "Cyberpunk"},
|
28 |
-
|
29 |
]
|
30 |
|
31 |
@spaces.GPU(duration=120)
|
@@ -35,7 +33,7 @@ def generate_images(
|
|
35 |
style,
|
36 |
width=1024,
|
37 |
height=1024,
|
38 |
-
steps=20,
|
39 |
guidance=5.0,
|
40 |
seed=1,
|
41 |
num_images=1,
|
@@ -43,7 +41,6 @@ def generate_images(
|
|
43 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
44 |
styled_prompt = f"{prompt}{STYLE_PRESETS[style]}"
|
45 |
gallery = []
|
46 |
-
|
47 |
for i in range(num_images):
|
48 |
image = pipeline(
|
49 |
prompt=styled_prompt,
|
@@ -56,7 +53,6 @@ def generate_images(
|
|
56 |
output_type="pil",
|
57 |
).images[0]
|
58 |
gallery.append((image, ""))
|
59 |
-
|
60 |
torch.cuda.empty_cache()
|
61 |
return gallery
|
62 |
|
@@ -78,7 +74,6 @@ def interface_fn(
|
|
78 |
raise gr.Error("Please enter a prompt!")
|
79 |
if randomize_seed:
|
80 |
seed = np.random.randint(0, 1000000)
|
81 |
-
|
82 |
gallery = generate_images(
|
83 |
prompt=prompt,
|
84 |
negative_prompt=negative_prompt,
|
@@ -90,7 +85,6 @@ def interface_fn(
|
|
90 |
seed=seed,
|
91 |
num_images=num_images
|
92 |
)
|
93 |
-
|
94 |
updated_history = update_history(gallery, history)
|
95 |
return gallery, seed, updated_history
|
96 |
|
@@ -103,39 +97,39 @@ def update_history(new_images, history):
|
|
103 |
|
104 |
def clear_result():
|
105 |
return gr.update(value=[]), gr.update(value=None)
|
106 |
-
|
107 |
-
#my custom css for layout
|
108 |
custom_css = """
|
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 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as interface:
|
@@ -162,13 +156,12 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as interface:
|
|
162 |
with gr.Accordion("Advanced Settings", open=False, elem_id="advanced_options"):
|
163 |
width_input = gr.Slider(256, 1536, step=256, value=1024, label="Width")
|
164 |
height_input = gr.Slider(256, 1536, step=256, value=1024, label="Height")
|
165 |
-
steps_input = gr.Slider(1, 50, step=1, value=20, label="Inference Steps")
|
166 |
guidance_input = gr.Slider(0, 10, step=0.5, value=5.0, label="Guidance Scale")
|
167 |
with gr.Row():
|
168 |
seed_input = gr.Number(value=1, label="Seed", visible=False)
|
169 |
randomize_seed_input = gr.Checkbox(value=True, label="Randomize Seed")
|
170 |
-
|
171 |
-
|
172 |
with gr.Column(scale=2):
|
173 |
image_output = gr.Gallery(
|
174 |
label="Generated Images",
|
@@ -179,14 +172,12 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as interface:
|
|
179 |
with gr.Row():
|
180 |
clear_btn = gr.Button("Clear", variant="secondary")
|
181 |
generate_btn = gr.Button("Generate", variant="primary")
|
182 |
-
|
183 |
history_gallery = gr.Gallery(
|
184 |
label="History",
|
185 |
columns=6,
|
186 |
object_fit="contain",
|
187 |
interactive=False
|
188 |
)
|
189 |
-
|
190 |
with gr.Row(equal_height=True, elem_classes=["example-row"]):
|
191 |
gr.Markdown("### Try these examples:")
|
192 |
with gr.Row(equal_height=True, elem_classes=["example-row"]):
|
@@ -199,18 +190,17 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as interface:
|
|
199 |
outputs=[prompt_input, style_input]
|
200 |
).then(
|
201 |
fn=interface_fn,
|
202 |
-
inputs=[prompt_input, neg_prompt_input, style_input, width_input, height_input,
|
203 |
-
steps_input, guidance_input, seed_input, num_images_input, randomize_seed_input, history_gallery],
|
204 |
outputs=[image_output, seed_input, history_gallery]
|
205 |
)
|
|
|
206 |
generate_btn.click(
|
207 |
fn=lambda: clear_result(),
|
208 |
inputs=[],
|
209 |
outputs=[image_output, seed_input]
|
210 |
).then(
|
211 |
fn=interface_fn,
|
212 |
-
inputs=[prompt_input, neg_prompt_input, style_input, width_input, height_input,
|
213 |
-
steps_input, guidance_input, seed_input, num_images_input, randomize_seed_input, history_gallery],
|
214 |
outputs=[image_output, seed_input, history_gallery]
|
215 |
)
|
216 |
|
|
|
1 |
+
import spaces
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
from diffusers import AuraFlowPipeline
|
|
|
5 |
import numpy as np
|
6 |
|
7 |
pipeline = AuraFlowPipeline.from_pretrained(
|
8 |
"fal/AuraFlow-v0.3",
|
9 |
torch_dtype=torch.float16,
|
|
|
10 |
use_safetensors=True,
|
11 |
+
).to("cuda")
|
12 |
|
13 |
STYLE_PRESETS = {
|
14 |
"None": "",
|
|
|
24 |
{"prompt": "A majestic dragon soaring high above a range of snow-capped mountains under a golden sunset sky", "style": "Comic"},
|
25 |
{"prompt": "A shiba inu on a rocky cliff overlooking a vibrant sunset ocean view", "style": "Photorealistic"},
|
26 |
{"prompt": "A futuristic city skyline glowing with neon lights, towering skyscrapers, and flying cars under a stormy night", "style": "Cyberpunk"},
|
|
|
27 |
]
|
28 |
|
29 |
@spaces.GPU(duration=120)
|
|
|
33 |
style,
|
34 |
width=1024,
|
35 |
height=1024,
|
36 |
+
steps=20,
|
37 |
guidance=5.0,
|
38 |
seed=1,
|
39 |
num_images=1,
|
|
|
41 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
42 |
styled_prompt = f"{prompt}{STYLE_PRESETS[style]}"
|
43 |
gallery = []
|
|
|
44 |
for i in range(num_images):
|
45 |
image = pipeline(
|
46 |
prompt=styled_prompt,
|
|
|
53 |
output_type="pil",
|
54 |
).images[0]
|
55 |
gallery.append((image, ""))
|
|
|
56 |
torch.cuda.empty_cache()
|
57 |
return gallery
|
58 |
|
|
|
74 |
raise gr.Error("Please enter a prompt!")
|
75 |
if randomize_seed:
|
76 |
seed = np.random.randint(0, 1000000)
|
|
|
77 |
gallery = generate_images(
|
78 |
prompt=prompt,
|
79 |
negative_prompt=negative_prompt,
|
|
|
85 |
seed=seed,
|
86 |
num_images=num_images
|
87 |
)
|
|
|
88 |
updated_history = update_history(gallery, history)
|
89 |
return gallery, seed, updated_history
|
90 |
|
|
|
97 |
|
98 |
def clear_result():
|
99 |
return gr.update(value=[]), gr.update(value=None)
|
100 |
+
|
101 |
+
# my custom css for layout
|
102 |
custom_css = """
|
103 |
+
.gr-button {margin: 5px;}
|
104 |
+
.output-image {border-radius: 8px;}
|
105 |
+
#advanced_options {margin-top: 20px;}
|
106 |
+
.style-dropdown {width: 100%; max-width: 800px;}
|
107 |
+
.gr-textbox {width: 100%;}
|
108 |
+
.example-row {margin-top: 20px;}
|
109 |
+
.example-button {white-space: normal; height: auto; min-height: 60px;}
|
110 |
+
/* Center the Generated Images gallery */
|
111 |
+
#output-gallery {
|
112 |
+
display: block;
|
113 |
+
width: 100%;
|
114 |
+
text-align: center;
|
115 |
+
}
|
116 |
+
#output-gallery .gallery {
|
117 |
+
display: inline-flex;
|
118 |
+
justify-content: center;
|
119 |
+
align-items: center;
|
120 |
+
flex-wrap: wrap;
|
121 |
+
margin: 0 auto;
|
122 |
+
}
|
123 |
+
#output-gallery .gallery > div {
|
124 |
+
display: flex;
|
125 |
+
justify-content: center;
|
126 |
+
align-items: center;
|
127 |
+
margin: 5px;
|
128 |
+
}
|
129 |
+
#output-gallery img {
|
130 |
+
display: block;
|
131 |
+
margin: 0 auto;
|
132 |
+
}
|
133 |
"""
|
134 |
|
135 |
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as interface:
|
|
|
156 |
with gr.Accordion("Advanced Settings", open=False, elem_id="advanced_options"):
|
157 |
width_input = gr.Slider(256, 1536, step=256, value=1024, label="Width")
|
158 |
height_input = gr.Slider(256, 1536, step=256, value=1024, label="Height")
|
159 |
+
steps_input = gr.Slider(1, 50, step=1, value=20, label="Inference Steps")
|
160 |
guidance_input = gr.Slider(0, 10, step=0.5, value=5.0, label="Guidance Scale")
|
161 |
with gr.Row():
|
162 |
seed_input = gr.Number(value=1, label="Seed", visible=False)
|
163 |
randomize_seed_input = gr.Checkbox(value=True, label="Randomize Seed")
|
164 |
+
num_images_input = gr.Slider(1, 4, step=1, value=1, label="Number of Images")
|
|
|
165 |
with gr.Column(scale=2):
|
166 |
image_output = gr.Gallery(
|
167 |
label="Generated Images",
|
|
|
172 |
with gr.Row():
|
173 |
clear_btn = gr.Button("Clear", variant="secondary")
|
174 |
generate_btn = gr.Button("Generate", variant="primary")
|
|
|
175 |
history_gallery = gr.Gallery(
|
176 |
label="History",
|
177 |
columns=6,
|
178 |
object_fit="contain",
|
179 |
interactive=False
|
180 |
)
|
|
|
181 |
with gr.Row(equal_height=True, elem_classes=["example-row"]):
|
182 |
gr.Markdown("### Try these examples:")
|
183 |
with gr.Row(equal_height=True, elem_classes=["example-row"]):
|
|
|
190 |
outputs=[prompt_input, style_input]
|
191 |
).then(
|
192 |
fn=interface_fn,
|
193 |
+
inputs=[prompt_input, neg_prompt_input, style_input, width_input, height_input, steps_input, guidance_input, seed_input, num_images_input, randomize_seed_input, history_gallery],
|
|
|
194 |
outputs=[image_output, seed_input, history_gallery]
|
195 |
)
|
196 |
+
|
197 |
generate_btn.click(
|
198 |
fn=lambda: clear_result(),
|
199 |
inputs=[],
|
200 |
outputs=[image_output, seed_input]
|
201 |
).then(
|
202 |
fn=interface_fn,
|
203 |
+
inputs=[prompt_input, neg_prompt_input, style_input, width_input, height_input, steps_input, guidance_input, seed_input, num_images_input, randomize_seed_input, history_gallery],
|
|
|
204 |
outputs=[image_output, seed_input, history_gallery]
|
205 |
)
|
206 |
|