Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -5,16 +5,15 @@ import time
|
|
5 |
from os import path
|
6 |
from safetensors.torch import load_file
|
7 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
8 |
|
|
|
9 |
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
10 |
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
11 |
os.environ["HF_HUB_CACHE"] = cache_path
|
12 |
os.environ["HF_HOME"] = cache_path
|
13 |
-
|
14 |
-
import gradio as gr
|
15 |
-
import torch
|
16 |
-
from diffusers import FluxPipeline
|
17 |
-
|
18 |
torch.backends.cuda.matmul.allow_tf32 = True
|
19 |
|
20 |
class timer:
|
@@ -27,6 +26,7 @@ class timer:
|
|
27 |
end = time.time()
|
28 |
print(f"{self.method} took {str(round(end - self.start, 2))}s")
|
29 |
|
|
|
30 |
if not path.exists(cache_path):
|
31 |
os.makedirs(cache_path, exist_ok=True)
|
32 |
|
@@ -35,43 +35,143 @@ pipe.load_lora_weights(hf_hub_download("ByteDance/Hyper-SD", "Hyper-FLUX.1-dev-8
|
|
35 |
pipe.fuse_lora(lora_scale=0.125)
|
36 |
pipe.to(device="cuda", dtype=torch.bfloat16)
|
37 |
|
|
|
38 |
css = """
|
39 |
-
footer {
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
}
|
42 |
"""
|
43 |
|
44 |
-
|
45 |
-
with gr.Blocks(theme=
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
with gr.Column(scale=3):
|
49 |
with gr.Group():
|
50 |
prompt = gr.Textbox(
|
51 |
-
label="
|
52 |
-
placeholder="
|
53 |
-
lines=3
|
|
|
54 |
)
|
55 |
|
56 |
with gr.Accordion("Advanced Settings", open=False):
|
57 |
with gr.Group():
|
58 |
with gr.Row():
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
with gr.Row():
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
seed = gr.Number(
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
generate_btn = gr.Button(
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
with gr.Column(scale=4):
|
71 |
-
output = gr.Image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
@spaces.GPU
|
76 |
def process_image(height, width, steps, scales, prompt, seed):
|
77 |
global pipe
|
@@ -85,12 +185,20 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css) as demo:
|
|
85 |
width=int(width),
|
86 |
max_sequence_length=256
|
87 |
).images[0]
|
88 |
-
|
|
|
89 |
generate_btn.click(
|
|
|
|
|
|
|
|
|
90 |
process_image,
|
91 |
inputs=[height, width, steps, scales, prompt, seed],
|
92 |
outputs=output
|
|
|
|
|
|
|
93 |
)
|
94 |
|
95 |
if __name__ == "__main__":
|
96 |
-
demo.launch()
|
|
|
5 |
from os import path
|
6 |
from safetensors.torch import load_file
|
7 |
from huggingface_hub import hf_hub_download
|
8 |
+
import gradio as gr
|
9 |
+
import torch
|
10 |
+
from diffusers import FluxPipeline
|
11 |
|
12 |
+
# Setup and initialization code remains the same
|
13 |
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
14 |
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
15 |
os.environ["HF_HUB_CACHE"] = cache_path
|
16 |
os.environ["HF_HOME"] = cache_path
|
|
|
|
|
|
|
|
|
|
|
17 |
torch.backends.cuda.matmul.allow_tf32 = True
|
18 |
|
19 |
class timer:
|
|
|
26 |
end = time.time()
|
27 |
print(f"{self.method} took {str(round(end - self.start, 2))}s")
|
28 |
|
29 |
+
# Model initialization
|
30 |
if not path.exists(cache_path):
|
31 |
os.makedirs(cache_path, exist_ok=True)
|
32 |
|
|
|
35 |
pipe.fuse_lora(lora_scale=0.125)
|
36 |
pipe.to(device="cuda", dtype=torch.bfloat16)
|
37 |
|
38 |
+
# Custom CSS for enhanced visual design
|
39 |
css = """
|
40 |
+
footer {display: none !important}
|
41 |
+
.container {max-width: 1200px; margin: auto;}
|
42 |
+
.gr-form {border-radius: 12px; padding: 20px; background: rgba(255, 255, 255, 0.05);}
|
43 |
+
.gr-box {border-radius: 8px; border: 1px solid rgba(255, 255, 255, 0.1);}
|
44 |
+
.gr-button {
|
45 |
+
border-radius: 8px;
|
46 |
+
background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%);
|
47 |
+
border: none;
|
48 |
+
color: white;
|
49 |
+
transition: transform 0.2s ease;
|
50 |
+
}
|
51 |
+
.gr-button:hover {
|
52 |
+
transform: translateY(-2px);
|
53 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
54 |
+
}
|
55 |
+
.gr-input {background: rgba(255, 255, 255, 0.05) !important;}
|
56 |
+
.gr-input:focus {border-color: #4B79A1 !important;}
|
57 |
+
.title-text {
|
58 |
+
text-align: center;
|
59 |
+
font-size: 2.5em;
|
60 |
+
font-weight: bold;
|
61 |
+
background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%);
|
62 |
+
-webkit-background-clip: text;
|
63 |
+
-webkit-text-fill-color: transparent;
|
64 |
+
margin-bottom: 1em;
|
65 |
}
|
66 |
"""
|
67 |
|
68 |
+
# Create Gradio interface with enhanced design
|
69 |
+
with gr.Blocks(theme=gr.themes.Soft(
|
70 |
+
primary_hue="blue",
|
71 |
+
secondary_hue="slate",
|
72 |
+
neutral_hue="slate",
|
73 |
+
font=gr.themes.GoogleFont("Inter")
|
74 |
+
), css=css) as demo:
|
75 |
+
|
76 |
+
gr.HTML("""
|
77 |
+
<div class="title-text">AI Image Generator</div>
|
78 |
+
<div style="text-align: center; margin-bottom: 2em; color: #666;">
|
79 |
+
Create stunning images from your descriptions using advanced AI
|
80 |
+
</div>
|
81 |
+
""")
|
82 |
+
|
83 |
+
with gr.Row().style(equal_height=True):
|
84 |
with gr.Column(scale=3):
|
85 |
with gr.Group():
|
86 |
prompt = gr.Textbox(
|
87 |
+
label="Image Description",
|
88 |
+
placeholder="Describe the image you want to create...",
|
89 |
+
lines=3,
|
90 |
+
elem_classes="gr-input"
|
91 |
)
|
92 |
|
93 |
with gr.Accordion("Advanced Settings", open=False):
|
94 |
with gr.Group():
|
95 |
with gr.Row():
|
96 |
+
with gr.Column(scale=1):
|
97 |
+
height = gr.Slider(
|
98 |
+
label="Height",
|
99 |
+
minimum=256,
|
100 |
+
maximum=1152,
|
101 |
+
step=64,
|
102 |
+
value=1024,
|
103 |
+
elem_classes="gr-input"
|
104 |
+
)
|
105 |
+
with gr.Column(scale=1):
|
106 |
+
width = gr.Slider(
|
107 |
+
label="Width",
|
108 |
+
minimum=256,
|
109 |
+
maximum=1152,
|
110 |
+
step=64,
|
111 |
+
value=1024,
|
112 |
+
elem_classes="gr-input"
|
113 |
+
)
|
114 |
|
115 |
with gr.Row():
|
116 |
+
with gr.Column(scale=1):
|
117 |
+
steps = gr.Slider(
|
118 |
+
label="Inference Steps",
|
119 |
+
minimum=6,
|
120 |
+
maximum=25,
|
121 |
+
step=1,
|
122 |
+
value=8,
|
123 |
+
elem_classes="gr-input"
|
124 |
+
)
|
125 |
+
with gr.Column(scale=1):
|
126 |
+
scales = gr.Slider(
|
127 |
+
label="Guidance Scale",
|
128 |
+
minimum=0.0,
|
129 |
+
maximum=5.0,
|
130 |
+
step=0.1,
|
131 |
+
value=3.5,
|
132 |
+
elem_classes="gr-input"
|
133 |
+
)
|
134 |
|
135 |
+
seed = gr.Number(
|
136 |
+
label="Seed (for reproducibility)",
|
137 |
+
value=3413,
|
138 |
+
precision=0,
|
139 |
+
elem_classes="gr-input"
|
140 |
+
)
|
141 |
|
142 |
+
generate_btn = gr.Button(
|
143 |
+
"✨ Generate Image",
|
144 |
+
variant="primary",
|
145 |
+
scale=1,
|
146 |
+
elem_classes="gr-button"
|
147 |
+
)
|
148 |
+
|
149 |
+
gr.HTML("""
|
150 |
+
<div style="margin-top: 1em; padding: 1em; border-radius: 8px; background: rgba(255, 255, 255, 0.05);">
|
151 |
+
<h4 style="margin: 0 0 0.5em 0;">Tips for best results:</h4>
|
152 |
+
<ul style="margin: 0; padding-left: 1.2em;">
|
153 |
+
<li>Be specific in your descriptions</li>
|
154 |
+
<li>Include details about style, lighting, and mood</li>
|
155 |
+
<li>Experiment with different guidance scales</li>
|
156 |
+
</ul>
|
157 |
+
</div>
|
158 |
+
""")
|
159 |
+
|
160 |
with gr.Column(scale=4):
|
161 |
+
output = gr.Image(
|
162 |
+
label="Generated Image",
|
163 |
+
elem_classes="gr-box",
|
164 |
+
height=512
|
165 |
+
)
|
166 |
+
|
167 |
+
with gr.Group(visible=False) as loading_info:
|
168 |
+
gr.HTML("""
|
169 |
+
<div style="text-align: center; padding: 1em;">
|
170 |
+
<div style="display: inline-block; animation: spin 1s linear infinite;">⚙️</div>
|
171 |
+
<p>Generating your image...</p>
|
172 |
+
</div>
|
173 |
+
""")
|
174 |
|
|
|
|
|
175 |
@spaces.GPU
|
176 |
def process_image(height, width, steps, scales, prompt, seed):
|
177 |
global pipe
|
|
|
185 |
width=int(width),
|
186 |
max_sequence_length=256
|
187 |
).images[0]
|
188 |
+
|
189 |
+
# Add loading state
|
190 |
generate_btn.click(
|
191 |
+
fn=lambda: gr.update(visible=True),
|
192 |
+
outputs=[loading_info],
|
193 |
+
queue=False
|
194 |
+
).then(
|
195 |
process_image,
|
196 |
inputs=[height, width, steps, scales, prompt, seed],
|
197 |
outputs=output
|
198 |
+
).then(
|
199 |
+
fn=lambda: gr.update(visible=False),
|
200 |
+
outputs=[loading_info]
|
201 |
)
|
202 |
|
203 |
if __name__ == "__main__":
|
204 |
+
demo.launch()
|