Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -589,50 +589,40 @@ def create_demo():
|
|
| 589 |
pass
|
| 590 |
return _convert_video_for_preview(path)
|
| 591 |
|
| 592 |
-
# Worker now returns (status_state, output_md, preview_path_state)
|
| 593 |
-
def worker(url: str, prompt: str, key: str, progress=gr.Progress()):
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
|
| 603 |
-
|
| 604 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 605 |
|
| 606 |
-
if is_vid:
|
| 607 |
-
progress(0.08, desc="Fetching video bytes (may take a while)...")
|
| 608 |
-
# Ensure video bytes fetching has proper error handling
|
| 609 |
-
raw = fetch_bytes(url, timeout=120, progress=progress)
|
| 610 |
-
if raw is None:
|
| 611 |
-
return ("error", "Failed to fetch video bytes.", "")
|
| 612 |
-
|
| 613 |
-
tmp = save_bytes_to_temp(raw, suffix=ext_from_src(url) or ".mp4")
|
| 614 |
-
progress(0.18, desc="Saved video to temp; converting for preview if needed...")
|
| 615 |
-
preview_tmp = _convert_video_for_preview(tmp)
|
| 616 |
-
preview_local = preview_tmp if os.path.exists(preview_tmp) else tmp
|
| 617 |
-
|
| 618 |
-
progress(0.25, desc="Starting video analysis...")
|
| 619 |
-
res = analyze_video_cohesive(client, tmp, prompt or "", progress=progress)
|
| 620 |
-
progress(0.90, desc="Finalizing results...")
|
| 621 |
-
|
| 622 |
-
except Exception as e:
|
| 623 |
-
return ("error", f"Unexpected worker error: {e}", "")
|
| 624 |
-
|
| 625 |
-
try:
|
| 626 |
-
if preview_tmp != tmp and os.path.exists(preview_tmp):
|
| 627 |
-
pass
|
| 628 |
-
finally:
|
| 629 |
-
try: os.remove(tmp)
|
| 630 |
-
except Exception: pass
|
| 631 |
-
status = "done" if not (isinstance(res, str) and res.lower().startswith("error")) else "error"
|
| 632 |
-
return (status, res if isinstance(res, str) else str(res), preview_local or "")
|
| 633 |
elif is_img:
|
| 634 |
progress(0.08, desc="Fetching image bytes...")
|
| 635 |
raw = fetch_bytes(url, progress=progress)
|
|
|
|
| 636 |
try:
|
| 637 |
preview_fd, preview_path = tempfile.mkstemp(suffix=".jpg")
|
| 638 |
os.close(preview_fd)
|
|
@@ -641,23 +631,25 @@ except Exception as e:
|
|
| 641 |
preview_local = preview_path
|
| 642 |
except Exception:
|
| 643 |
preview_local = None
|
|
|
|
| 644 |
progress(0.18, desc="Analyzing image...")
|
| 645 |
try:
|
| 646 |
res = analyze_image_structured(client, raw, prompt or "", progress=progress)
|
| 647 |
except UnidentifiedImageError:
|
| 648 |
return ("error", "Error: provided file is not a valid image.", preview_local or "")
|
|
|
|
| 649 |
progress(0.98, desc="Finalizing result...")
|
| 650 |
-
|
| 651 |
-
return (status, res if isinstance(res, str) else str(res), preview_local or "")
|
| 652 |
else:
|
| 653 |
progress(0.07, desc="Unknown media type — fetching bytes for heuristics...")
|
| 654 |
raw = fetch_bytes(url, timeout=120, progress=progress)
|
|
|
|
| 655 |
try:
|
| 656 |
progress(0.15, desc="Attempting to interpret as image...")
|
| 657 |
Image.open(BytesIO(raw))
|
| 658 |
-
progress(0.
|
| 659 |
res = analyze_image_structured(client, raw, prompt or "", progress=progress)
|
| 660 |
-
|
| 661 |
try:
|
| 662 |
preview_fd, preview_path = tempfile.mkstemp(suffix=".jpg")
|
| 663 |
os.close(preview_fd)
|
|
@@ -666,29 +658,35 @@ except Exception as e:
|
|
| 666 |
preview_local = preview_path
|
| 667 |
except Exception:
|
| 668 |
preview_local = None
|
| 669 |
-
|
| 670 |
except Exception:
|
| 671 |
fd, tmp = tempfile.mkstemp(suffix=ext_from_src(url) or ".mp4")
|
| 672 |
os.close(fd)
|
| 673 |
with open(tmp, "wb") as fh:
|
| 674 |
fh.write(raw)
|
|
|
|
| 675 |
try:
|
| 676 |
-
progress(0.
|
| 677 |
preview_tmp = _convert_video_for_preview(tmp)
|
| 678 |
preview_local = preview_tmp if os.path.exists(preview_tmp) else tmp
|
| 679 |
res = analyze_video_cohesive(client, tmp, prompt or "", progress=progress)
|
| 680 |
-
status = "done" if not (isinstance(res, str) and res.lower().startswith("error")) else "error"
|
| 681 |
-
return (status, res if isinstance(res, str) else str(res), preview_local or "")
|
| 682 |
finally:
|
| 683 |
-
try:
|
| 684 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 685 |
except Exception as e:
|
| 686 |
return ("error", f"Unexpected worker error: {e}", "")
|
| 687 |
-
|
| 688 |
-
|
|
|
|
| 689 |
submit_btn.click(fn=lambda: "busy", inputs=[], outputs=[status_state])
|
| 690 |
-
|
| 691 |
-
# Actual heavy work runs in the queue and shows progress
|
| 692 |
submit_btn.click(
|
| 693 |
fn=worker,
|
| 694 |
inputs=[url_input, custom_prompt, api_key],
|
|
@@ -697,8 +695,7 @@ except Exception as e:
|
|
| 697 |
show_progress="full",
|
| 698 |
show_progress_on=progress_md,
|
| 699 |
)
|
| 700 |
-
|
| 701 |
-
# update submit button label from status
|
| 702 |
def _btn_label_for_status(s):
|
| 703 |
labels = {
|
| 704 |
"idle": "Submit",
|
|
@@ -707,8 +704,7 @@ except Exception as e:
|
|
| 707 |
"error": "Retry",
|
| 708 |
}
|
| 709 |
return labels.get(s, "Submit") # Default case
|
| 710 |
-
|
| 711 |
-
|
| 712 |
status_state.change(fn=lambda s: _btn_label_for_status(s), inputs=[status_state], outputs=[submit_btn])
|
| 713 |
|
| 714 |
# map status to progress text
|
|
|
|
| 589 |
pass
|
| 590 |
return _convert_video_for_preview(path)
|
| 591 |
|
| 592 |
+
# Worker now returns (status_state, output_md, preview_path_state)
|
| 593 |
+
def worker(url: str, prompt: str, key: str, progress=gr.Progress()):
|
| 594 |
+
try:
|
| 595 |
+
if not url:
|
| 596 |
+
return ("error", "**Error:** No URL provided.", "")
|
| 597 |
|
| 598 |
+
progress(0.01, desc="Starting processing...") # Initial processing
|
| 599 |
+
progress(0.03, desc="Checking URL / content-type...") # URL check
|
| 600 |
+
is_img, is_vid = determine_media_type(url, progress=progress)
|
| 601 |
+
progress(0.06, desc=f"Determined media type: image={is_img}, video={is_vid}")
|
| 602 |
|
| 603 |
+
client = get_client(key)
|
| 604 |
+
preview_local = None
|
| 605 |
+
|
| 606 |
+
if is_vid:
|
| 607 |
+
progress(0.08, desc="Fetching video bytes (may take a while)...")
|
| 608 |
+
# Ensure video bytes fetching has proper error handling
|
| 609 |
+
raw = fetch_bytes(url, timeout=120, progress=progress)
|
| 610 |
+
if raw is None:
|
| 611 |
+
return ("error", "Failed to fetch video bytes.", "")
|
| 612 |
+
|
| 613 |
+
tmp = save_bytes_to_temp(raw, suffix=ext_from_src(url) or ".mp4")
|
| 614 |
+
progress(0.18, desc="Saved video to temp; converting for preview if needed...")
|
| 615 |
+
preview_tmp = _convert_video_for_preview(tmp)
|
| 616 |
+
preview_local = preview_tmp if os.path.exists(preview_tmp) else tmp
|
| 617 |
+
|
| 618 |
+
progress(0.25, desc="Starting video analysis...")
|
| 619 |
+
res = analyze_video_cohesive(client, tmp, prompt or "", progress=progress)
|
| 620 |
+
progress(0.90, desc="Finalizing results...")
|
| 621 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
elif is_img:
|
| 623 |
progress(0.08, desc="Fetching image bytes...")
|
| 624 |
raw = fetch_bytes(url, progress=progress)
|
| 625 |
+
|
| 626 |
try:
|
| 627 |
preview_fd, preview_path = tempfile.mkstemp(suffix=".jpg")
|
| 628 |
os.close(preview_fd)
|
|
|
|
| 631 |
preview_local = preview_path
|
| 632 |
except Exception:
|
| 633 |
preview_local = None
|
| 634 |
+
|
| 635 |
progress(0.18, desc="Analyzing image...")
|
| 636 |
try:
|
| 637 |
res = analyze_image_structured(client, raw, prompt or "", progress=progress)
|
| 638 |
except UnidentifiedImageError:
|
| 639 |
return ("error", "Error: provided file is not a valid image.", preview_local or "")
|
| 640 |
+
|
| 641 |
progress(0.98, desc="Finalizing result...")
|
| 642 |
+
|
|
|
|
| 643 |
else:
|
| 644 |
progress(0.07, desc="Unknown media type — fetching bytes for heuristics...")
|
| 645 |
raw = fetch_bytes(url, timeout=120, progress=progress)
|
| 646 |
+
|
| 647 |
try:
|
| 648 |
progress(0.15, desc="Attempting to interpret as image...")
|
| 649 |
Image.open(BytesIO(raw))
|
| 650 |
+
progress(0.20, desc="Image detected — analyzing...")
|
| 651 |
res = analyze_image_structured(client, raw, prompt or "", progress=progress)
|
| 652 |
+
|
| 653 |
try:
|
| 654 |
preview_fd, preview_path = tempfile.mkstemp(suffix=".jpg")
|
| 655 |
os.close(preview_fd)
|
|
|
|
| 658 |
preview_local = preview_path
|
| 659 |
except Exception:
|
| 660 |
preview_local = None
|
| 661 |
+
|
| 662 |
except Exception:
|
| 663 |
fd, tmp = tempfile.mkstemp(suffix=ext_from_src(url) or ".mp4")
|
| 664 |
os.close(fd)
|
| 665 |
with open(tmp, "wb") as fh:
|
| 666 |
fh.write(raw)
|
| 667 |
+
|
| 668 |
try:
|
| 669 |
+
progress(0.30, desc="Saved fallback video file; analyzing...")
|
| 670 |
preview_tmp = _convert_video_for_preview(tmp)
|
| 671 |
preview_local = preview_tmp if os.path.exists(preview_tmp) else tmp
|
| 672 |
res = analyze_video_cohesive(client, tmp, prompt or "", progress=progress)
|
|
|
|
|
|
|
| 673 |
finally:
|
| 674 |
+
try:
|
| 675 |
+
os.remove(tmp)
|
| 676 |
+
except Exception:
|
| 677 |
+
pass
|
| 678 |
+
|
| 679 |
+
# Determine final status and return
|
| 680 |
+
status = "done" if not (isinstance(res, str) and res.lower().startswith("error")) else "error"
|
| 681 |
+
return (status, res if isinstance(res, str) else str(res), preview_local or "")
|
| 682 |
+
|
| 683 |
except Exception as e:
|
| 684 |
return ("error", f"Unexpected worker error: {e}", "")
|
| 685 |
+
|
| 686 |
+
|
| 687 |
+
# UI wiring
|
| 688 |
submit_btn.click(fn=lambda: "busy", inputs=[], outputs=[status_state])
|
| 689 |
+
|
|
|
|
| 690 |
submit_btn.click(
|
| 691 |
fn=worker,
|
| 692 |
inputs=[url_input, custom_prompt, api_key],
|
|
|
|
| 695 |
show_progress="full",
|
| 696 |
show_progress_on=progress_md,
|
| 697 |
)
|
| 698 |
+
|
|
|
|
| 699 |
def _btn_label_for_status(s):
|
| 700 |
labels = {
|
| 701 |
"idle": "Submit",
|
|
|
|
| 704 |
"error": "Retry",
|
| 705 |
}
|
| 706 |
return labels.get(s, "Submit") # Default case
|
| 707 |
+
|
|
|
|
| 708 |
status_state.change(fn=lambda s: _btn_label_for_status(s), inputs=[status_state], outputs=[submit_btn])
|
| 709 |
|
| 710 |
# map status to progress text
|