Hug0endob commited on
Commit
3ab874b
·
verified ·
1 Parent(s): 0dc03f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -50
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
- with gr.Accordion("Prompt (optional)", open=False):
423
- custom_prompt = gr.Textbox(label="Prompt", lines=4, value="")
424
- with gr.Accordion("Mistral API Key (optional)", open=False):
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
- empty_img = gr.update(value=None, visible=False)
434
- empty_vid = gr.update(value=None, visible=False)
435
-
436
- if not url: return empty_img, empty_vid
437
-
438
- if is_remote(url):
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
- img = Image.open(BytesIO(r.content))
446
- if getattr(img, "is_animated", False): img.seek(0)
447
- return gr.update(value=img.convert("RGB"), visible=True), empty_vid
448
- except Exception:
449
- return empty_img, empty_vid
450
- return empty_img, empty_vid
 
 
 
 
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
- raise ValueError("No URL provided.")
463
-
464
- src = url
465
  progress(0, desc="Starting processing...")
466
-
467
- result = run_blocking_in_thread(
468
- process_media,
469
- src,
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 error in worker: {e}"
482
 
483
- submit_btn.click(fn=worker, inputs=[url_input, custom_prompt, api_key], outputs=[output_md], queue=True).then(
484
- fn=lambda res: ("error", f"**Error:** {res}") if isinstance(res, str) and res.lower().startswith("error") else ("done", res),
485
- inputs=[output_md],
486
- outputs=[status_state, output_md],
487
- )
488
 
489
- status_state.change(fn=lambda s: _btn_label_for_status(s), inputs=[status_state], outputs=[submit_btn])
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