Spaces:
Runtime error
Runtime error
Commit ·
bb0f4d9
1
Parent(s): 82a1045
Replace bottom Examples + top stats card with one demo button
Browse files- Remove the 'Run summary' tile card at the top of the output
column; the four output panels (vis video, charts, canvas,
raw JSON accordion) are enough.
- Remove the bottom 'Demo video' card with three example rows;
sat awkwardly below the layout.
- Add a single 'Load demo video' button right under the upload
widget in the Input card. One click fills every control with
a curated preset (sbs · combined · log · GOP=dynamic · 1024
patches) and loads the bundled demo clip.
- Button is hidden when examples/demo_codec_heatmap.mp4 is
missing so the UI degrades cleanly on stripped checkouts.
- process() is back to its 4-output signature.
39 unmodified lines404142434445406 unmodified lines452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492116 unmodified lines6096106116126136146153 unmodified lines61962062162262362462523 unmodified lines649650651652653654655108 unmodified lines764765766767768769770771772773774775776777778779780781782783784785786787788789790412 unmodified lines120312041205120612071208120912101211121212131214121512161217121812191220122112226 unmodified lines12291230123112321233123486 unmodified lines132113221323132413251326132713281329133032 unmodified lines136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140823 unmodified lines143214331434143514361437143839 unmodified linesPATCH_CHOICES = [14, 16, 28]def smart_resize(frame: np.ndarray, max_pixels: int, factor: int) -> np.ndarray:"""Resize so h,w are multiples of `factor` and h*w <= max_pixels."""406 unmodified linesreturn canvas, ndef render_stats_html(*, total_selected: int, n_groups: int, gop_label: str,n_sampled: int, total_frames: int, canvas_resolution: str,saliency_signal: str, score_log_scale: bool, elapsed_sec: float,grid_label: str, n_uniform: int, codec: str = "",) -> str:"""Format the run's headline numbers as a grid of brand-colored tiles."""sig = saliency_signal + (" · log" if score_log_scale else "")gop_disp = (gop_label if gop_label in ("global", "dynamic") else f"GOP={gop_label}")cells = [("selected patches", f"{total_selected:,}"),("groups", f"{n_groups} · {gop_disp}"),("sampled frames", f"{n_sampled} / {total_frames}"),("canvas", canvas_resolution),("uniform baseline", f"{n_uniform} frame{'s' if n_uniform != 1 else ''}"),("saliency", sig),("patch grid / frame", grid_label),("elapsed", f"{elapsed_sec:.2f}s"),]if codec:cells.insert(2, ("input codec", codec))parts = ['<div class="ovc-stats">']for label, value in cells:parts.append(f'<div class="ovc-stat">'f'<div class="value">{value}</div>'f'<div class="label">{label}</div>'f'</div>')parts.append("</div>")return "".join(parts)def make_charts(grids: List[np.ndarray],masks: List[np.ndarray],116 unmodified linesprogress=gr.Progress(track_tqdm=False),):if not video_path:return None, None, "Please upload a video.", None, EMPTY_STATSt0 = time.time()progress(0.05, desc="Reading metadata")3 unmodified linesreturn None, None, json.dumps({"error": "Could not read frame count.", "metadata": meta},indent=2, ensure_ascii=False,), None, EMPTY_STATSprogress(0.10, desc="Sampling frames")fps = float(meta.get("fps") or 0.0)23 unmodified linesreturn None, None, json.dumps({"error": "Failed to decode frames.", "metadata": meta},indent=2, ensure_ascii=False,), None, EMPTY_STATSprogress(0.25, desc="smart_resize")resized = [smart_resize(f, int(max_pixels), int(patch_size)) for f in raw]108 unmodified linesgroups=groups, gop_label=gop_resolved,)stats_html = render_stats_html(total_selected=int(actual_selected),n_groups=len(groups),gop_label=gop_resolved,n_sampled=len(fids),total_frames=int(total),canvas_resolution=f"{canvas.shape[1]}×{canvas.shape[0]}",saliency_signal=saliency_signal,score_log_scale=bool(score_log_scale),elapsed_sec=round(time.time() - t0, 2),grid_label=f"{hb}×{wb}",n_uniform=int(n_uniform),codec=str(meta.get("codec") or ""),)progress(1.0, desc="Done")return (vis_path, canvas_path,json.dumps(info, indent=2, ensure_ascii=False),chart_fig,stats_html,)412 unmodified lines</div>"""EMPTY_STATS = ('<div class="ovc-stats">''<div class="ovc-stat" style="grid-column: 1 / -1; text-align:center; ''background: linear-gradient(135deg, rgba(79,70,229,0.04), rgba(6,182,212,0.03));">''<div class="value" style="font-size:1.05rem; font-weight:600; ''-webkit-background-clip: initial; background-clip: initial; color: #64748b;">''Hit <span style="background: var(--ovc-grad); -webkit-background-clip:text; ''background-clip:text; color:transparent;">Run pipeline</span>, ''or pick a row in <i>Demo video</i> below.''</div>''<div class="label" style="margin-top:6px;">key numbers will land here</div>''</div>''</div>')with gr.Blocks(**_BLOCK_KW) as demo:6 unmodified lineswith gr.Group(elem_classes="ovc-card"):gr.Markdown("### Input")video_in = gr.Video(label="Video", sources=["upload"], height=240)with gr.Group(elem_classes="ovc-card"):gr.Markdown("### Pipeline")86 unmodified lines# ─── Outputs (wide column) ───────────────────────────────────────with gr.Column(scale=6, min_width=420):with gr.Group(elem_classes="ovc-card ovc-card-primary"):gr.Markdown("### Run summary")stats_out = gr.HTML(value=EMPTY_STATS)with gr.Group(elem_classes="ovc-card ovc-card-primary"):gr.Markdown("### Patch selection visualization")vis_out = gr.Video(32 unmodified lineslabel="", language="json", lines=18,)DEMO_VIDEO = os.path.join(os.path.dirname(os.path.abspath(__file__)),"examples", "demo_codec_heatmap.mp4",)if os.path.exists(DEMO_VIDEO):with gr.Group(elem_classes="ovc-card"):gr.Markdown("### Demo video")gr.Markdown("<small>Click any row below to load the bundled clip with a ""preset configuration. Or upload your own video at the top.</small>")gr.Examples(examples=[[DEMO_VIDEO, 16, 14, 1024, 150000,"selection", 0.55, 0.0, 0.0,"gradient", False, 99.0, 0.55, "global",],[DEMO_VIDEO, 16, 14, 1024, 150000,"heatmap", 0.55, 0.0, 0.0,"combined", True, 96.0, 0.55, "dynamic",],[DEMO_VIDEO, 24, 14, 2048, 150000,"sbs", 0.55, 0.0, 0.0,"combined", True, 95.0, 0.55, "8",],],inputs=[video_in, sample_frames, patch_size, top_k, max_pixels,viz_mode, heatmap_alpha, start_sec, end_sec,saliency_signal, score_log_scale, bitcost_pct,fade_strength, gop,],label="",cache_examples=False,examples_per_page=5,)gr.HTML('<div id="ovc-footer">''<b>OneVision Encoder</b> · codec-style patch saliency demo · '23 unmodified linessaliency_signal, score_log_scale, bitcost_pct, fade_strength,gop,],outputs=[vis_out, canvas_out, info_out, chart_out, stats_out],)39 unmodified lines4041424344454647484950515253545556575859606162636465406 unmodified lines472473474475476477116 unmodified lines5945955965975985996003 unmodified lines60460560660760860961023 unmodified lines634635636637638639640108 unmodified lines749750751752753754755756757758759412 unmodified lines1172117311741175117611776 unmodified lines1184118511861187118811891190119111921193119486 unmodified lines12811282128312841285128632 unmodified lines13191320132113221323132423 unmodified lines13481349135013511352135313541355135613571358135913601361136213631364136539 unmodified linesPATCH_CHOICES = [14, 16, 28]DEMO_VIDEO_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),"examples", "demo_codec_heatmap.mp4",)DEMO_PRESET = (DEMO_VIDEO_PATH, # video_in16, # sample_frames14, # patch_size1024, # total_patches150000, # max_pixels"sbs", # viz_mode0.55, # heatmap_alpha0.0, 0.0, # start_sec, end_sec"combined", # saliency_signalTrue, # score_log_scale96.0, # bitcost_pct0.55, # fade_strength"dynamic", # gop)def smart_resize(frame: np.ndarray, max_pixels: int, factor: int) -> np.ndarray:"""Resize so h,w are multiples of `factor` and h*w <= max_pixels."""406 unmodified linesreturn canvas, ndef make_charts(grids: List[np.ndarray],masks: List[np.ndarray],116 unmodified linesprogress=gr.Progress(track_tqdm=False),):if not video_path:return None, None, "Please upload a video.", Nonet0 = time.time()progress(0.05, desc="Reading metadata")3 unmodified linesreturn None, None, json.dumps({"error": "Could not read frame count.", "metadata": meta},indent=2, ensure_ascii=False,), Noneprogress(0.10, desc="Sampling frames")fps = float(meta.get("fps") or 0.0)23 unmodified linesreturn None, None, json.dumps({"error": "Failed to decode frames.", "metadata": meta},indent=2, ensure_ascii=False,), Noneprogress(0.25, desc="smart_resize")resized = [smart_resize(f, int(max_pixels), int(patch_size)) for f in raw]108 unmodified linesgroups=groups, gop_label=gop_resolved,)progress(1.0, desc="Done")return (vis_path, canvas_path,json.dumps(info, indent=2, ensure_ascii=False),chart_fig,)412 unmodified lines</div>"""with gr.Blocks(**_BLOCK_KW) as demo:6 unmodified lineswith gr.Group(elem_classes="ovc-card"):gr.Markdown("### Input")video_in = gr.Video(label="Video", sources=["upload"], height=240)with gr.Row(elem_classes="ovc-preset"):btn_demo = gr.Button("Load demo video", size="sm",visible=os.path.exists(DEMO_VIDEO_PATH),)with gr.Group(elem_classes="ovc-card"):gr.Markdown("### Pipeline")86 unmodified lines# ─── Outputs (wide column) ───────────────────────────────────────with gr.Column(scale=6, min_width=420):with gr.Group(elem_classes="ovc-card ovc-card-primary"):gr.Markdown("### Patch selection visualization")vis_out = gr.Video(32 unmodified lineslabel="", language="json", lines=18,)gr.HTML('<div id="ovc-footer">''<b>OneVision Encoder</b> · codec-style patch saliency demo · '23 unmodified linessaliency_signal, score_log_scale, bitcost_pct, fade_strength,gop,],outputs=[vis_out, canvas_out, info_out, chart_out],)btn_demo.click(lambda: DEMO_PRESET,inputs=None,outputs=[video_in, sample_frames, patch_size, top_k, max_pixels,viz_mode, heatmap_alpha, start_sec, end_sec,saliency_signal, score_log_scale, bitcost_pct, fade_strength,gop,],)
app.py+40-113