Shami96 commited on
Commit
172f849
·
verified ·
1 Parent(s): 2377a4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -29
app.py CHANGED
@@ -1,47 +1,75 @@
 
1
  from pathlib import Path
2
  import json
3
  import gradio as gr
4
 
 
5
  from runner import process_video
6
  from config import FRAME_INTERVAL_SEC, MAX_FRAMES
7
 
8
- def run_pipeline(video_file, interval, max_frames):
9
- if video_file is None:
10
- return None, "No file", {}, []
11
- src = Path(video_file)
12
- zip_path = process_video(src, interval_sec=interval, max_frames=max_frames)
13
 
14
- # Build previews
15
- out_root = Path("out") / src.stem
16
- trans_path = out_root / "transcription.txt"
17
- exp_path = out_root / "explanations.json"
18
- frames_dir = out_root / "frames"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- transcript_snippet = (trans_path.read_text(encoding="utf-8")[:1000] + "…") if trans_path.exists() else ""
21
- explanations = json.loads(exp_path.read_text(encoding="utf-8")) if exp_path.exists() else {}
22
- first_items = dict(list(explanations.items())[:5])
23
 
24
- # gallery preview (up to 8)
25
- previews = [str(p) for p in sorted(frames_dir.glob("*.jpg"))[:8]]
26
 
27
- return str(zip_path), transcript_snippet, first_items, previews
 
28
 
29
  with gr.Blocks(title="Video → ZIP Caption Prep") as demo:
30
- gr.Markdown("## Video → ZIP Caption Prep")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- with gr.Row():
33
- video = gr.Video(label="Upload video", sources=["upload"], interactive=True)
34
- with gr.Column():
35
- interval = gr.Number(value=FRAME_INTERVAL_SEC, label="Frame interval (sec)", precision=1)
36
- max_frames = gr.Number(value=MAX_FRAMES, label="Max frames", precision=0)
37
- run_btn = gr.Button("Process", variant="primary")
38
 
39
- zip_out = gr.File(label="Download ZIP")
40
- transcript_box = gr.Textbox(label="Transcription snippet", lines=8)
41
- json_preview = gr.JSON(label="First 5 frame captions")
42
- gallery = gr.Gallery(label="Frame preview", columns=4, height=300)
 
43
 
44
- run_btn.click(run_pipeline, inputs=[video, interval, max_frames], outputs=[zip_out, transcript_box, json_preview, gallery])
45
 
46
  if __name__ == "__main__":
47
- demo.launch()
 
1
+ # app.py (updated)
2
  from pathlib import Path
3
  import json
4
  import gradio as gr
5
 
6
+
7
  from runner import process_video
8
  from config import FRAME_INTERVAL_SEC, MAX_FRAMES
9
 
 
 
 
 
 
10
 
11
+ def run_pipeline(video_file, interval, max_frames, movie_title, movie_year):
12
+ if video_file is None:
13
+ return None, "No file", {}, []
14
+ src = Path(video_file)
15
+ zip_path = process_video(
16
+ src,
17
+ interval_sec=interval,
18
+ max_frames=max_frames,
19
+ movie_title=(movie_title or "").strip(),
20
+ movie_year=(movie_year or "").strip(),
21
+ )
22
+
23
+
24
+ # Build previews
25
+ out_root = Path("out") / src.stem
26
+ trans_path = out_root / "transcription.txt"
27
+ exp_path = out_root / "explanations.json"
28
+ frames_dir = out_root / "frames"
29
+
30
+
31
+ transcript_snippet = (trans_path.read_text(encoding="utf-8")[:1000] + "…") if trans_path.exists() else ""
32
+ explanations = json.loads(exp_path.read_text(encoding="utf-8")) if exp_path.exists() else {}
33
+ # Show up to first 5 items for preview
34
+ try:
35
+ first_items = dict(list(explanations.items())[:5])
36
+ except Exception:
37
+ first_items = explanations
38
+
39
 
40
+ # gallery preview (up to 8)
41
+ previews = [str(p) for p in sorted(frames_dir.glob("*.jpg"))[:8]]
 
42
 
 
 
43
 
44
+ return str(zip_path), transcript_snippet, first_items, previews
45
+
46
 
47
  with gr.Blocks(title="Video → ZIP Caption Prep") as demo:
48
+ gr.Markdown("## Video → ZIP Caption Prep")
49
+
50
+
51
+ with gr.Row():
52
+ video = gr.Video(label="Upload video", sources=["upload"], interactive=True)
53
+ with gr.Column():
54
+ interval = gr.Number(value=FRAME_INTERVAL_SEC, label="Frame interval (sec)", precision=1)
55
+ max_frames = gr.Number(value=MAX_FRAMES, label="Max frames", precision=0)
56
+ movie_title = gr.Textbox(label="Movie title (optional)", placeholder="Peacemaker")
57
+ movie_year = gr.Textbox(label="Year (optional)", placeholder="2022")
58
+ run_btn = gr.Button("Process", variant="primary")
59
+
60
+
61
+ zip_out = gr.File(label="Download ZIP")
62
+ transcript_box = gr.Textbox(label="Transcription snippet", lines=8)
63
+ json_preview = gr.JSON(label="First 5 frame captions")
64
+ gallery = gr.Gallery(label="Frame preview", columns=4, height=300)
65
 
 
 
 
 
 
 
66
 
67
+ run_btn.click(
68
+ run_pipeline,
69
+ inputs=[video, interval, max_frames, movie_title, movie_year],
70
+ outputs=[zip_out, transcript_box, json_preview, gallery],
71
+ )
72
 
 
73
 
74
  if __name__ == "__main__":
75
+ demo.launch()