worldarchived commited on
Commit
7a4136f
·
verified ·
1 Parent(s): 0e7d3e3

Fix video preview: copy hf downloads to /tmp for Gradio serving

Browse files
Files changed (1) hide show
  1. app.py +27 -3
app.py CHANGED
@@ -2,11 +2,14 @@
2
  from __future__ import annotations
3
 
4
  import json
 
 
5
  from functools import lru_cache
 
6
 
7
  import gradio as gr
8
  import pandas as pd
9
- from huggingface_hub import hf_hub_download
10
 
11
  REPO = "WorldArchive/mono-india-workplace-sample"
12
  LEROBOT_REPO = "WorldArchive/mono-india-workplace-lerobot"
@@ -33,8 +36,22 @@ def load_clips_df() -> pd.DataFrame:
33
 
34
 
35
  def clip_video_path(clip_id: str, layer: str) -> str:
 
 
 
 
 
36
  rel = f"clips_preview/{clip_id}/{layer}.mp4"
37
- return hf_hub_download(REPO, rel, repo_type="dataset")
 
 
 
 
 
 
 
 
 
38
 
39
 
40
  def clip_metadata(clip_id: str) -> str:
@@ -64,7 +81,14 @@ def clip_segments(clip_id: str) -> str:
64
 
65
  def update(clip_label: str, layer: str):
66
  clip_id = next(c for c, label in CLIPS if label == clip_label)
67
- return clip_video_path(clip_id, layer), clip_metadata(clip_id), clip_segments(clip_id)
 
 
 
 
 
 
 
68
 
69
 
70
  with gr.Blocks(title="World Archive Data Explorer", theme=gr.themes.Soft()) as demo:
 
2
  from __future__ import annotations
3
 
4
  import json
5
+ import shutil
6
+ import tempfile
7
  from functools import lru_cache
8
+ from pathlib import Path
9
 
10
  import gradio as gr
11
  import pandas as pd
12
+ from huggingface_hub import hf_hub_download, hf_hub_url
13
 
14
  REPO = "WorldArchive/mono-india-workplace-sample"
15
  LEROBOT_REPO = "WorldArchive/mono-india-workplace-lerobot"
 
36
 
37
 
38
  def clip_video_path(clip_id: str, layer: str) -> str:
39
+ """Return a path Gradio can serve on HF Spaces.
40
+
41
+ hf_hub_download cache paths are often outside the Space static dir and
42
+ show as 'Error' in gr.Video. Copy into a temp file under /tmp instead.
43
+ """
44
  rel = f"clips_preview/{clip_id}/{layer}.mp4"
45
+ try:
46
+ src = hf_hub_download(REPO, rel, repo_type="dataset")
47
+ dest = Path(tempfile.gettempdir()) / "wa_explorer" / clip_id / f"{layer}.mp4"
48
+ dest.parent.mkdir(parents=True, exist_ok=True)
49
+ if not dest.exists() or dest.stat().st_mtime < Path(src).stat().st_mtime:
50
+ shutil.copy2(src, dest)
51
+ return str(dest)
52
+ except Exception:
53
+ # Fallback: direct CDN URL (works when copy fails)
54
+ return hf_hub_url(REPO, rel, repo_type="dataset")
55
 
56
 
57
  def clip_metadata(clip_id: str) -> str:
 
81
 
82
  def update(clip_label: str, layer: str):
83
  clip_id = next(c for c, label in CLIPS if label == clip_label)
84
+ try:
85
+ video = clip_video_path(clip_id, layer)
86
+ meta = clip_metadata(clip_id)
87
+ segs = clip_segments(clip_id)
88
+ return video, meta, segs
89
+ except Exception as e:
90
+ err = json.dumps({"clip_id": clip_id, "layer": layer, "error": str(e)}, indent=2)
91
+ return None, err, f"_Preview failed: {e}_"
92
 
93
 
94
  with gr.Blocks(title="World Archive Data Explorer", theme=gr.themes.Soft()) as demo: