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|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import random
|
4 |
+
import spaces
|
5 |
+
from diffusers import DiffusionPipeline
|
6 |
+
import torch
|
7 |
+
|
8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
+
model_repo_id = "stabilityai/stable-diffusion-3.5-large"
|
10 |
+
|
11 |
+
if torch.cuda.is_available():
|
12 |
+
torch_dtype = torch.bfloat16
|
13 |
+
else:
|
14 |
+
torch_dtype = torch.float32
|
15 |
+
|
16 |
+
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
17 |
+
pipe = pipe.to(device)
|
18 |
+
|
19 |
+
MAX_SEED = np.iinfo(np.int32).max
|
20 |
+
MAX_IMAGE_SIZE = 1024
|
21 |
+
|
22 |
+
@spaces.GPU(duration=65)
|
23 |
+
def infer(
|
24 |
+
prompt,
|
25 |
+
negative_prompt="",
|
26 |
+
seed=42,
|
27 |
+
randomize_seed=False,
|
28 |
+
width=1024,
|
29 |
+
height=1024,
|
30 |
+
guidance_scale=4.5,
|
31 |
+
num_inference_steps=40,
|
32 |
+
progress=gr.Progress(track_tqdm=True),
|
33 |
+
):
|
34 |
+
if randomize_seed:
|
35 |
+
seed = random.randint(0, MAX_SEED)
|
36 |
+
|
37 |
+
generator = torch.Generator().manual_seed(seed)
|
38 |
+
|
39 |
+
image = pipe(
|
40 |
+
prompt=prompt,
|
41 |
+
negative_prompt=negative_prompt,
|
42 |
+
guidance_scale=guidance_scale,
|
43 |
+
num_inference_steps=num_inference_steps,
|
44 |
+
width=width,
|
45 |
+
height=height,
|
46 |
+
generator=generator,
|
47 |
+
).images[0]
|
48 |
+
|
49 |
+
return image, seed
|
50 |
+
|
51 |
+
# Enhanced examples with creative prompts
|
52 |
+
examples = [
|
53 |
+
"A capybara wearing a suit holding a sign that reads Hello World",
|
54 |
+
"A steampunk-style flying ship made of brass and wood, floating through cotton candy clouds",
|
55 |
+
"A magical library where books are flying and glowing, with a wise owl librarian",
|
56 |
+
"A cyberpunk street food vendor selling neon-colored dumplings in the rain",
|
57 |
+
"A group of penguins having a formal tea party in the Antarctic",
|
58 |
+
"A treehouse city at sunset with bioluminescent plants and floating lanterns"
|
59 |
+
]
|
60 |
+
|
61 |
+
# Custom CSS with modern styling
|
62 |
+
css = """
|
63 |
+
:root {
|
64 |
+
--primary-color: #7B2CBF;
|
65 |
+
--secondary-color: #9D4EDD;
|
66 |
+
--background-color: #10002B;
|
67 |
+
--text-color: #E0AAFF;
|
68 |
+
--card-bg: #240046;
|
69 |
+
}
|
70 |
+
|
71 |
+
#col-container {
|
72 |
+
max-width: 850px !important;
|
73 |
+
margin: 0 auto;
|
74 |
+
padding: 20px;
|
75 |
+
background: var(--background-color);
|
76 |
+
border-radius: 15px;
|
77 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
78 |
+
}
|
79 |
+
|
80 |
+
.main-title {
|
81 |
+
color: var(--text-color) !important;
|
82 |
+
text-align: center;
|
83 |
+
font-size: 2.5em !important;
|
84 |
+
margin-bottom: 1em !important;
|
85 |
+
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
|
86 |
+
}
|
87 |
+
|
88 |
+
.gradio-container {
|
89 |
+
background: var(--background-color) !important;
|
90 |
+
color: var(--text-color) !important;
|
91 |
+
}
|
92 |
+
|
93 |
+
.gr-button {
|
94 |
+
background: var(--primary-color) !important;
|
95 |
+
border: none !important;
|
96 |
+
color: white !important;
|
97 |
+
transition: transform 0.2s !important;
|
98 |
+
}
|
99 |
+
|
100 |
+
.gr-button:hover {
|
101 |
+
transform: translateY(-2px) !important;
|
102 |
+
background: var(--secondary-color) !important;
|
103 |
+
}
|
104 |
+
|
105 |
+
.gr-input, .gr-box {
|
106 |
+
background: var(--card-bg) !important;
|
107 |
+
border: 1px solid var(--primary-color) !important;
|
108 |
+
color: var(--text-color) !important;
|
109 |
+
}
|
110 |
+
|
111 |
+
.footer-custom a {
|
112 |
+
color: var(--text-color);
|
113 |
+
text-decoration: none;
|
114 |
+
margin: 0 10px;
|
115 |
+
transition: color 0.3s;
|
116 |
+
}
|
117 |
+
|
118 |
+
.footer-custom a:hover {
|
119 |
+
color: var(--secondary-color);
|
120 |
+
text-decoration: underline;
|
121 |
+
}
|
122 |
+
"""
|
123 |
+
|
124 |
+
# Footer HTML
|
125 |
+
footer = """
|
126 |
+
<div class="footer-custom" style="text-align: center; margin-top: 20px; color: #f8f8f2;">
|
127 |
+
<a href="https://www.linkedin.com/in/pejman-ebrahimi-4a60151a7/" target="_blank">LinkedIn</a> |
|
128 |
+
<a href="https://github.com/arad1367" target="_blank">GitHub</a> |
|
129 |
+
<a href="https://arad1367.pythonanywhere.com/" target="_blank">Live demo of my PhD defense</a> |
|
130 |
+
<a href="https://huggingface.co/stabilityai/stable-diffusion-3.5-large" target="_blank">stable-diffusion-3.5-large model</a> |
|
131 |
+
<a href="https://huggingface.co/spaces/stabilityai/stable-diffusion-3.5-large-turbo" target="_blank">stable-diffusion-3.5-large-turbo</a> |
|
132 |
+
<a href="https://stability.ai/license" target="_blank">Stability.ai licence</a>
|
133 |
+
<br>
|
134 |
+
<p style="margin-top: 10px;">Made with π by Pejman Ebrahimi</p>
|
135 |
+
</div>
|
136 |
+
"""
|
137 |
+
|
138 |
+
with gr.Blocks(css=css) as demo:
|
139 |
+
with gr.Column(elem_id="col-container"):
|
140 |
+
gr.HTML(
|
141 |
+
'<h1 class="main-title">Stable Diffusion 3.5 Large (8B)</h1>'
|
142 |
+
'<div style="text-align: center; margin-bottom: 20px;">'
|
143 |
+
'<a href="https://stability.ai" target="_blank" style="color: #E0AAFF;">Visit Stability.ai</a>'
|
144 |
+
'</div>'
|
145 |
+
)
|
146 |
+
|
147 |
+
with gr.Row():
|
148 |
+
prompt = gr.Text(
|
149 |
+
label="Prompt",
|
150 |
+
show_label=False,
|
151 |
+
max_lines=1,
|
152 |
+
placeholder="Enter your prompt",
|
153 |
+
container=False,
|
154 |
+
)
|
155 |
+
run_button = gr.Button("Generate", scale=0, variant="primary")
|
156 |
+
|
157 |
+
result = gr.Image(label="Result", show_label=False)
|
158 |
+
|
159 |
+
with gr.Accordion("Advanced Settings", open=False):
|
160 |
+
negative_prompt = gr.Text(
|
161 |
+
label="Negative prompt",
|
162 |
+
max_lines=1,
|
163 |
+
placeholder="Enter a negative prompt",
|
164 |
+
visible=False,
|
165 |
+
)
|
166 |
+
|
167 |
+
seed = gr.Slider(
|
168 |
+
label="Seed",
|
169 |
+
minimum=0,
|
170 |
+
maximum=MAX_SEED,
|
171 |
+
step=1,
|
172 |
+
value=0,
|
173 |
+
)
|
174 |
+
|
175 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
176 |
+
|
177 |
+
with gr.Row():
|
178 |
+
width = gr.Slider(
|
179 |
+
label="Width",
|
180 |
+
minimum=512,
|
181 |
+
maximum=MAX_IMAGE_SIZE,
|
182 |
+
step=32,
|
183 |
+
value=1024,
|
184 |
+
)
|
185 |
+
height = gr.Slider(
|
186 |
+
label="Height",
|
187 |
+
minimum=512,
|
188 |
+
maximum=MAX_IMAGE_SIZE,
|
189 |
+
step=32,
|
190 |
+
value=1024,
|
191 |
+
)
|
192 |
+
|
193 |
+
with gr.Row():
|
194 |
+
guidance_scale = gr.Slider(
|
195 |
+
label="Guidance scale",
|
196 |
+
minimum=0.0,
|
197 |
+
maximum=7.5,
|
198 |
+
step=0.1,
|
199 |
+
value=4.5,
|
200 |
+
)
|
201 |
+
num_inference_steps = gr.Slider(
|
202 |
+
label="Number of inference steps",
|
203 |
+
minimum=1,
|
204 |
+
maximum=50,
|
205 |
+
step=1,
|
206 |
+
value=40,
|
207 |
+
)
|
208 |
+
|
209 |
+
gr.Examples(
|
210 |
+
examples=examples,
|
211 |
+
inputs=[prompt],
|
212 |
+
outputs=[result, seed],
|
213 |
+
fn=infer,
|
214 |
+
cache_examples=True,
|
215 |
+
cache_mode="lazy"
|
216 |
+
)
|
217 |
+
|
218 |
+
gr.HTML(footer)
|
219 |
+
|
220 |
+
gr.on(
|
221 |
+
triggers=[run_button.click, prompt.submit],
|
222 |
+
fn=infer,
|
223 |
+
inputs=[
|
224 |
+
prompt,
|
225 |
+
negative_prompt,
|
226 |
+
seed,
|
227 |
+
randomize_seed,
|
228 |
+
width,
|
229 |
+
height,
|
230 |
+
guidance_scale,
|
231 |
+
num_inference_steps,
|
232 |
+
],
|
233 |
+
outputs=[result, seed],
|
234 |
+
)
|
235 |
+
|
236 |
+
if __name__ == "__main__":
|
237 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
accelerate
|
2 |
+
diffusers
|
3 |
+
torch
|
4 |
+
transformers
|
5 |
+
git+https://github.com/huggingface/diffusers.git
|
6 |
+
sentencepiece
|