Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,6 @@ import spaces
|
|
5 |
from diffusers import DiffusionPipeline
|
6 |
import torch
|
7 |
|
8 |
-
# Подключение к устройству
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
model_repo_id = "stabilityai/stable-diffusion-3.5-large"
|
11 |
|
@@ -18,58 +17,50 @@ pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
|
18 |
pipe = pipe.to(device)
|
19 |
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
-
MAX_IMAGE_SIZE =
|
22 |
|
23 |
-
|
24 |
-
def get_available_gpu_quota():
|
25 |
-
# Симуляция запроса (в реальном случае можно подключить API Hugging Face)
|
26 |
-
# Здесь вы можете вручную ввести оставшееся время
|
27 |
-
return 20 # Пример: 20 секунд доступно
|
28 |
-
|
29 |
-
# Динамическая настройка
|
30 |
-
@spaces.GPU(duration=min(get_available_gpu_quota(), 30)) # Устанавливаем время в пределах доступного
|
31 |
def infer(
|
32 |
prompt,
|
33 |
negative_prompt="",
|
34 |
seed=42,
|
35 |
randomize_seed=False,
|
36 |
-
width=
|
37 |
-
height=
|
38 |
guidance_scale=4.5,
|
39 |
-
num_inference_steps=
|
40 |
progress=gr.Progress(track_tqdm=True),
|
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 |
examples = [
|
70 |
-
"A
|
71 |
-
"A lush fantasy forest with glowing mushrooms, magical fireflies, and a small elf standing by an ancient tree, surrounded by mystical mist.",
|
72 |
-
"A dystopian cityscape at night with flying cars, neon holographic advertisements, and a lone figure standing on a rooftop, looking at the skyline."
|
73 |
]
|
74 |
|
75 |
# Define the custom theme with input styles
|
@@ -96,21 +87,26 @@ footer {
|
|
96 |
padding: 0;
|
97 |
overflow: hidden;
|
98 |
}
|
|
|
99 |
/* Import Google Fonts */
|
100 |
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&family=Montserrat:wght@500;700&display=swap');
|
|
|
101 |
/* Apply fonts to different elements */
|
102 |
body, input, button, textarea, select, .gr-button {
|
103 |
font-family: 'Roboto', sans-serif;
|
104 |
}
|
|
|
105 |
/* Make button text normal weight */
|
106 |
.generate-button, .generate-button .gr-button {
|
107 |
font-weight: normal !important;
|
108 |
}
|
|
|
109 |
/* Ensure headings use Montserrat */
|
110 |
h1, h2, h3, h4, h5, h6 {
|
111 |
font-family: 'Montserrat', sans-serif;
|
112 |
font-weight: 700;
|
113 |
}
|
|
|
114 |
/* Additional styling for sliders and checkboxes if needed */
|
115 |
input[type="range"]::-webkit-slider-thumb {
|
116 |
background: #5271FF;
|
@@ -124,13 +120,13 @@ input[type="range"]::-ms-thumb {
|
|
124 |
input[type="checkbox"]:checked {
|
125 |
background-color: #5271FF;
|
126 |
}
|
|
|
127 |
/* Make Prompt text bold */
|
128 |
.prompt-text {
|
129 |
font-weight: bold;
|
130 |
}
|
131 |
"""
|
132 |
|
133 |
-
# Создание интерфейса Gradio
|
134 |
with gr.Blocks(theme=CustomTheme(), css=css) as demo:
|
135 |
with gr.Column(elem_id="col-container"):
|
136 |
# Make "Prompt" bold using Markdown syntax and assign a class
|
@@ -154,26 +150,96 @@ with gr.Blocks(theme=CustomTheme(), css=css) as demo:
|
|
154 |
|
155 |
result = gr.Image(label="Result", show_label=False)
|
156 |
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
gr.Examples(
|
159 |
examples=examples,
|
160 |
inputs=[prompt],
|
161 |
-
outputs=[result],
|
162 |
fn=infer,
|
163 |
cache_examples=True,
|
164 |
cache_mode="lazy"
|
165 |
)
|
166 |
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
|
178 |
if __name__ == "__main__":
|
179 |
demo.launch(
|
|
|
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 |
|
|
|
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 |
+
try:
|
35 |
+
if randomize_seed:
|
36 |
+
seed = random.randint(0, MAX_SEED)
|
37 |
+
|
38 |
+
generator = torch.Generator().manual_seed(seed)
|
39 |
+
|
40 |
+
image = pipe(
|
41 |
+
prompt=prompt,
|
42 |
+
negative_prompt=negative_prompt,
|
43 |
+
guidance_scale=guidance_scale,
|
44 |
+
num_inference_steps=num_inference_steps,
|
45 |
+
width=width,
|
46 |
+
height=height,
|
47 |
+
generator=generator,
|
48 |
+
).images[0]
|
49 |
+
|
50 |
+
return image, seed
|
51 |
+
|
52 |
+
except Exception as e:
|
53 |
+
# Log the exception if needed
|
54 |
+
print(f"Error during image generation: {e}")
|
55 |
+
|
56 |
+
# Raise a Gradio-specific error with a user-friendly message
|
57 |
+
raise gr.Error(
|
58 |
+
"You have exceeded your GPU quota or encountered an unexpected error. "
|
59 |
+
"Please wait a moment and try again."
|
60 |
+
)
|
61 |
+
|
62 |
examples = [
|
63 |
+
"A border collie lying in some Fall leaves as the forest trees change colors",
|
|
|
|
|
64 |
]
|
65 |
|
66 |
# Define the custom theme with input styles
|
|
|
87 |
padding: 0;
|
88 |
overflow: hidden;
|
89 |
}
|
90 |
+
|
91 |
/* Import Google Fonts */
|
92 |
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&family=Montserrat:wght@500;700&display=swap');
|
93 |
+
|
94 |
/* Apply fonts to different elements */
|
95 |
body, input, button, textarea, select, .gr-button {
|
96 |
font-family: 'Roboto', sans-serif;
|
97 |
}
|
98 |
+
|
99 |
/* Make button text normal weight */
|
100 |
.generate-button, .generate-button .gr-button {
|
101 |
font-weight: normal !important;
|
102 |
}
|
103 |
+
|
104 |
/* Ensure headings use Montserrat */
|
105 |
h1, h2, h3, h4, h5, h6 {
|
106 |
font-family: 'Montserrat', sans-serif;
|
107 |
font-weight: 700;
|
108 |
}
|
109 |
+
|
110 |
/* Additional styling for sliders and checkboxes if needed */
|
111 |
input[type="range"]::-webkit-slider-thumb {
|
112 |
background: #5271FF;
|
|
|
120 |
input[type="checkbox"]:checked {
|
121 |
background-color: #5271FF;
|
122 |
}
|
123 |
+
|
124 |
/* Make Prompt text bold */
|
125 |
.prompt-text {
|
126 |
font-weight: bold;
|
127 |
}
|
128 |
"""
|
129 |
|
|
|
130 |
with gr.Blocks(theme=CustomTheme(), css=css) as demo:
|
131 |
with gr.Column(elem_id="col-container"):
|
132 |
# Make "Prompt" bold using Markdown syntax and assign a class
|
|
|
150 |
|
151 |
result = gr.Image(label="Result", show_label=False)
|
152 |
|
153 |
+
with gr.Accordion("Advanced Settings", open=False):
|
154 |
+
negative_prompt = gr.Text(
|
155 |
+
label="Negative Prompt",
|
156 |
+
max_lines=1,
|
157 |
+
placeholder="Enter a negative prompt",
|
158 |
+
visible=False,
|
159 |
+
)
|
160 |
+
|
161 |
+
seed = gr.Slider(
|
162 |
+
label="Seed",
|
163 |
+
minimum=0,
|
164 |
+
maximum=MAX_SEED,
|
165 |
+
step=1,
|
166 |
+
value=0,
|
167 |
+
)
|
168 |
+
|
169 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
170 |
+
|
171 |
+
with gr.Row():
|
172 |
+
width = gr.Slider(
|
173 |
+
label="Width",
|
174 |
+
minimum=512,
|
175 |
+
maximum=MAX_IMAGE_SIZE,
|
176 |
+
step=32,
|
177 |
+
value=1024,
|
178 |
+
)
|
179 |
+
|
180 |
+
height = gr.Slider(
|
181 |
+
label="Height",
|
182 |
+
minimum=512,
|
183 |
+
maximum=MAX_IMAGE_SIZE,
|
184 |
+
step=32,
|
185 |
+
value=1024,
|
186 |
+
)
|
187 |
+
|
188 |
+
with gr.Row():
|
189 |
+
guidance_scale = gr.Slider(
|
190 |
+
label="Guidance Scale",
|
191 |
+
minimum=0.0,
|
192 |
+
maximum=7.5,
|
193 |
+
step=0.1,
|
194 |
+
value=4.5,
|
195 |
+
)
|
196 |
+
|
197 |
+
num_inference_steps = gr.Slider(
|
198 |
+
label="Number of Inference Steps",
|
199 |
+
minimum=1,
|
200 |
+
maximum=50,
|
201 |
+
step=1,
|
202 |
+
value=40,
|
203 |
+
)
|
204 |
+
|
205 |
gr.Examples(
|
206 |
examples=examples,
|
207 |
inputs=[prompt],
|
208 |
+
outputs=[result, seed],
|
209 |
fn=infer,
|
210 |
cache_examples=True,
|
211 |
cache_mode="lazy"
|
212 |
)
|
213 |
|
214 |
+
# Use click method for the button and submit for the text field
|
215 |
+
run_button.click(
|
216 |
+
fn=infer,
|
217 |
+
inputs=[
|
218 |
+
prompt,
|
219 |
+
negative_prompt,
|
220 |
+
seed,
|
221 |
+
randomize_seed,
|
222 |
+
width,
|
223 |
+
height,
|
224 |
+
guidance_scale,
|
225 |
+
num_inference_steps,
|
226 |
+
],
|
227 |
+
outputs=[result, seed],
|
228 |
+
)
|
229 |
+
prompt.submit(
|
230 |
+
fn=infer,
|
231 |
+
inputs=[
|
232 |
+
prompt,
|
233 |
+
negative_prompt,
|
234 |
+
seed,
|
235 |
+
randomize_seed,
|
236 |
+
width,
|
237 |
+
height,
|
238 |
+
guidance_scale,
|
239 |
+
num_inference_steps,
|
240 |
+
],
|
241 |
+
outputs=[result, seed],
|
242 |
+
)
|
243 |
|
244 |
if __name__ == "__main__":
|
245 |
demo.launch(
|