YT_Video / app.py
Shami96's picture
Update app.py
51fc284 verified
from pathlib import Path
import json
import gradio as gr
from runner import process_video
from config import FRAME_INTERVAL_SEC, MAX_FRAMES
def run_pipeline(video_file, interval, max_frames, movie_title, movie_year):
if video_file is None:
return None, "No file", {}, []
src = Path(video_file)
zip_path = process_video(
src,
interval_sec=interval,
max_frames=max_frames,
movie_title=(movie_title or "").strip(),
movie_year=(movie_year or "").strip(),
)
# Build previews
out_root = Path("out") / src.stem
trans_path = out_root / "transcription.txt"
exp_path = out_root / "explanations.json"
frames_dir = out_root / "frames"
transcript_snippet = (trans_path.read_text(encoding="utf-8")[:1000] + "…") if trans_path.exists() else ""
explanations = json.loads(exp_path.read_text(encoding="utf-8")) if exp_path.exists() else {}
try:
first_items = dict(list(explanations.items())[:5])
except Exception:
first_items = explanations
previews = [str(p) for p in sorted(frames_dir.glob("*.jpg"))[:8]]
return str(zip_path), transcript_snippet, first_items, previews
with gr.Blocks(title="Video → ZIP Caption Prep") as demo:
gr.Markdown("## Video → ZIP Caption Prep")
with gr.Row():
video = gr.Video(label="Upload video", sources=["upload"], interactive=True)
with gr.Column():
interval = gr.Number(value=FRAME_INTERVAL_SEC, label="Frame interval (sec)", precision=1)
max_frames = gr.Number(value=MAX_FRAMES, label="Max frames", precision=0)
movie_title = gr.Textbox(label="Movie title (optional)", placeholder="Peacemaker")
movie_year = gr.Textbox(label="Year (optional)", placeholder="2022")
run_btn = gr.Button("Process", variant="primary")
zip_out = gr.File(label="Download ZIP")
transcript_box = gr.Textbox(label="Transcription snippet", lines=8)
json_preview = gr.JSON(label="First 5 frame captions")
gallery = gr.Gallery(label="Frame preview", columns=4, height=300)
run_btn.click(
run_pipeline,
inputs=[video, interval, max_frames, movie_title, movie_year],
outputs=[zip_out, transcript_box, json_preview, gallery],
)
if __name__ == "__main__":
demo.launch()