"""Flow4RT web demo (Gradio): upload a video and explore the result in the interactive 4D viewer. pip install -e .[app] python app.py # http://localhost:7860; --share for a public link Also runs as a Hugging Face Space (ZeroGPU if the `spaces` package is present). """ import argparse import os import re import shutil import tempfile import threading import time import uuid import gradio as gr import torch from flow4rt.inference import frames_to_tensor, load_model, predict, read_frames from visualize import write_empty_viewer, write_viewer try: import spaces # Hugging Face ZeroGPU gpu = spaces.GPU(duration=120) except ImportError: gpu = lambda fn: fn ROOT = os.path.dirname(os.path.abspath(__file__)) WEIGHTS = os.path.join(ROOT, "checkpoints", "flow4rt.safetensors") OUTPUTS = os.path.join(tempfile.gettempdir(), "flow4rt_app") # The clips of the project page, in its order (sources in examples/CREDITS.md). EXAMPLES = ["bmx_bowl", "parkour", "soapbox", "forest_car", "toy_car", "rollerblade", "swing", "kite_surf", "horsejump_high", "car_turn"] EXAMPLE_DIR = os.path.join(ROOT, "examples") MAX_FRAMES = 64 KEEP_HOURS = 6 # results older than this are deleted # The viewer's design tokens (viewer/template.html), applied to the Gradio page. FONT = ["-apple-system", "BlinkMacSystemFont", "Segoe UI", "Roboto", "Helvetica", "Arial", "sans-serif"] ACCENT, ACCENT_INK, ACCENT_SOFT = "#1a73e8", "#0f57b8", "#e8f0fe" TEXT, INK, SOFT, LINE, LINE_STRONG, TRACK = "#1d232c", "#14181f", "#586272", "#e2e7ee", "#cfd7e2", "#e9edf3" CHIP = "#f1f4f8" # the project page's resource pills SHADOW = "0 1px 2px rgba(16,24,40,.05), 0 6px 20px rgba(16,24,40,.06)" THEME = gr.themes.Base(font=FONT, font_mono=["ui-monospace", "SFMono-Regular", "Menlo", "monospace"]).set( body_background_fill="#fff", body_text_color=TEXT, body_text_color_subdued=SOFT, background_fill_primary="#fff", background_fill_secondary="#fff", border_color_primary=LINE, block_background_fill="#fff", block_border_color=LINE, block_border_width="1px", block_radius="14px", block_shadow=SHADOW, block_padding="14px", block_label_background_fill="#fff", block_label_text_color=SOFT, block_label_border_color=LINE, block_title_text_color=SOFT, block_title_text_weight="500", block_label_text_weight="500", layout_gap="16px", panel_background_fill="#fff", panel_border_width="0px", input_background_fill="#fff", input_border_color=LINE_STRONG, input_border_color_focus=ACCENT, input_radius="10px", input_shadow_focus="0 0 0 3px rgba(26,115,232,.28)", slider_color=ACCENT, color_accent=ACCENT, color_accent_soft=ACCENT_SOFT, loader_color=ACCENT, button_large_radius="999px", button_medium_radius="999px", button_small_radius="999px", button_large_text_weight="600", button_primary_background_fill=ACCENT, button_primary_background_fill_hover=ACCENT_INK, button_primary_border_color=ACCENT, button_primary_text_color="#fff", button_primary_shadow="0 1px 2px rgba(15,87,184,.35), 0 2px 8px rgba(26,115,232,.28)", button_secondary_background_fill="#fff", button_secondary_background_fill_hover=ACCENT_SOFT, button_secondary_border_color=LINE_STRONG, button_secondary_text_color=INK, button_secondary_text_color_hover=ACCENT_INK, ) CSS = f""" .gradio-container {{ width: 100% !important; max-width: 1480px !important; margin: 0 auto !important; padding: 20px 16px 8px !important; }} footer {{ display: none !important; }} #header {{ display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px 24px; padding: 4px 2px 6px; }} #header h1 {{ margin: 0; font-size: 1.6rem; line-height: 1.2; font-weight: 700; color: {INK}; letter-spacing: -.01em; }} #header h1 i {{ font-style: normal; color: {ACCENT}; }} #header h1 span {{ color: {SOFT}; font-weight: 500; }} #header .links {{ display: flex; gap: 8px; }} /* The links as the project page's resource pills: grey, no outline, blue on hover. */ #header .links a {{ display: inline-flex; align-items: center; gap: 5px; height: 30px; padding: 0 12px 0 9px; border-radius: 999px; border: 1px solid transparent; background: {CHIP}; color: {TEXT}; font-size: .85rem; font-weight: 500; text-decoration: none; transition: background .16s, border-color .16s, color .16s; }} #header .links a:hover {{ background: {ACCENT_SOFT}; color: {ACCENT_INK}; border-color: transparent; }} #header .links svg {{ width: 13px; height: 13px; flex: none; }} /* One card for the settings, examples and the Run button; the blocks inside it are flat. */ #settings {{ border: 1px solid {LINE}; border-radius: 14px; box-shadow: {SHADOW}; background: #fff; padding: 14px 16px 16px; gap: 14px; }} #settings .block, #settings .form {{ border: 0 !important; box-shadow: none !important; padding: 0 !important; background: none !important; overflow: visible !important; }} #settings .form {{ gap: 14px !important; flex: none !important; }} /* Examples: a vertical list of clips left of the video, played while hovered. */ #examples {{ border: 1px solid {LINE}; border-radius: 14px; box-shadow: {SHADOW}; background: #fff; padding: 12px; gap: 8px; }} #examples .html-container, #examples .prose, #examples .block {{ padding: 0 !important; border: 0 !important; box-shadow: none !important; background: none !important; }} .card-title {{ font-size: .85rem; font-weight: 500; color: {SOFT}; margin: 0 2px; line-height: 1.2; }} .ex-list {{ display: grid; grid-template-columns: 1fr 1fr; align-content: start; gap: 8px; height: 330px; margin-top: 8px; overflow-y: auto; padding-right: 2px; }} #examples .ex {{ position: relative; display: block; width: 100%; height: 0; padding: 0 0 56.25%; border-radius: 10px; overflow: hidden; border: 1px solid {LINE}; background: {TRACK}; box-shadow: none; cursor: pointer; transition: border-color .16s, box-shadow .16s; }} #examples .ex:hover {{ border-color: {ACCENT}; }} #examples .ex.on {{ border-color: {ACCENT}; box-shadow: 0 0 0 2px {ACCENT}; }} #examples .ex video {{ position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }} #example-pick {{ display: none !important; }} #run {{ margin-top: auto; flex: none; height: 44px; min-height: 44px; }} /* The viewer brings its own 16px padding and canvas card: align its canvas with the cards above. */ #viewer, #viewer .html-container, #viewer .prose {{ padding: 0 !important; margin: 0 !important; overflow: visible !important; max-width: none !important; }} #viewer iframe {{ display: block; width: calc(100% + 32px); margin: 0 -16px; height: min(820px, 90vh); border: 0; }} """ # Narrow windows, in
: Gradio rewrites the selectors inside the @media rules of `css`. HEAD = """""" # The viewer is light-only: keep the page light as well. JS = """() => { const u = new URL(location.href); if (u.searchParams.get('__theme') !== 'light') { u.searchParams.set('__theme', 'light'); location.replace(u.href); return; } // Example clips: play while hovered; a click passes the clip's name to the hidden #example-pick textbox. document.addEventListener('mouseover', e => { const v = e.target.closest('.ex')?.querySelector('video'); if (v && v.paused) v.play().catch(() => {}); }); document.addEventListener('mouseout', e => { const b = e.target.closest('.ex'); if (b && !b.contains(e.relatedTarget)) { const v = b.querySelector('video'); v.pause(); v.currentTime = 0; } }); document.addEventListener('click', e => { const b = e.target.closest('.ex'); if (!b) return; document.querySelectorAll('.ex').forEach(x => x.classList.toggle('on', x === b)); const box = document.querySelector('#example-pick textarea, #example-pick input'); box.value = b.dataset.clip; box.dispatchEvent(new Event('input', { bubbles: true })); }); // In a narrow window the viewer is as tall as its content: size its iframe to it. document.addEventListener('load', e => { const f = e.target; if (f.tagName !== 'IFRAME' || !f.closest('#viewer')) return; const fit = () => { if (innerWidth <= 900) f.style.height = f.contentDocument.documentElement.scrollHeight + 'px'; else f.style.height = ''; }; new ResizeObserver(fit).observe(f.contentDocument.body); addEventListener('resize', fit); fit(); }, true); }""" def icon(paths: str) -> str: """A line icon in the text colour, as the project page's resource pills (globe, brackets, cube).""" return ('') HEADER = f"""