Update app.py from anycoder
Browse files
app.py
CHANGED
|
@@ -21,7 +21,7 @@ print("Loading GLM-Image model... This may take a few minutes.")
|
|
| 21 |
pipe = GlmImagePipeline.from_pretrained(
|
| 22 |
"zai-org/GLM-Image",
|
| 23 |
torch_dtype=torch.bfloat16,
|
| 24 |
-
device_map="cuda"
|
| 25 |
)
|
| 26 |
print("Model loaded successfully!")
|
| 27 |
|
|
@@ -36,9 +36,7 @@ def calculate_duration(num_inference_steps: int) -> int:
|
|
| 36 |
Returns:
|
| 37 |
Estimated duration in seconds
|
| 38 |
"""
|
| 39 |
-
# Estimate ~3.75 seconds per inference step (typical for this model on H200)
|
| 40 |
step_duration = 3.75
|
| 41 |
-
# Base time for image loading/preprocessing
|
| 42 |
base_time = 15
|
| 43 |
return base_time + (num_inference_steps * step_duration)
|
| 44 |
|
|
@@ -99,7 +97,7 @@ def process_image(
|
|
| 99 |
num_inference_steps: int,
|
| 100 |
guidance_scale: float,
|
| 101 |
seed: int,
|
| 102 |
-
progress: gr.Progress = None
|
| 103 |
) -> tuple:
|
| 104 |
"""
|
| 105 |
Process the image through the GLM-Image pipeline.
|
|
@@ -180,6 +178,14 @@ def update_time_estimate(num_steps: int) -> str:
|
|
| 180 |
"""Update the estimated processing time display."""
|
| 181 |
return f"**Estimated time:** {estimate_display_time(num_steps)}"
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
custom_theme = gr.themes.Soft(
|
| 184 |
primary_hue="indigo",
|
| 185 |
secondary_hue="blue",
|
|
@@ -320,6 +326,7 @@ with gr.Blocks(fill_height=True) as demo:
|
|
| 320 |
show_label=True
|
| 321 |
)
|
| 322 |
|
|
|
|
| 323 |
download_btn = gr.DownloadButton(
|
| 324 |
"Download Image",
|
| 325 |
value=None,
|
|
@@ -378,8 +385,6 @@ with gr.Blocks(fill_height=True) as demo:
|
|
| 378 |
api_visibility="private"
|
| 379 |
)
|
| 380 |
|
| 381 |
-
# FIXED: Removed progress=gr.Progress() from click arguments
|
| 382 |
-
# In Gradio 6, progress is handled automatically via the function parameter
|
| 383 |
generate_btn.click(
|
| 384 |
fn=process_image,
|
| 385 |
inputs=[
|
|
@@ -392,40 +397,49 @@ with gr.Blocks(fill_height=True) as demo:
|
|
| 392 |
seed
|
| 393 |
],
|
| 394 |
outputs=[output_image, status]
|
| 395 |
-
# NOTE: progress is handled automatically by Gradio 6
|
| 396 |
-
# Just add progress: gr.Progress as a parameter in your function
|
| 397 |
)
|
| 398 |
|
| 399 |
def enable_download(img):
|
| 400 |
if img is not None:
|
| 401 |
-
return
|
| 402 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
|
| 404 |
output_image.change(
|
| 405 |
fn=enable_download,
|
| 406 |
inputs=output_image,
|
| 407 |
-
outputs=download_btn,
|
| 408 |
api_visibility="private"
|
| 409 |
)
|
| 410 |
|
| 411 |
def clear_all():
|
|
|
|
| 412 |
return {
|
| 413 |
input_image: None,
|
| 414 |
prompt: "",
|
| 415 |
output_image: None,
|
| 416 |
status: "Ready to generate! GPU will be allocated automatically.",
|
| 417 |
-
download_btn: gr.DownloadButton(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
}
|
| 419 |
|
| 420 |
clear_btn.click(
|
| 421 |
fn=clear_all,
|
| 422 |
-
outputs=
|
| 423 |
-
input_image: input_image,
|
| 424 |
-
prompt: prompt,
|
| 425 |
-
output_image: output_image,
|
| 426 |
-
status: status,
|
| 427 |
-
download_btn: download_btn
|
| 428 |
-
},
|
| 429 |
api_visibility="private"
|
| 430 |
)
|
| 431 |
|
|
@@ -466,10 +480,6 @@ demo.launch(
|
|
| 466 |
#input-image:hover, #output-image:hover {
|
| 467 |
border-color: var(--primary-400);
|
| 468 |
}
|
| 469 |
-
/* Make the generate button full width */
|
| 470 |
-
button#generate-btn {
|
| 471 |
-
width: 100% !important;
|
| 472 |
-
}
|
| 473 |
""",
|
| 474 |
footer_links=[
|
| 475 |
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
|
|
|
|
| 21 |
pipe = GlmImagePipeline.from_pretrained(
|
| 22 |
"zai-org/GLM-Image",
|
| 23 |
torch_dtype=torch.bfloat16,
|
| 24 |
+
device_map="cuda"
|
| 25 |
)
|
| 26 |
print("Model loaded successfully!")
|
| 27 |
|
|
|
|
| 36 |
Returns:
|
| 37 |
Estimated duration in seconds
|
| 38 |
"""
|
|
|
|
| 39 |
step_duration = 3.75
|
|
|
|
| 40 |
base_time = 15
|
| 41 |
return base_time + (num_inference_steps * step_duration)
|
| 42 |
|
|
|
|
| 97 |
num_inference_steps: int,
|
| 98 |
guidance_scale: float,
|
| 99 |
seed: int,
|
| 100 |
+
progress: gr.Progress = None
|
| 101 |
) -> tuple:
|
| 102 |
"""
|
| 103 |
Process the image through the GLM-Image pipeline.
|
|
|
|
| 178 |
"""Update the estimated processing time display."""
|
| 179 |
return f"**Estimated time:** {estimate_display_time(num_steps)}"
|
| 180 |
|
| 181 |
+
def create_disabled_download_button() -> gr.DownloadButton:
|
| 182 |
+
"""Create a disabled DownloadButton component."""
|
| 183 |
+
return gr.DownloadButton(
|
| 184 |
+
value=None,
|
| 185 |
+
interactive=False,
|
| 186 |
+
variant="secondary"
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
custom_theme = gr.themes.Soft(
|
| 190 |
primary_hue="indigo",
|
| 191 |
secondary_hue="blue",
|
|
|
|
| 326 |
show_label=True
|
| 327 |
)
|
| 328 |
|
| 329 |
+
# Initialize download button as disabled
|
| 330 |
download_btn = gr.DownloadButton(
|
| 331 |
"Download Image",
|
| 332 |
value=None,
|
|
|
|
| 385 |
api_visibility="private"
|
| 386 |
)
|
| 387 |
|
|
|
|
|
|
|
| 388 |
generate_btn.click(
|
| 389 |
fn=process_image,
|
| 390 |
inputs=[
|
|
|
|
| 397 |
seed
|
| 398 |
],
|
| 399 |
outputs=[output_image, status]
|
|
|
|
|
|
|
| 400 |
)
|
| 401 |
|
| 402 |
def enable_download(img):
|
| 403 |
if img is not None:
|
| 404 |
+
return {
|
| 405 |
+
download_btn: gr.DownloadButton(
|
| 406 |
+
value=img,
|
| 407 |
+
interactive=True,
|
| 408 |
+
variant="secondary"
|
| 409 |
+
)
|
| 410 |
+
}
|
| 411 |
+
return {
|
| 412 |
+
download_btn: gr.DownloadButton(
|
| 413 |
+
value=None,
|
| 414 |
+
interactive=False,
|
| 415 |
+
variant="secondary"
|
| 416 |
+
)
|
| 417 |
+
}
|
| 418 |
|
| 419 |
output_image.change(
|
| 420 |
fn=enable_download,
|
| 421 |
inputs=output_image,
|
| 422 |
+
outputs=[download_btn],
|
| 423 |
api_visibility="private"
|
| 424 |
)
|
| 425 |
|
| 426 |
def clear_all():
|
| 427 |
+
"""Clear all inputs and outputs."""
|
| 428 |
return {
|
| 429 |
input_image: None,
|
| 430 |
prompt: "",
|
| 431 |
output_image: None,
|
| 432 |
status: "Ready to generate! GPU will be allocated automatically.",
|
| 433 |
+
download_btn: gr.DownloadButton(
|
| 434 |
+
value=None,
|
| 435 |
+
interactive=False,
|
| 436 |
+
variant="secondary"
|
| 437 |
+
)
|
| 438 |
}
|
| 439 |
|
| 440 |
clear_btn.click(
|
| 441 |
fn=clear_all,
|
| 442 |
+
outputs=[input_image, prompt, output_image, status, download_btn],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
api_visibility="private"
|
| 444 |
)
|
| 445 |
|
|
|
|
| 480 |
#input-image:hover, #output-image:hover {
|
| 481 |
border-color: var(--primary-400);
|
| 482 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 483 |
""",
|
| 484 |
footer_links=[
|
| 485 |
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"},
|