Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -419,74 +419,58 @@ def create_demo():
|
|
| 419 |
preview_video = gr.Video(label="Preview Video", elem_classes="preview_media", visible=False)
|
| 420 |
with gr.Column(scale=2):
|
| 421 |
url_input = gr.Textbox(label="Image / Video URL", placeholder="https://...", lines=1)
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
with gr.
|
| 425 |
-
api_key = gr.Textbox(label="API Key", type="password", max_lines=1)
|
| 426 |
-
with gr.Row(): # Buttons in the same row
|
| 427 |
submit_btn = gr.Button("Submit")
|
| 428 |
clear_btn = gr.Button("Clear")
|
| 429 |
output_md = gr.Markdown("")
|
| 430 |
status_state = gr.State("idle")
|
| 431 |
|
| 432 |
def load_preview(url: str):
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
head = safe_head(url)
|
| 440 |
-
if head and any(url.lower().endswith(ext) for ext in VIDEO_EXTS):
|
| 441 |
-
return empty_img, gr.update(value=url, visible=True)
|
| 442 |
-
|
| 443 |
-
try:
|
| 444 |
r = safe_get(url)
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 451 |
|
| 452 |
url_input.change(fn=load_preview, inputs=[url_input], outputs=[preview_image, preview_video])
|
| 453 |
|
| 454 |
def clear_all():
|
| 455 |
-
return "", None, None, "idle"
|
| 456 |
-
|
| 457 |
-
clear_btn.click(fn=clear_all, inputs=[], outputs=[url_input, preview_image, preview_video, status_state])
|
| 458 |
|
| 459 |
def worker(url: str, prompt: str, key: str, progress=gr.Progress()):
|
| 460 |
try:
|
| 461 |
-
if not url:
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
src = url
|
| 465 |
progress(0, desc="Starting processing...")
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
prompt or "",
|
| 471 |
-
key or "",
|
| 472 |
-
progress
|
| 473 |
-
)
|
| 474 |
-
|
| 475 |
-
if hasattr(result, "result"):
|
| 476 |
-
result = result.result()
|
| 477 |
-
|
| 478 |
-
return result if result else "Error: no result returned."
|
| 479 |
-
|
| 480 |
except Exception as e:
|
| 481 |
-
return f"Unexpected
|
| 482 |
|
| 483 |
-
submit_btn.click(fn=worker, inputs=[url_input, custom_prompt, api_key], outputs=[output_md], queue=True)
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
)
|
| 488 |
|
| 489 |
-
status_state.change(fn=
|
| 490 |
|
| 491 |
return demo
|
| 492 |
|
|
|
|
| 419 |
preview_video = gr.Video(label="Preview Video", elem_classes="preview_media", visible=False)
|
| 420 |
with gr.Column(scale=2):
|
| 421 |
url_input = gr.Textbox(label="Image / Video URL", placeholder="https://...", lines=1)
|
| 422 |
+
custom_prompt = gr.Textbox(label="Prompt (optional)", lines=4, value="")
|
| 423 |
+
api_key = gr.Textbox(label="Mistral API Key (optional)", type="password", max_lines=1)
|
| 424 |
+
with gr.Row():
|
|
|
|
|
|
|
| 425 |
submit_btn = gr.Button("Submit")
|
| 426 |
clear_btn = gr.Button("Clear")
|
| 427 |
output_md = gr.Markdown("")
|
| 428 |
status_state = gr.State("idle")
|
| 429 |
|
| 430 |
def load_preview(url: str):
|
| 431 |
+
if not url:
|
| 432 |
+
return gr.update(value=None, visible=False), gr.update(value=None, visible=False)
|
| 433 |
+
if is_remote(url) and any(url.lower().endswith(ext) for ext in VIDEO_EXTS):
|
| 434 |
+
return gr.update(value=None, visible=False), gr.update(value=url, visible=True)
|
| 435 |
+
try:
|
| 436 |
+
if is_remote(url):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
r = safe_get(url)
|
| 438 |
+
img_bytes = r.content
|
| 439 |
+
else:
|
| 440 |
+
with open(url, "rb") as f:
|
| 441 |
+
img_bytes = f.read()
|
| 442 |
+
img = Image.open(BytesIO(img_bytes))
|
| 443 |
+
if getattr(img, "is_animated", False):
|
| 444 |
+
img.seek(0)
|
| 445 |
+
return gr.update(value=img.convert("RGB"), visible=True), gr.update(value=None, visible=False)
|
| 446 |
+
except Exception:
|
| 447 |
+
return gr.update(value=None, visible=False), gr.update(value=None, visible=False)
|
| 448 |
|
| 449 |
url_input.change(fn=load_preview, inputs=[url_input], outputs=[preview_image, preview_video])
|
| 450 |
|
| 451 |
def clear_all():
|
| 452 |
+
return "", None, None, "idle", ""
|
| 453 |
+
|
| 454 |
+
clear_btn.click(fn=clear_all, inputs=[], outputs=[url_input, preview_image, preview_video, status_state, output_md])
|
| 455 |
|
| 456 |
def worker(url: str, prompt: str, key: str, progress=gr.Progress()):
|
| 457 |
try:
|
| 458 |
+
if not url:
|
| 459 |
+
return ("error", "**Error:** No URL provided.")
|
|
|
|
|
|
|
| 460 |
progress(0, desc="Starting processing...")
|
| 461 |
+
fut = run_blocking_in_thread(process_media, url, prompt or "", key or "", progress)
|
| 462 |
+
res = fut.result()
|
| 463 |
+
status = "done" if not (isinstance(res, str) and res.lower().startswith("error")) else "error"
|
| 464 |
+
return (status, res if isinstance(res, str) else str(res))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
except Exception as e:
|
| 466 |
+
return ("error", f"Unexpected worker error: {e}")
|
| 467 |
|
| 468 |
+
submit_btn.click(fn=worker, inputs=[url_input, custom_prompt, api_key], outputs=[status_state, output_md], queue=True)
|
| 469 |
+
|
| 470 |
+
def btn_label_from_state(s):
|
| 471 |
+
return _btn_label_for_status(s)
|
|
|
|
| 472 |
|
| 473 |
+
status_state.change(fn=btn_label_from_state, inputs=[status_state], outputs=[submit_btn])
|
| 474 |
|
| 475 |
return demo
|
| 476 |
|