akhaliq HF Staff commited on
Commit
3ba95e4
·
1 Parent(s): 6b9a636

Fix example loading, file upload and ZeroGPU compatibility

Browse files
Files changed (2) hide show
  1. app.py +3 -1
  2. index.html +39 -22
app.py CHANGED
@@ -11,6 +11,7 @@ from fastapi.staticfiles import StaticFiles
11
  import cv2
12
  import numpy as np
13
  import os
 
14
  import tempfile
15
  import re
16
  import time
@@ -354,6 +355,7 @@ SAFETY_MARGIN = 25
354
  EST_SECONDS_PER_FRAME = 20
355
 
356
 
 
357
  def run_image_gpu_api(
358
  image_path: str, category: str, model_mode: str, temp: float, top_p: float, top_k: int,
359
  short_size: int | None, question_override: str | None
@@ -400,6 +402,7 @@ def run_image_gpu_api(
400
  return out_img_path, stats, output_text, detections_summary
401
 
402
 
 
403
  def run_video_gpu_api(
404
  video_path: str, category: str, model_mode: str, temp: float, top_p: float, top_k: int,
405
  short_size: int | None, question_override: str | None, max_video_frames: int
@@ -569,7 +572,6 @@ async def homepage():
569
 
570
 
571
  @app.api(name="run_inference")
572
- @spaces.GPU(duration=240)
573
  def run_inference_api(
574
  input_type: str,
575
  image_file: Any = None,
 
11
  import cv2
12
  import numpy as np
13
  import os
14
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
15
  import tempfile
16
  import re
17
  import time
 
355
  EST_SECONDS_PER_FRAME = 20
356
 
357
 
358
+ @spaces.GPU(duration=120)
359
  def run_image_gpu_api(
360
  image_path: str, category: str, model_mode: str, temp: float, top_p: float, top_k: int,
361
  short_size: int | None, question_override: str | None
 
402
  return out_img_path, stats, output_text, detections_summary
403
 
404
 
405
+ @spaces.GPU(duration=240)
406
  def run_video_gpu_api(
407
  video_path: str, category: str, model_mode: str, temp: float, top_p: float, top_k: int,
408
  short_size: int | None, question_override: str | None, max_video_frames: int
 
572
 
573
 
574
  @app.api(name="run_inference")
 
575
  def run_inference_api(
576
  input_type: str,
577
  image_file: Any = None,
index.html CHANGED
@@ -484,7 +484,7 @@
484
 
485
  <!-- Gradio client connection & app runtime logic -->
486
  <script type="module">
487
- import { client } from "https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.min.js";
488
 
489
  // State variables
490
  let selectedMediaType = "Image";
@@ -696,10 +696,23 @@
696
  }
697
  }
698
 
 
 
 
 
 
 
 
 
 
 
 
 
 
699
  // Initialize preloaded examples click actions
700
  function setupExamples() {
701
  document.querySelectorAll(".example-card").forEach(card => {
702
- card.addEventListener("click", () => {
703
  const type = card.getAttribute("data-type");
704
  const name = card.getAttribute("data-name");
705
  const category = card.getAttribute("data-category");
@@ -721,22 +734,27 @@
721
  // Setup Media type
722
  setMediaType(type);
723
 
724
- // Set active file as local path string
725
- activeFile = assetPath;
726
-
727
- // Display Preview Image/Video using absolute URL
728
- const resolvedUrl = window.location.origin + "/" + assetPath;
729
- uploadPrompt.classList.add("hidden");
730
- if (type === "Image") {
731
- previewImage.src = resolvedUrl;
732
- previewImage.classList.remove("hidden");
733
- previewVideo.classList.add("hidden");
734
- workspaceStatus.textContent = `Example Image Loaded: ${name}`;
 
 
 
 
 
 
 
 
735
  } else {
736
- previewVideo.src = resolvedUrl;
737
- previewVideo.classList.remove("hidden");
738
- previewImage.classList.add("hidden");
739
- workspaceStatus.textContent = `Example Video Loaded: ${name}`;
740
  }
741
  });
742
  });
@@ -773,11 +791,10 @@
773
  throw new Error("Unable to create Gradio Client instance.");
774
  }
775
 
776
- // Handle file parameter packing
777
- // If activeFile is a string (local example), pass it directly as a string.
778
- // Otherwise, it's a browser File object, pass it directly.
779
- const imageFile = (selectedMediaType === "Image") ? activeFile : null;
780
- const videoFile = (selectedMediaType === "Video") ? activeFile : null;
781
 
782
  // Collect configuration values
783
  const taskType = taskTypeSelect.value;
 
484
 
485
  <!-- Gradio client connection & app runtime logic -->
486
  <script type="module">
487
+ import { client, handle_file } from "https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.min.js";
488
 
489
  // State variables
490
  let selectedMediaType = "Image";
 
696
  }
697
  }
698
 
699
+ // Initialize preloaded examples click actions
700
+ // Utility to fetch preloaded example assets and convert to File
701
+ async function loadExampleFromAsset(url, filename) {
702
+ try {
703
+ const response = await fetch(url);
704
+ const blob = await response.blob();
705
+ return new File([blob], filename, { type: blob.type });
706
+ } catch (err) {
707
+ console.error("Failed to load example asset:", err);
708
+ return null;
709
+ }
710
+ }
711
+
712
  // Initialize preloaded examples click actions
713
  function setupExamples() {
714
  document.querySelectorAll(".example-card").forEach(card => {
715
+ card.addEventListener("click", async () => {
716
  const type = card.getAttribute("data-type");
717
  const name = card.getAttribute("data-name");
718
  const category = card.getAttribute("data-category");
 
734
  // Setup Media type
735
  setMediaType(type);
736
 
737
+ // Fetch asset file with robust absolute URL resolution (works in iframe)
738
+ const ext = type === "Image" ? "jpg" : "mp4";
739
+ const resolvedAssetUrl = new URL(assetPath, window.location.href).href;
740
+ console.log("Fetching example from:", resolvedAssetUrl);
741
+ const file = await loadExampleFromAsset(resolvedAssetUrl, `${name.toLowerCase()}.${ext}`);
742
+ if (file) {
743
+ activeFile = file;
744
+ uploadPrompt.classList.add("hidden");
745
+ if (type === "Image") {
746
+ previewImage.src = URL.createObjectURL(file);
747
+ previewImage.classList.remove("hidden");
748
+ previewVideo.classList.add("hidden");
749
+ workspaceStatus.textContent = `Example Image Loaded: ${name}`;
750
+ } else {
751
+ previewVideo.src = URL.createObjectURL(file);
752
+ previewVideo.classList.remove("hidden");
753
+ previewImage.classList.add("hidden");
754
+ workspaceStatus.textContent = `Example Video Loaded: ${name}`;
755
+ }
756
  } else {
757
+ workspaceStatus.textContent = `Failed to load ${name} example`;
 
 
 
758
  }
759
  });
760
  });
 
791
  throw new Error("Unable to create Gradio Client instance.");
792
  }
793
 
794
+ // Handle file parameter wrapping using Gradio client handle_file
795
+ const wrappedFile = activeFile ? handle_file(activeFile) : null;
796
+ const imageFile = (selectedMediaType === "Image") ? wrappedFile : null;
797
+ const videoFile = (selectedMediaType === "Video") ? wrappedFile : null;
 
798
 
799
  // Collect configuration values
800
  const taskType = taskTypeSelect.value;