Gradio server/workflow (#5)
Browse files- pi-H3 Studio: gr.Server studio + gr.Workflow canvas (Jeveration pattern) (a275e7f8d8a6e03d0144118793fd8a1fb035b1c7)
- Add pi-H3 Studio page (7e8780b27374b26155ed127f4dc123f5e4951c47)
- Add workflow canvas starter graph (b04ec50c6109759ed6079e721a6d339db1d75e48)
- Use fastapi response classes for the studio routes (97408037c91fe2f4ed8e2986884a24905e1eac1a)
- Keep the Blocks demo as the default page, with the studio as a tab (3a3372d83b1d7656c294a19c5a11165486062f68)
Co-authored-by: AK <akhaliq@users.noreply.huggingface.co>
Files changed (3)
- app.py+349-11
- studio.htmlAdded+714
- workflow.jsonAdded+161
1234567894 unmodified lines1415161718196 unmodified lines2627282930313233343511 unmodified lines474849505152169 unmodified lines2222232242252262272281 unmodified line2302312322332342352362374 unmodified lines242243244245246247248249250251252253254255256257258259260import hashlibimport jsonimport mathimport osimport tempfilefrom types import SimpleNamespaceos.environ.setdefault('PYTORCH_CUDA_ALLOC_CONF', 'expandable_segments:True')4 unmodified linestorch.backends.cudnn.allow_tf32 = Trueimport gradio as grfrom PIL import Image, ImageOpsfrom lakonlab.models.architectures import RGBColorEncoderfrom lakonlab.models.architectures.diffusers.attention import set_attention_backends6 unmodified linesfrom lakonlab.pipelines.utils import load_lakonlab_adapterfrom lakonlab.ui.gradio.create_img_to_video import create_interface_img_to_videofrom lakonlab.utils.io_utils import rgb_to_yuv420p, save_video_audioADAPTER_REPO = 'Lakonik/pi-H3'ROOT = os.path.dirname(os.path.abspath(__file__))DEFAULT_PROMPT = ('integrated_multimodal_description: [Shot 1] Live-action, handheld smartphone selfie video, a close '"medium shot with the camera held at arm's length slightly above eye level, a young man in an orange "11 unmodified lines'N/A')def canvas_size(aspect_ratio, num_pixels, multiple=32):"""(height, width) of the aspect ratio at num_pixels, both rounded up to the multiple, so 1 MP gives H3's native768x1344 for 16:9 and 672x1536 for 21:9."""169 unmodified linesoptions={'preset': 'veryfast', 'crf': str(args.crf)}, metadata={'comment': json.dumps(settings)})return output_pathreturn build_interface(generate)def build_interface(generate):1 unmodified linetitle='pi-H3 Demo') as demo:md_txt = '# pi-H3 Demo 1.0\n\n' \'The fastest MiniMax H3 video model: 2 or 4 steps, direct pixel output without a video VAE decoder. ' \'Accelerated by **pi-Flow V2** and **[DWD](https://hanshengchen.com/dwd/)** ' \'(**D**ecoding **W**ithout a **D**ecoder). ' \'Read the [blog post](https://hanshengchen.com/blog/pi-h3/) for more details.\n' \4 unmodified linesgenerate,prompt=DEFAULT_PROMPT,steps=2, min_steps=2, max_steps=4, steps_slider_step=2, remove_decoder=True,aspect_ratios=('21:9', '16:9', '4:3', '1:1', '3:4', '9:16'),examples=json.load(open(os.path.join(ROOT, 'demo/examples/minimax_h3/examples.json'))),args=['last_seed', 'prompt', 'in_image', 'in_last_image', 'aspect_ratio', 'megapixels', 'duration','steps', 'remove_decoder'])return demodef launch_demo(demo):demo.queue().launch(ssr_mode=False,allowed_paths=[os.path.join(ROOT, 'demo/examples/minimax_h3')],css_paths=os.path.join(ROOT, 'lakonlab/ui/gradio/style.css'),head_paths=os.path.join(ROOT, 'lakonlab/ui/gradio/head.html'))if __name__ == "__main__":12345678910111213144 unmodified lines1920212223242526276 unmodified lines3435363738394041424344454611 unmodified lines585960616263646566676869169 unmodified lines2392402412422432442452462472482492502512522532542552561 unmodified line2582592602612622632642654 unmodified lines270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598import base64import hashlibimport jsonimport mathimport osimport randomimport tempfileimport threadingimport timefrom types import SimpleNamespacefrom urllib.parse import unquote, urlparseos.environ.setdefault('PYTORCH_CUDA_ALLOC_CONF', 'expandable_segments:True')4 unmodified linestorch.backends.cudnn.allow_tf32 = Trueimport gradio as grfrom fastapi.responses import FileResponse, HTMLResponse, RedirectResponsefrom gradio.context import LocalContextfrom gradio.utils import get_upload_folderfrom PIL import Image, ImageOpsfrom lakonlab.models.architectures import RGBColorEncoderfrom lakonlab.models.architectures.diffusers.attention import set_attention_backends6 unmodified linesfrom lakonlab.pipelines.utils import load_lakonlab_adapterfrom lakonlab.ui.gradio.create_img_to_video import create_interface_img_to_videofrom lakonlab.utils.io_utils import rgb_to_yuv420p, save_video_audiofrom starlette.requests import Request as StarletteRequestADAPTER_REPO = 'Lakonik/pi-H3'ROOT = os.path.dirname(os.path.abspath(__file__))MAX_SEED = 2**31 - 1ASPECT_RATIOS = ('21:9', '16:9', '4:3', '1:1', '3:4', '9:16')DEFAULT_PROMPT = ('integrated_multimodal_description: [Shot 1] Live-action, handheld smartphone selfie video, a close '"medium shot with the camera held at arm's length slightly above eye level, a young man in an orange "11 unmodified lines'N/A')with open(os.path.join(ROOT, 'demo/examples/minimax_h3/examples.json')) as _examples_handle:EXAMPLES = json.load(_examples_handle)EXAMPLE_FILES = {os.path.basename(example[key]) for example in EXAMPLESfor key in ('in_image', 'in_last_image') if example.get(key)}def canvas_size(aspect_ratio, num_pixels, multiple=32):"""(height, width) of the aspect ratio at num_pixels, both rounded up to the multiple, so 1 MP gives H3's native768x1344 for 16:9 and 672x1536 for 21:9."""169 unmodified linesoptions={'preset': 'veryfast', 'crf': str(args.crf)}, metadata={'comment': json.dumps(settings)})return output_pathreturn build_app(generate)TABS_HTML = ('<style>.html-container:has(.pih3-head){padding:0!important}''.pih3-head{display:flex;align-items:center;gap:20px;flex-wrap:wrap}.pih3-head h1{margin:0!important}''.pih3-tabs{display:flex;gap:4px;padding:4px;border:1px solid var(--border-color-primary);border-radius:10px}''.pih3-tabs a{padding:6px 16px;border-radius:7px;font-weight:600;''text-decoration:none!important;color:var(--body-text-color)!important}''.pih3-tabs a.active{background:var(--body-text-color);color:var(--background-fill-primary)!important}</style>''<div class="pih3-head prose"><h1>pi-H3 Demo 1.0</h1><div class="pih3-tabs"><a class="active">Classic UI</a>''<a href="/studio" onclick="this.href=\'/studio\'+location.search">Studio</a></div></div>')def build_interface(generate):1 unmodified linetitle='pi-H3 Demo') as demo:gr.HTML(TABS_HTML, padding=False)md_txt = 'The fastest MiniMax H3 video model: 2 or 4 steps, direct pixel output without a video VAE decoder. ' \'Accelerated by **pi-Flow V2** and **[DWD](https://hanshengchen.com/dwd/)** ' \'(**D**ecoding **W**ithout a **D**ecoder). ' \'Read the [blog post](https://hanshengchen.com/blog/pi-h3/) for more details.\n' \4 unmodified linesgenerate,prompt=DEFAULT_PROMPT,steps=2, min_steps=2, max_steps=4, steps_slider_step=2, remove_decoder=True,aspect_ratios=ASPECT_RATIOS,examples=EXAMPLES,args=['last_seed', 'prompt', 'in_image', 'in_last_image', 'aspect_ratio', 'megapixels', 'duration','steps', 'remove_decoder'])return demo# --------------------------------------------------------------------------- helpersdef _resolve_seed(seed, randomize) -> int:return random.randint(0, MAX_SEED) if randomize else min(max(int(float(seed)), 0), MAX_SEED)def _check_choice(value, choices, name):if value not in choices:raise gr.Error(f"{name} must be one of: {', '.join(choices)}.")return valuedef _requested_path(value) -> str:"""Turn whatever the studio/API client sends into a path."""if isinstance(value, str):return valueif not isinstance(value, dict):raise gr.Error(f'Expected an image, got {type(value).__name__}')if path := value.get('path'):return pathurl = value.get('url') or ''if '/gradio_api/file=' in url:return unquote(url.split('/gradio_api/file=', 1)[1])if url.startswith(('http://', 'https://')):raise gr.Error('Upload the image instead of passing a URL.')return unquote(urlparse(url).path)def _local_path(value) -> str:"""A path this process may open: only files inside gradio's upload folder,where /gradio_api/upload puts uploads."""path = os.path.realpath(_requested_path(value))folder = os.path.realpath(get_upload_folder())if os.path.commonpath([path, folder]) != folder:raise gr.Error('Upload the image first.')return pathdef _open_rgb(value):if not value:return Nonereturn ImageOps.exif_transpose(Image.open(_local_path(value))).convert('RGB')def _publish_mp4(path: str) -> str:"""Move the finished mp4 into gradio's upload folder so the studio and the workflow canvascan fetch it by URL."""folder = get_upload_folder()os.makedirs(folder, exist_ok=True)target = os.path.join(folder, f'pi_h3_{os.urandom(8).hex()}.mp4')os.replace(path, target)return targetdef _as_media(path: str) -> dict:"""The media shape used by gr.Workflow inputs and outputs."""import gradio_client.utils as client_utilsreturn {'path': path,'url': f'/gradio_api/file={client_utils.encode_file_path(path)}','is_file': True,}def _run(fn, *args):"""One GPU call with the studio's error surface: messages, not stack traces."""try:return fn(*args)except (RuntimeError, ValueError) as error:message = str(error).split('\n')[0]if 'ZeroGPU' in message and ('limit' in message or 'quota' in message):message += (' The run was billed anonymously: open the Space from its huggingface.co page ''while signed in, so it uses your own ZeroGPU quota — the anonymous allowance ''is tiny and shared.')raise gr.Error(message)# ---- ZeroGPU quota on the workflow canvas -----------------------------------# The canvas is nested inside the studio iframe, so gradio's postMessage handshake with# huggingface.co — which would attach the visitor's x-ip-token to the canvas's requests —# never completes, and its @spaces.GPU calls bill the Space's anonymous allowance. The# studio page's own @gradio/client calls do carry the header, so every studio API call# stashes it per client IP, and workflow runs from the same IP re-attach it here.# Pattern from akhaliq/idol-studio._TOKEN_TTL = 1800 # seconds_tokens: dict[str, tuple[str, float]] = {}_tokens_lock = threading.Lock()def _token_claims(token: str) -> dict:"""The unverified JWT payload, to tell user-attributed tokens from anonymous ones."""try:payload = token.split('.')[1]return json.loads(base64.urlsafe_b64decode(payload + '=' * (-len(payload) % 4)))except Exception: # noqa: BLE001 - not a JWTreturn {}def _client_ip(request) -> str:forwarded = (request.headers.get('x-forwarded-for') or '').split(',')[0].strip()return forwarded or (request.client.host if request.client else '')def _is_user_token(token: str) -> bool:return bool(_token_claims(token).get('user'))def _is_fresh_user_token(token: str) -> bool:"""User-attributed and not expiring in the next few seconds (proxy tokens live ~2-4 min)."""claims = _token_claims(token)return bool(claims.get('user')) and claims.get('exp', 0) > time.time() + 5def _remember_token(request) -> None:if request is None:returntoken = request.headers.get('x-ip-token')if not token:returnip = _client_ip(request)with _tokens_lock:now = time.time()for stale in [stale for stale, (_, at) in _tokens.items() if now - at > _TOKEN_TTL]:del _tokens[stale]existing = _tokens.get(ip)# never downgrade: an anonymous edge token must not replace a usable user-attributed oneif existing and _is_fresh_user_token(existing[0]) and not _is_user_token(token):return_tokens[ip] = (token, now)def _fake_starlette_request(token_or_headers) -> StarletteRequest:"""A real starlette request carrying just an x-ip-token (or a full header dict), so thegradio Request built on top of it behaves like one served over HTTP."""if isinstance(token_or_headers, str):headers = {'x-ip-token': token_or_headers}else:headers = token_or_headersscope = {'type': 'http','method': 'GET','path': '/','scheme': 'https','query_string': b'','headers': [(k.lower().encode('ascii'), str(v).encode('latin-1'))for k, v in headers.items() if v is not None],}return StarletteRequest(scope)def _as_gradio_request(request):"""A gradio Request with a live underlying starlette request — the shape spaces' ZeroGPUhook expects in LocalContext (it reads `request.request`). The workflow runtime and someServer-mode paths inject a raw starlette Request, and queue-created gradio Requests carryno live one; both are wrapped so ZeroGPU can read the visitor's x-ip-token."""if request is None:return Noneif isinstance(request, gr.Request):if request.request is not None:return requestreturn gr.Request(request=_fake_starlette_request(dict(getattr(request, 'headers', {}) or {})))if isinstance(request, StarletteRequest):return gr.Request(request=request)return gr.Request(request=_fake_starlette_request(str(request)))def _zerogpu_request(request):"""The gradio Request to bind to LocalContext for the GPU call, or None to run anonymously.The studio's handshaken token (stored per IP) wins over the injected request's own header:that one may be an anonymous edge-minted token, which bills the Space's anonymous allowance."""direct = request.headers.get('x-ip-token') if request is not None else Noneip = _client_ip(request) if request is not None else ''with _tokens_lock:hit = _tokens.get(ip)stored = hit[0] if hit and time.time() - hit[1] <= _TOKEN_TTL else Noneif stored and _is_user_token(stored) and not _is_fresh_user_token(stored):stored = None # expired: the scheduler would reject it anywayif stored and (_is_user_token(stored) or not direct):return _as_gradio_request(stored)if direct:return _as_gradio_request(request)return Nonedef _on_visitor_quota(request, fn, *args):"""Run one GPU call on the visitor's quota when their token is known, else anonymously."""zerogpu_request = _zerogpu_request(request)if zerogpu_request is None:return _run(fn, *args)reset = LocalContext.request.set(zerogpu_request)try:return _run(fn, *args)finally:LocalContext.request.reset(reset)def _parse_bool(value) -> bool:"""The canvas passes DWD as a text reference; accept any spelling."""return value if isinstance(value, bool) else str(value).strip().lower() in ('true', '1', 'yes', 'on')# ------------------------------------------------------------------ studio and workflow (contributed by AK)# A second UI over the same generate(): a gr.Server studio page at /studio and a gr.Workflow canvas at /workflow,# next to the Blocks demo at /demo. Images arrive as uploaded file paths and are opened the way gr.Image(type='pil',# image_mode='RGB') opens them (EXIF transpose, then RGB), so both UIs feed generate() identical inputs.def build_app(generate):"""The Space's ASGI app: the Blocks demo, the studio and the workflow canvas over one generate()."""def generate_studio(request, seed, prompt, in_image, in_last_image, aspect_ratio, megapixels, duration, steps,remove_decoder):"""One generate() call on the visitor's ZeroGPU quota -> (published mp4 path, run info note)."""start = time.perf_counter()path = _on_visitor_quota(request, generate, seed, prompt, _open_rgb(in_image), _open_rgb(in_last_image), aspect_ratio,megapixels, duration, steps, remove_decoder)height, width = canvas_size(aspect_ratio, megapixels * 1e6)note = (f"{2 if steps < 3 else 4} steps · {'DWD (no decoder)' if remove_decoder else 'VAE decoded'} · "f'{width}x{height} · {time.perf_counter() - start:.1f} s')return _publish_mp4(path), notedef pi_h3_generate(prompt: str = '', in_image: str = '', in_last_image: str = '', seed: str = '42',aspect_ratio: str = '16:9', duration: str = '5.0', steps: str = '2',remove_decoder: str = 'true', request: gr.Request = None) -> dict:"""Generate one video with pi-H3 from a prompt and optional first/last keyframes.gr.Workflow passes the node's inputs positionally, so the parameters follow the order of theop_pi_h3_generate inputs in workflow.json."""_check_choice(aspect_ratio, ASPECT_RATIOS, 'Aspect ratio')if not (prompt or '').strip() and not in_image:raise gr.Error('Write a prompt or add a first frame.')path, _ = generate_studio(request, min(max(int(float(seed)), 0), MAX_SEED), prompt, in_image, in_last_image, aspect_ratio, 1.0,float(duration) if duration else 5.0, int(float(steps)) if steps else 2, _parse_bool(remove_decoder))return _as_media(path)server = gr.Server(title='pi-H3 Studio')@server.get('/')def _root(request: StarletteRequest): # noqa: ANN201"""The Blocks demo is the default page; keep the query (HF passes the theme and sign-in token there)."""query = request.url.queryreturn RedirectResponse('/demo/' + ('?' + query if query else ''))@server.get('/studio', response_class=HTMLResponse)def _studio_page(): # noqa: ANN201with open(os.path.join(ROOT, 'studio.html'), encoding='utf-8') as handle:return handle.read()@server.get('/examples/{name}', response_class=FileResponse)def _example_file(name: str): # noqa: ANN201"""The example keyframes, fetched by the studio page and uploaded by the visitor like any file."""if name not in EXAMPLE_FILES or os.path.sep in name:raise gr.Error('Unknown example.')return FileResponse(os.path.join(ROOT, 'demo/examples/minimax_h3', name))@server.api(name='generate', concurrency_limit=1)def api_generate(prompt: str = '', seed: float = 0, randomize: bool = True, in_image: str = '',in_last_image: str = '', aspect_ratio: str = '16:9', megapixels: float = 1.0,duration: float = 5.0, steps: float = 2, remove_decoder: bool = True,request: gr.Request = None) -> dict:"""Generate one video with pi-H3 (2 or 4 steps, optional VAE-free DWD decoding)."""_remember_token(request)_check_choice(aspect_ratio, ASPECT_RATIOS, 'Aspect ratio')if not (prompt or '').strip() and not in_image:raise gr.Error('Write a prompt or add a first frame.')seed_value = _resolve_seed(seed, randomize)path, note = generate_studio(request, seed_value, prompt, in_image, in_last_image, aspect_ratio, megapixels, duration, steps,remove_decoder)return {'video_url': _as_media(path)['url'], 'info': note, 'seed': seed_value}@server.api(name='handshake')def api_handshake(request: gr.Request = None) -> dict:"""No-op the studio page calls on load and every minute; @gradio/client only attachesthe visitor's ZeroGPU user token to queued calls, which _remember_token then stashesfor the workflow canvas's runs (whose iframe never completes the handshake)."""_remember_token(request)return {'ok': True}workflow = gr.Workflow(graph=os.path.join(ROOT, 'workflow.json'), bind={'pi_h3_generate': pi_h3_generate})workflow.title = 'pi-H3 Workflow'return SimpleNamespace(server=server, mounts={'/demo': (build_interface(generate), dict(allowed_paths=[os.path.join(ROOT, 'demo/examples/minimax_h3')],css_paths=os.path.join(ROOT, 'lakonlab/ui/gradio/style.css'),head_paths=os.path.join(ROOT, 'lakonlab/ui/gradio/head.html'))),'/workflow': (workflow, {})})def launch_demo(app, port=None):# The @server.api endpoints are deferred until Server.launch(), which also fires the ZeroGPU registration hook# (pack weights + startup report) through the patched Blocks.launch path. Launch on a loopback port for# registration, then serve the same app via uvicorn.port = port or int(os.environ.get('GRADIO_SERVER_PORT', '7860'))os.environ['GRADIO_SERVER_PORT'] = '7861'try:app.server.launch(prevent_thread_lock=True, server_name='127.0.0.1', server_port=7861, quiet=True,show_error=False, ssr_mode=False)finally:os.environ['GRADIO_SERVER_PORT'] = str(port)try:app.server.close()except Exception:pass# mount only after the registration launch above, so the mounted queues stay on the public server's event loopserver = app.serverfor path, (blocks, kwargs) in app.mounts.items():server = gr.mount_gradio_app(server, blocks, path=path, ssr_mode=False, **kwargs)import uvicornuvicorn.run(server, host='0.0.0.0', port=port)if __name__ == "__main__":
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714<!DOCTYPE html><html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><title>pi-H3 Studio</title><link rel="preconnect" href="https://fonts.googleapis.com" /><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /><link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=IBM+Plex+Mono:wght@400;500&display=swap" rel="stylesheet" /><style> :root { --bg: #ffffff; --panel: #ffffff; --panel2: #fafafa; --raised: #f5f5f5; --border: #e4e4e4; --hairline: #ececec; --text: #111111; --muted: #666666; --dim: #929292; --accent: #111111; --accent2: #315efb; --accent-soft: rgba(49,94,251,0.08); --lavender: #111111; --link: #315efb; --spark: #f05f67; --stage-base: #fafafa; --stage-square: #efefef; --thumb-bg: #f5f5f5; --button-text: #ffffff; --radius: 12px; } body.dark { --bg: #111111; --panel: #171717; --panel2: #1d1d1d; --raised: #262626; --border: #383838; --hairline: #2c2c2c; --text: #f7f7f7; --muted: #b0b0b0; --dim: #7d7d7d; --accent: #f4f4f4; --accent2: #82a2ff; --accent-soft: rgba(130,162,255,0.12); --lavender: #f4f4f4; --link: #9db5ff; --spark: #f05f67; --stage-base: #131313; --stage-square: #1c1c1c; --thumb-bg: #202020; --button-text: #111111; } * { margin: 0; padding: 0; box-sizing: border-box; } [hidden] { display: none !important; } body { font-family: "Inter", ui-sans-serif, system-ui, sans-serif; background: var(--bg); color: var(--text); height: 100vh; display: flex; flex-direction: column; overflow: hidden; font-size: 13px; -webkit-font-smoothing: antialiased; } .topbar { display: flex; align-items: center; gap: 14px; padding: 12px 24px; background: var(--panel); border-bottom: 1px solid var(--hairline); flex-shrink: 0; } .brand { font-weight: 700; font-size: 1.35rem; white-space: nowrap; letter-spacing: -0.055em; display: flex; align-items: center; } .brand .pi { color: #4585ed; } .brand .dash { color: var(--dim); } .brand .h3 { color: var(--spark); } .studio-badge { font-size: 0.58rem; font-weight: 650; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); border: 1px solid var(--border); border-radius: 999px; padding: 3px 9px; margin-left: 12px; background: transparent; } .mode-tabs { display: flex; gap: 2px; background: var(--panel2); border: 1px solid var(--border); border-radius: 9px; padding: 3px; } .mode-tabs button { border: none; border-radius: 7px; padding: 6px 16px; font-weight: 550; font-size: 0.82rem; background: transparent; color: var(--muted); cursor: pointer; transition: color 0.15s, background 0.15s; font-family: inherit; } .mode-tabs button:hover { color: var(--text); } .mode-tabs button.active { background: var(--accent); color: var(--button-text); box-shadow: none; } .spacer { flex: 1; } .ml-badge { color: var(--muted); text-decoration: none; font-size: 0.8rem; font-weight: 500; transition: color 0.15s; white-space: nowrap; } .ml-badge:hover { color: var(--text); } .ml-badge-img { display: inline-flex; align-items: center; } .ml-badge-img img { height: 20px; display: block; } .ml-badge-img:hover { opacity: 0.85; } .theme-toggle { width: 34px; height: 34px; border: 1px solid var(--border); border-radius: 9px; background: transparent; color: var(--muted); cursor: pointer; flex-shrink: 0; display: inline-flex; align-items: center; justify-content: center; transition: color 0.15s, background 0.15s; } .theme-toggle:hover { color: var(--text); background: var(--panel2); } .theme-toggle svg { width: 17px; height: 17px; } .theme-toggle .theme-icon-moon { display: none; } .theme-toggle[data-mode="light"] .theme-icon-sun { display: none; } .theme-toggle[data-mode="light"] .theme-icon-moon { display: block; } .main { display: flex; flex: 1; min-height: 0; } .rail { width: 52px; flex-shrink: 0; display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 0; background: var(--panel); border-right: 1px solid var(--hairline); } .rail button { width: 38px; height: 38px; border: 1px solid transparent; border-radius: 8px; background: transparent; color: var(--muted); cursor: pointer; display: flex; align-items: center; justify-content: center; transition: color 0.15s, background 0.15s; } .rail button:hover { background: var(--panel2); color: var(--text); } .viewport { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow: auto; position: relative; } .workflow-view { position: absolute; inset: 0; padding: 16px; background: var(--bg); } .workflow-view iframe { width: 100%; height: 100%; border: 1px solid var(--border); border-radius: 12px; background: var(--panel); } .doc-grid { flex: 1; min-height: 0; display: flex; gap: 16px; width: 100%; align-items: stretch; justify-content: center; padding: 20px 20px 10px; } .doc { flex: 1; min-width: 0; display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; overflow: hidden; } .doc figcaption { padding: 8px 12px; font-size: 0.62rem; font-weight: 650; letter-spacing: 0.12em; color: var(--dim); border-bottom: 1px solid var(--hairline); text-transform: uppercase; display: flex; align-items: center; } .doc .stage { position: relative; flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; overflow: hidden; background-color: var(--stage-base); background-image: linear-gradient(45deg, var(--stage-square) 25%, transparent 25%), linear-gradient(-45deg, var(--stage-square) 25%, transparent 25%), linear-gradient(45deg, transparent 75%, var(--stage-square) 75%), linear-gradient(-45deg, transparent 75%, var(--stage-square) 75%); background-size: 20px 20px; background-position: 0 0, 0 10px, 10px -10px, -10px 0; } .doc img, .doc video { max-width: 100%; max-height: 100%; object-fit: contain; display: block; } .doc video.previewing { width: 100%; height: 100%; object-fit: contain; } .drop-hint { color: var(--dim); font-size: 0.85rem; text-align: center; padding: 20px; } .drop-hint b { color: var(--muted); display: block; margin-bottom: 6px; } .doc.dragover { outline: 2px dashed var(--accent2); outline-offset: -2px; } .doc-bar { display: flex; align-items: center; gap: 10px; padding: 6px 12px; border-top: 1px solid var(--hairline); background: var(--panel); font-size: 0.72rem; color: var(--muted); flex-shrink: 0; } .doc-bar .spark-tag { color: var(--spark); font-weight: 600; } .history-strip { display: flex; gap: 8px; padding: 8px 12px; overflow-x: auto; flex-shrink: 0; background: var(--panel); border-top: 1px solid var(--hairline); align-items: center; } .history-strip .hlabel { font-size: 0.62rem; font-weight: 650; letter-spacing: 0.14em; color: var(--dim); text-transform: uppercase; white-space: nowrap; } .history-chip { width: 52px; height: 52px; border-radius: 6px; border: 1px solid var(--border); overflow: hidden; cursor: pointer; flex-shrink: 0; position: relative; display: flex; align-items: center; justify-content: center; background: var(--raised); color: var(--dim); font-size: 0.55rem; letter-spacing: 0.08em; } .history-chip:hover { border-color: var(--accent); } .history-chip img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; } .history-chip .hseed { position: absolute; bottom: 0; left: 0; right: 0; background: rgba(0,0,0,0.65); color: #ddd; font-size: 0.52rem; padding: 1px 3px; text-align: center; z-index: 2; } body.dark .history-chip .hseed { background: rgba(0,0,0,0.72); color: #eeeeee; } .panels { width: 400px; flex-shrink: 0; display: flex; flex-direction: column; gap: 10px; padding: 12px; overflow-y: auto; background: var(--panel); border-left: 1px solid var(--hairline); } .panel { background: var(--panel2); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; } .panel-title { font-size: 0.62rem; font-weight: 650; letter-spacing: 0.12em; color: var(--dim); text-transform: uppercase; margin-bottom: 10px; cursor: pointer; user-select: none; display: flex; align-items: center; justify-content: space-between; } .panel .panel-title::after { content: "▾"; font-size: 0.7rem; color: var(--dim); } .panel.collapsed .panel-title::after { content: "▸"; } .panel.collapsed .panel-body { display: none; } .panel label { display: block; font-size: 0.72rem; font-weight: 550; color: var(--muted); margin: 10px 0 4px; } .panel label:first-of-type { margin-top: 0; } textarea, input[type="text"], input[type="number"], input[type="range"], select { width: 100%; background: var(--raised); color: var(--text); border: 1px solid var(--border); border-radius: 7px; padding: 8px 10px; font-family: inherit; font-size: 0.83rem; resize: vertical; transition: border-color 0.15s, box-shadow 0.15s; } input[type="range"] { padding: 0; background: transparent; border: none; accent-color: var(--accent2); } select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%; background-size: 5px 5px; background-repeat: no-repeat; } textarea:focus, input:focus, select:focus { outline: none; border-color: var(--accent2); box-shadow: 0 0 0 3px var(--accent-soft); } textarea.code { font-family: "IBM Plex Mono", ui-monospace, monospace; font-size: 0.72rem; } .row { display: flex; gap: 8px; align-items: center; } .row > * { flex: 1; } .range-row { display: flex; align-items: center; gap: 10px; } .range-row input { flex: 1; } .range-out { font-family: "IBM Plex Mono", ui-monospace, monospace; font-size: 0.72rem; color: var(--lavender); min-width: 44px; text-align: right; } .panel label.switch, .switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; margin: 0; } .switch input { display: none; } .switch .sw { display: inline-block; width: 34px; height: 20px; border-radius: 999px; background: var(--raised); border: 1px solid var(--border); position: relative; flex-shrink: 0; transition: background 0.18s, border-color 0.18s; } .switch .sw::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%; background: var(--muted); transition: transform 0.18s, background 0.18s; } .switch input:checked + .sw { background: var(--accent); border-color: var(--accent); } .switch input:checked + .sw::after { transform: translateX(14px); background: #fff; } .switch .sw-label { font-size: 0.75rem; color: var(--muted); } .hint { font-size: 0.68rem; color: var(--dim); margin-top: 8px; line-height: 1.4; } .hint a { color: var(--link); text-decoration: none; } #examples { display: flex; flex-direction: column; gap: 6px; max-height: 240px; overflow-y: auto; } .ex-chip { border: 1px solid var(--border); border-radius: 8px; cursor: pointer; flex-shrink: 0; background: var(--raised); padding: 7px 10px; text-align: left; display: flex; align-items: center; justify-content: space-between; gap: 8px; transition: border-color 0.15s; font-family: inherit; } .ex-chip:hover { border-color: var(--accent); } .ex-chip .ex-label { font-size: 0.75rem; color: var(--text); font-weight: 550; } .ex-chip .ex-aspect { font-size: 0.62rem; color: var(--lavender); font-family: "IBM Plex Mono", ui-monospace, monospace; white-space: nowrap; } .frame-slots { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; } .frame-slot { position: relative; aspect-ratio: 16 / 10; overflow: hidden; border: 1px dashed var(--border); border-radius: 8px; background: var(--raised); padding: 8px; cursor: pointer; font-family: inherit; text-align: center; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; transition: border-color 0.15s; } .frame-slot:hover { border-color: var(--accent); } .frame-slot.filled { border-style: solid; } .frame-slot .fs-thumb { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; } .frame-slot .fs-label { font-size: 0.75rem; font-weight: 550; color: var(--muted); } .frame-slot .fs-sub { font-size: 0.65rem; color: var(--dim); } .frame-slot.filled .fs-label, .frame-slot.filled .fs-sub { display: none; } .frame-slot .fs-clear { position: absolute; top: 5px; right: 5px; z-index: 1; width: 22px; height: 22px; border-radius: 50%; display: flex; align-items: center; justify-content: center; background: rgba(0, 0, 0, 0.6); color: #fff; font-size: 0.7rem; cursor: pointer; } .frame-slot .fs-clear[hidden] { display: none; } #run-btn { margin-top: 4px; padding: 12px; border: 1px solid var(--accent); border-radius: 999px; cursor: pointer; background: var(--accent); color: var(--button-text); box-shadow: none; font-weight: 600; font-size: 0.9rem; letter-spacing: 0.01em; font-family: inherit; transition: filter 0.15s, transform 0.05s; } #run-btn:hover { filter: brightness(1.12); } #run-btn:active { transform: translateY(1px); } #run-btn:disabled { opacity: 0.5; cursor: wait; } .statusbar { display: flex; align-items: center; gap: 16px; padding: 7px 16px; flex-shrink: 0; background: var(--panel); border-top: 1px solid var(--hairline); font-size: 0.75rem; color: var(--muted); } .statusbar .pulse { width: 8px; height: 8px; border-radius: 50%; background: #4fd1a5; } .statusbar.busy .pulse { background: var(--accent2); animation: pulse 1s infinite alternate; } @keyframes pulse { from { opacity: 0.4; } to { opacity: 1; } } .foot-links { display: flex; gap: 12px; align-items: center; } .foot-links a { color: var(--dim); text-decoration: none; font-size: 0.72rem; font-weight: 500; } .foot-links a:hover { color: var(--link); } #file-input { display: none; } @media (max-width: 860px) { html, body { height: auto !important; overflow: visible !important; } body { display: block; } .topbar { flex-wrap: wrap; gap: 8px; padding: 8px 12px; } .brand { font-size: 1.1rem; } .topbar .ml-badge, .topbar .ml-badge-img { display: none; } .main { display: block; height: auto; min-height: 0; } .rail { flex-direction: row; width: 100%; justify-content: center; gap: 10px; padding: 6px 10px; border-right: none; border-bottom: 1px solid var(--hairline); } .rail button { width: 44px; height: 44px; } .viewport { display: block; padding: 10px; overflow: visible; } .workflow-view { position: static; height: 78vh; padding: 0 0 10px; } .doc-grid { flex-direction: column; align-items: stretch; height: auto; } .doc { max-width: 100%; width: 100%; } .doc .stage { min-height: 220px; } .panels { width: 100%; border-left: none; border-top: 1px solid var(--hairline); padding: 10px 12px; overflow-y: visible; } .mode-tabs button { padding: 6px 13px; font-size: 0.8rem; } .foot-links { display: none; } .statusbar { position: sticky; bottom: 0; } }</style></head><body> <header class="topbar"> <div class="brand"><span class="pi">pi</span><span class="dash">-</span><span class="h3">H3</span><span class="studio-badge">Video Studio</span></div> <div class="mode-tabs"> <button onclick="location.href='/demo/' + location.search">Classic UI</button> <button id="mode-generate" class="active">Generate</button> <button id="mode-workflow">Workflow</button> </div> <div class="spacer"></div> <a class="ml-badge-img" href="https://hf.co/chat?mode=ml-intern" target="_blank" rel="noopener"> <img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/built-with-ml-intern-md.svg" alt="Built with ML Intern" /> </a> <a class="ml-badge" href="https://hanshengchen.com/blog/pi-h3/" target="_blank" rel="noopener">Blog</a> <a class="ml-badge" href="https://huggingface.co/Lakonik/pi-H3" target="_blank" rel="noopener">Model</a> <button type="button" class="theme-toggle" id="theme-toggle" data-mode="light" aria-label="Switch to dark mode" title="Switch to dark mode"> <svg class="theme-icon-sun" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <circle cx="12" cy="12" r="4"></circle> <path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"></path> </svg> <svg class="theme-icon-moon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M21 14.5A8.5 8.5 0 1 1 9.5 3 7 7 0 0 0 21 14.5z"></path> </svg> </button> </header>
<div class="main"> <div class="rail"> <button id="tool-upload" title="Upload a first frame"><svg width="19" height="19" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="17 8 12 3 7 8"/><line x1="12" y1="3" x2="12" y2="15"/></svg></button> <button id="tool-clear-doc" title="Clear document"><svg width="19" height="19" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg></button> </div>
<div class="viewport"> <div class="workflow-view" id="workflow-view" hidden> <iframe src="/workflow/?ui=canvas" title="pi-H3 workflow canvas"></iframe> </div> <div class="doc-grid"> <figure class="doc" id="doc-after"> <figcaption>Output video</figcaption> <div class="stage" id="after-stage"> <video id="result-video" controls autoplay muted playsinline hidden></video> <div class="drop-hint" id="result-hint"><b>Nothing yet</b>Hit Generate — 2 or 4 steps, audio included</div> </div> <div class="doc-bar"> <span id="result-meta">—</span> <div class="spacer"></div> <a id="result-download" href="#" download="pi_h3.mp4" target="_blank" rel="noopener" hidden>MPEG-4</a> </div> </figure> </div> <div class="history-strip" id="history-strip"><span class="hlabel">History</span></div> </div>
<aside class="panels"> <section class="panel"> <div class="panel-title">Examples</div> <div id="examples"></div> </section> <section class="panel"> <div class="panel-title">Prompt</div> <div class="panel-body"> <textarea id="prompt" rows="8" placeholder="Describe the video: shots, camera, dialogue between <d> tags, soundscape, music…"></textarea> <div class="hint">H3 prompting guide: <a href="https://huggingface.co/MiniMaxAI/MiniMax-H3/blob/main/docs/VIDEO_PROMPT_WRITING_GUIDE_base_en.md" target="_blank" rel="noopener">VIDEO_PROMPT_WRITING_GUIDE_base_en.md</a></div> </div> </section> <section class="panel"> <div class="panel-title">Keyframes</div> <div class="panel-body"> <div class="frame-slots"> <button class="frame-slot" id="slot-first"><img class="fs-thumb" id="fs-first-thumb" alt="" hidden /><span class="fs-label">First frame</span><span class="fs-sub">Drop or click</span><span class="fs-clear" id="fs-first-clear" role="button" title="Clear first frame" hidden>✕</span></button> <button class="frame-slot" id="slot-last"><img class="fs-thumb" id="fs-last-thumb" alt="" hidden /><span class="fs-label">Last frame</span><span class="fs-sub">Drop or click</span><span class="fs-clear" id="fs-last-clear" role="button" title="Clear last frame" hidden>✕</span></button> </div> <div class="hint">Both keyframes are optional. They are center-cropped and resized onto the requested canvas. A last frame needs both shots described in the prompt.</div> </div> </section> <section class="panel"> <div class="panel-title">Settings</div> <div class="panel-body"> <div class="row"> <div><label for="aspect">Aspect ratio</label><select id="aspect"></select></div> <div><label for="steps">Steps</label><select id="steps"><option value="2">2</option><option value="4">4</option></select></div> </div> <label for="duration">Duration</label> <div class="range-row"><input type="range" id="duration" min="5" max="15" step="0.5" value="5" /><span class="range-out" id="duration-out">5.0 s</span></div> <label for="megapixels">Megapixels</label> <div class="range-row"><input type="range" id="megapixels" min="0.25" max="1" step="0.05" value="1" /><span class="range-out" id="megapixels-out">1.00 MP</span></div> <div class="row" style="margin-top:10px"> <label class="switch"><input type="checkbox" id="remove-decoder" checked /><span class="sw"></span><span class="sw-label">Remove decoder (DWD direct pixel prediction)</span></label> </div> </div> </section> <section class="panel"> <div class="panel-title">Advanced</div> <div class="panel-body"> <div class="row"> <div><label for="seed">Seed</label><input type="number" id="seed" value="0" min="0" /></div> <label class="switch" style="align-self:flex-end;height:34px"><input type="checkbox" id="randomize" checked /><span class="sw"></span><span class="sw-label">Randomize</span></label> </div> </div> </section> <section class="panel"> <div class="panel-title">Run info</div> <div class="panel-body"> <textarea id="run-info" class="code" rows="3" readonly placeholder="Steps, decoder tier, resolution, frame count and timing appear here after a run."></textarea> </div> </section> <button id="run-btn">Generate</button> </aside> </div>
<footer class="statusbar" id="statusbar"> <div class="pulse"></div> <span id="status">Idle</span> <div class="spacer"></div> <span class="foot-links"> <a href="https://hanshengchen.com/blog/pi-h3/" target="_blank" rel="noopener">Blog</a> <a href="https://hanshengchen.com/dwd/" target="_blank" rel="noopener">Project page</a> <a href="https://arxiv.org/abs/2611.22287" target="_blank" rel="noopener">arXiv</a> <a href="https://github.com/Lakonik/LakonLab" target="_blank" rel="noopener">Code</a> </span> <span id="seed-out"></span> </footer>
<input type="file" id="file-input" accept="image/*" />
<script type="module"> import { client } from "https://cdn.jsdelivr.net/npm/@gradio/client@2.7.1/dist/index.min.js";
const $ = (id) => document.getElementById(id); const state = { mode: "generate", firstPath: null, lastName: null, lastPath: null, app: null, target: "first" }; // which slot the file input / drop targets fill
// The three demo examples (demo/examples/minimax_h3/examples.json), prompts included. const EXAMPLES = [ { label: "Beach bear selfie (official default)", prompt: "integrated_multimodal_description: [Shot 1] Live-action, handheld smartphone selfie video, a close medium shot with the camera held at arm's length slightly above eye level, a young man in an orange beanie and a green rain jacket fills the left 40 percent of the frame and looks straight into the lens. Behind him, a lakeside campsite glows at golden hour, with a yellow tent at the right edge of the background and a dark treeline above the shore. The camera shakes slightly. The young man with a cheerful, breathy voice (S1) says: <d>[English] Day two of camping, and the vibes are immaculate.</d> While he speaks without noticing, a large brown bear steps out of the treeline in the right background and walks along the shore toward the camera, growing larger in the frame. It stops directly behind him and lowers its huge head onto his shoulder nearest the right edge of the frame, so that its snout reaches into the right half of the frame beside his ear. His smile freezes in place and his eyes slide sideways toward the bear without his head moving.\n\noverall_soundscape: Wind hisses across the lake while gravel crunches under heavy, slow paws. The bear lets out a long, warm huff beside the microphone and his nylon jacket rustles as he goes rigid.\n\nnon_diegetic_music: N/A", aspect: "16:9", duration: 5.0, steps: 2, seed: 0, first: null, last: null, }, { label: "Your render outlived you", prompt: "integrated_multimodal_description: [Shot 1] Live-action, epic cinematic, a slow push-in through a dim, dusty server room overgrown with thick cobwebs, a single ancient computer monitor glowing at the center of the frame showing a progress bar stuck at \"99%\", a skeleton in an office chair slumped in front of it with one bony finger still on the mouse, dust drifting through a shaft of cold light. A deep, gravelly movie-trailer narrator (S1) says off-screen: <d>[English] In a world where your render outlived you...</d>\n\noverall_soundscape: A low electrical hum, the slow creak of the office chair, and a single cooling fan rattling and winding down.\n\nnon_diegetic_music: Ominous low brass and taiko drums build slowly.", aspect: "16:9", duration: 5.0, steps: 2, seed: 1034413408, first: null, last: null, }, { label: "Distracted boyfriend", prompt: "For the target video, at 0.00 seconds into the target video, <Picture 1> (from [Shot 1]) is fully referenced.\n\nintegrated_multimodal_description: [Shot 1] Live-action, candid street photography look with shallow depth of field, the three people shown in <Picture 1> walk along a sunlit European city street, preserving their exact positions: the woman in the red sleeveless top at the left foreground walking away from the camera, the young man in the blue plaid short-sleeve shirt at the center with his head turned back over his left shoulder toward the woman in red, and his girlfriend in the light blue sleeveless top at the right, staring at him with her mouth open in disbelief. The blurred stone buildings and warm afternoon light are preserved. The camera holds a static shot. The man's head keeps turning further toward the woman in red, his whole upper body twisting now, and he stumbles slightly on the pavement as he stops watching where he's walking. His girlfriend's expression sharpens, she yanks her hand free from his and smacks him on the upper arm with her open palm. The girlfriend with an offended, sharp, loud voice (S1) says: <d>[Spanish] ¿En serio?</d> He flinches and snaps his head back toward her, raising both hands in front of his chest. The woman in red glances back over her right shoulder with a small smirk and keeps walking out of the left edge of the frame.\n\noverall_soundscape: Footsteps on stone pavement, distant city traffic, the slap of a palm on a cloth sleeve, and a faint murmur of pedestrians in the background.\n\nnon_diegetic_music: N/A", aspect: "16:9", duration: 5.0, steps: 2, seed: 1262275999, first: "distracted_boyfriend.webp", last: null, }, { label: "Woman yelling at cat", prompt: "How the reference pictures align with the target video — <Picture 1> (from [Shot 1]) aligns with the 0.00-second mark of the target video; <Picture 2> (from [Shot 2]) aligns with the 10.00-second mark of the target video.\n\nintegrated_multimodal_description: [Shot 1] Live-action, reality TV look with shallow depth of field, the scene shown in <Picture 1> at an outdoor restaurant patio in warm afternoon sunlight, preserving all positions: the blonde woman in the black sleeveless top at the center-left, mouth wide open and right arm extended pointing toward the camera, pearl bracelets on her wrist, a small white fluffy dog on her lap, and the dark-haired woman in the beige lace top behind her at the right, gripping her arm and looking alarmed. A marble table in the foreground holds a green salad plate, a glass of white wine, water glasses, and white hydrangeas. Diners sit in soft focus behind a black iron railing in the background. The camera holds a static shot. The blonde woman leans further forward, her pointing finger jabbing the air, and she slams her other hand flat on the table, rattling the glasses and sloshing wine. The blonde woman with a loud, high, furious voice (S1) shouts: <d>[English] I had a meeting on Zoom and you walked across the keyboard!</d> [Shot 2] At 00:05.000, the camera cuts to a dim indoor restaurant booth, where the white cat shown in <Picture 2> sits behind a dark table in a dark leather booth seat, its head just above table height. A man walks into the frame from the right edge and pets the cat's head as the cat meows quietly, and then the man puts his hand on the chair above the cat. Canned sitcom laughter. The camera holds a static shot. The cat blinks once, slowly, and the laughter dies down. Its left ear flattens back. It looks down at the salad, then back up at the camera with the same flat expression. The scene settles into the exact composition, lighting, and framing of <Picture 2>.\n\noverall_soundscape: Outdoor patio ambience with clinking glasses, distant conversation, and birds in the background during the first shot. A hand slaps the marble table with a sharp smack and wine sloshes in the glass. The small dog barks in sharp, rapid bursts. After the cut, the sound drops to a quiet indoor room with faint restaurant murmur. Two bursts of canned sitcom studio audience laughter play, the first loud and long on the cut, the second shorter after the cat looks back up.\n\nnon_diegetic_music: N/A", aspect: "16:9", duration: 10.0, steps: 2, seed: 14851915, first: "woman_cat_meme_first.webp", last: "woman_cat_meme_last.webp", }, ];
async function getApp() { if (!state.app) state.app = await client(location.origin); return state.app; }
async function uploadFile(file) { const form = new FormData(); form.append("files", file); const resp = await fetch("/gradio_api/upload", { method: "POST", body: form }); if (!resp.ok) throw new Error(`Upload failed (${resp.status})`); const paths = await resp.json(); return Array.isArray(paths) ? paths[0] : paths; }
async function uploadExampleUrl(url, name) { const resp = await fetch(url); if (!resp.ok) throw new Error(`Could not load ${name} (${resp.status})`); const blob = await resp.blob(); return uploadFile(new File([blob], name, { type: blob.type || "image/webp" })); }
function setStatus(text, busy = false) { $("status").textContent = text; $("statusbar").classList.toggle("busy", busy); $("run-btn").disabled = busy; }
// ---------- theme ---------- const themeBtn = $("theme-toggle"); function applyTheme(mode) { document.body.classList.toggle("dark", mode === "dark"); themeBtn.dataset.mode = mode; const label = mode === "dark" ? "Switch to light mode" : "Switch to dark mode"; themeBtn.setAttribute("aria-label", label); themeBtn.setAttribute("title", label); try { localStorage.setItem("pi-h3-theme", mode); } catch (e) {} } themeBtn.onclick = () => applyTheme(document.body.classList.contains("dark") ? "light" : "dark"); (function initTheme() { let mode = "light"; try { if (localStorage.getItem("pi-h3-theme") === "dark") mode = "dark"; } catch (e) {} applyTheme(mode); })();
function setMode(mode) { state.mode = mode; for (const m of ["generate", "workflow"]) $(`mode-${m}`).classList.toggle("active", mode === m); const workflow = mode === "workflow"; $("workflow-view").hidden = !workflow; document.querySelector(".doc-grid").style.display = workflow ? "none" : ""; document.querySelector(".rail").style.display = workflow ? "none" : ""; document.querySelector(".panels").style.display = workflow ? "none" : ""; $("history-strip").style.display = workflow ? "none" : ""; setStatus(workflow ? "Chain nodes into one pipeline: prompt and keyframes feed the pi-H3 generate node" : "Idle"); } $("mode-generate").onclick = () => setMode("generate"); $("mode-workflow").onclick = () => setMode("workflow");
// ---------- keyframes ---------- const fileInput = $("file-input"); // one keyframe slot ("first" or "last"): its uploaded path, thumbnail and clear button function setSlot(slot, path, src, name) { state[slot + "Path"] = path; const thumb = $(`fs-${slot}-thumb`); thumb.hidden = !src; if (src) thumb.src = src; else thumb.removeAttribute("src"); $(`fs-${slot}-clear`).hidden = !src; $(`slot-${slot}`).classList.toggle("filled", !!src); $(`slot-${slot}`).title = name || ""; } async function takeFile(file) { setStatus("Uploading…", true); try { setSlot(state.target, await uploadFile(file), URL.createObjectURL(file), file.name); setStatus("Idle"); } catch (err) { setStatus(String(err)); } } $("tool-upload").onclick = () => { state.target = "first"; fileInput.click(); }; for (const slot of ["first", "last"]) { $(`slot-${slot}`).onclick = () => { state.target = slot; fileInput.click(); }; $(`fs-${slot}-clear`).onclick = (e) => { e.stopPropagation(); setSlot(slot, null, null); }; $(`slot-${slot}`).addEventListener("dragover", (e) => e.preventDefault()); $(`slot-${slot}`).addEventListener("drop", (e) => acceptDrop(e, slot)); } fileInput.onchange = async (e) => { const file = e.target.files[0]; e.target.value = ""; if (file) await takeFile(file); }; const acceptDrop = async (e, target) => { e.preventDefault(); const file = e.dataTransfer.files[0]; if (!file) return; state.target = target; await takeFile(file); };
// ---------- history ---------- // Each run is its own entry: restoring a chip restores its prompt, keyframes note, // settings and (still valid) video URL, without touching the current document keys. const history = []; function renderHistory() { const strip = $("history-strip"); strip.innerHTML = '<span class="hlabel">History</span>'; history.forEach((h) => { const chip = document.createElement("div"); chip.className = "history-chip"; chip.title = h.info || ""; if (h.poster) { const img = document.createElement("img"); img.src = h.poster; chip.appendChild(img); } else { chip.textContent = "MP4"; } const tag = document.createElement("div"); tag.className = "hseed"; tag.textContent = h.seed ?? ""; chip.appendChild(tag); chip.onclick = () => restoreRun(h); strip.appendChild(chip); }); } function pushHistory(entry) { history.unshift(entry); if (history.length > 8) history.pop(); renderHistory(); } function restoreRun(h) { setMode("generate"); $("prompt").value = h.prompt || ""; $("aspect").value = h.aspect || "16:9"; $("steps").value = h.steps || "2"; $("duration").value = h.duration ?? 5; $("duration-out").textContent = ($("duration").value) + " s"; $("megapixels").value = h.megapixels ?? 1; $("megapixels-out").textContent = Number($("megapixels").value).toFixed(2) + " MP"; $("remove-decoder").checked = !!h.remove_decoder; if (h.seed_v != null) { $("seed").value = h.seed_v; $("randomize").checked = false; } if (h.video_url) showResult(h.video_url, h); $("run-info").value = h.info || ""; }
// ---------- settings sliders ---------- const aspectSel = $("aspect"); for (const r of ["21:9", "16:9", "4:3", "1:1", "3:4", "9:16"]) { const opt = document.createElement("option"); opt.value = r; opt.textContent = r; aspectSel.appendChild(opt); } aspectSel.value = "16:9"; $("duration").addEventListener("input", () => { $("duration-out").textContent = Number($("duration").value).toFixed(1) + " s"; }); $("megapixels").addEventListener("input", () => { $("megapixels-out").textContent = Number($("megapixels").value).toFixed(2) + " MP"; }); $("steps").value = "2"; $("remove-decoder").checked = true; $("prompt").value = document.querySelector("#prompt").dataset.default || "";
// ---------- examples ---------- function renderExamples() { const exRow = $("examples"); exRow.innerHTML = ""; EXAMPLES.forEach((ex) => { const chip = document.createElement("button"); chip.className = "ex-chip"; const label = document.createElement("span"); label.className = "ex-label"; label.textContent = ex.label; const tag = document.createElement("span"); tag.className = "ex-aspect"; tag.textContent = `${ex.aspect} · ${ex.duration} s`; chip.appendChild(label); chip.appendChild(tag); chip.onclick = async () => { setMode("generate"); setStatus("Loading example…", true); try { $("prompt").value = ex.prompt; aspectSel.value = ex.aspect; $("steps").value = String(ex.steps); $("duration").value = ex.duration; $("duration-out").textContent = ex.duration.toFixed(1) + " s"; $("seed").value = ex.seed; $("randomize").checked = false; // an example sets both slots, so a frame from an earlier example never lingers for (const slot of ["first", "last"]) { const name = ex[slot]; setSlot(slot, name ? await uploadExampleUrl(`/examples/${name}`, name) : null, name ? `/examples/${name}` : null, name); } setStatus("Example loaded — hit Generate"); } catch (err) { setStatus(String(err)); } }; exRow.appendChild(chip); }); }
// ---------- ZeroGPU quota handshake for the workflow canvas ---------- // The canvas iframe can't complete gradio's postMessage handshake with huggingface.co, so its // GPU calls would bill the Space's anonymous allowance. The studio page's own @gradio/client // calls carry the visitor's x-ip-token; /handshake stashes it server-side per client IP for // the canvas's runs to re-attach. getApp().then((app) => app.predict("/handshake", {})).catch(() => {}); setInterval(() => { getApp().then((app) => app.predict("/handshake", {})).catch(() => {}); }, 60000);
// ---------- run ---------- function unwrap(result) { const data = Array.isArray(result?.data) ? result.data[0] : result?.data; return data ?? {}; }
function showResult(uri, payload) { const video = $("result-video"); video.src = uri; video.hidden = false; video.classList.add("previewing"); $("result-hint").style.display = "none"; const dl = $("result-download"); dl.href = uri; dl.hidden = false; if (payload?.seed != null) $("seed-out").textContent = "seed " + payload.seed; if (payload?.info) { $("result-meta").textContent = payload.info; $("run-info").value = payload.info; } }
$("run-btn").onclick = async () => { const app = await getApp(); const prompt = $("prompt").value.trim(); const seed = parseInt($("seed").value, 10) || 0; const randomize = $("randomize").checked; if (!prompt && !state.firstPath) { setStatus("Write a prompt or add a first frame."); return; } setStatus("Generating with pi-H3… (a short clip takes tens of seconds)", true); $("result-video").hidden = true; $("result-download").hidden = true; $("result-video").removeAttribute("src"); try { const payload = unwrap(await app.predict("/generate", { prompt: prompt || "", seed, randomize, in_image: state.firstPath || "", in_last_image: state.lastPath || "", aspect_ratio: aspectSel.value, megapixels: parseFloat($("megapixels").value) || 1.0, duration: parseFloat($("duration").value) || 5.0, steps: parseFloat($("steps").value) || 2, remove_decoder: $("remove-decoder").checked, })); const uri = typeof payload.video_url === "string" ? payload.video_url : null; if (!uri) { setStatus("Run finished but no video came back (see console)."); return; } const preset = state.firstPath ? "" : null; showResult(uri, payload); pushHistory({ prompt, aspect: aspectSel.value, steps: $("steps").value, duration: parseFloat($("duration").value), megapixels: parseFloat($("megapixels").value), remove_decoder: $("remove-decoder").checked, seed: payload.seed, seed_v: payload.seed, info: payload.info, video_url: uri, poster: state.firstPath ? $("fs-first-thumb").src : null, preset, }); setStatus("Done"); } catch (err) { setStatus(err?.message || String(err)); } };
// ---------- collapsible panels ---------- document.querySelectorAll(".panel .panel-title").forEach((title) => { const body = document.createElement("div"); body.className = "panel-body"; [...title.parentElement.children].forEach((child) => { if (child !== title) body.appendChild(child); }); title.parentElement.appendChild(body); title.onclick = () => title.parentElement.classList.toggle("collapsed"); });
// ---------- clear ---------- $("tool-clear-doc").onclick = () => { setSlot("first", null, null); setSlot("last", null, null); const video = $("result-video"); video.hidden = true; video.removeAttribute("src"); $("result-hint").style.display = ""; $("result-download").hidden = true; $("result-meta").textContent = "—"; $("seed-out").textContent = ""; $("run-info").value = ""; };
setMode("generate"); renderExamples();</script></body></html>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161{ "schema_version": "2", "name": "pi-H3 Workflow", "references": [ { "id": "ref_prompt", "label": "Prompt", "role": "reference", "asset_type": "text", "inputs": [{"id": "in", "label": "Text", "type": "text"}], "outputs": [{"id": "out", "label": "Text", "type": "text"}], "x": 40, "y": 60, "width": 300, "height": 200, "data": {"out": "integrated_multimodal_description: [Shot 1] Live-action, handheld selfie video, a young man in an orange beanie at a lakeside campsite at golden hour says: <d>[English] Day two of camping.</d> Behind him, a large brown bear steps out of the treeline and walks toward the camera."} }, { "id": "ref_seed", "label": "Seed", "role": "reference", "asset_type": "text", "inputs": [{"id": "in", "label": "Text", "type": "text"}], "outputs": [{"id": "out", "label": "Text", "type": "text"}], "x": 40, "y": 290, "width": 140, "height": 90, "data": {"out": "42"} }, { "id": "ref_aspect", "label": "Aspect Ratio", "role": "reference", "asset_type": "text", "inputs": [{"id": "in", "label": "Text", "type": "text"}], "outputs": [{"id": "out", "label": "Text", "type": "text"}], "x": 40, "y": 410, "width": 160, "height": 90, "data": {"out": "16:9"} }, { "id": "ref_duration", "label": "Duration (s)", "role": "reference", "asset_type": "text", "inputs": [{"id": "in", "label": "Text", "type": "text"}], "outputs": [{"id": "out", "label": "Text", "type": "text"}], "x": 40, "y": 530, "width": 160, "height": 90, "data": {"out": "5.0"} }, { "id": "ref_steps", "label": "Steps (2 or 4)", "role": "reference", "asset_type": "text", "inputs": [{"id": "in", "label": "Text", "type": "text"}], "outputs": [{"id": "out", "label": "Text", "type": "text"}], "x": 40, "y": 650, "width": 150, "height": 90, "data": {"out": "2"} }, { "id": "ref_decoder", "label": "Remove Decoder (DWD)", "role": "reference", "asset_type": "text", "inputs": [{"id": "in", "label": "Text", "type": "text"}], "outputs": [{"id": "out", "label": "Text", "type": "text"}], "x": 40, "y": 770, "width": 180, "height": 90, "data": {"out": "true"} }, { "id": "ref_first", "label": "Optional First Frame", "role": "reference", "asset_type": "image", "inputs": [{"id": "in", "label": "Image", "type": "image"}], "outputs": [{"id": "out", "label": "Image", "type": "image"}], "x": 40, "y": 890, "width": 220, "height": 130, "data": {} }, { "id": "ref_last", "label": "Optional Last Frame", "role": "reference", "asset_type": "image", "inputs": [{"id": "in", "label": "Image", "type": "image"}], "outputs": [{"id": "out", "label": "Image", "type": "image"}], "x": 40, "y": 1050, "width": 220, "height": 130, "data": {} } ], "operators": [ { "id": "op_pi_h3_generate", "label": "pi-H3 Generate", "role": "operator", "kind": "fn", "fn": "pi_h3_generate", "inputs": [ {"id": "prompt", "label": "Prompt", "type": "text", "required": true}, {"id": "in_image", "label": "First Frame", "type": "image"}, {"id": "in_last_image", "label": "Last Frame", "type": "image"}, {"id": "seed", "label": "Seed", "type": "text"}, {"id": "aspect_ratio", "label": "Aspect Ratio", "type": "text", "required": true}, {"id": "duration", "label": "Duration (s)", "type": "text"}, {"id": "steps", "label": "Steps", "type": "text"}, {"id": "remove_decoder", "label": "Remove Decoder", "type": "text"} ], "outputs": [{"id": "out_0", "label": "Video", "type": "video", "output_index": 0}], "x": 420, "y": 60, "width": 260, "height": 300, "data": {} } ], "subjects": [ { "id": "sub_video", "label": "Video (MP4, 24 fps, with audio)", "role": "subject", "asset_type": "video", "inputs": [{"id": "in", "label": "Video", "type": "video"}], "outputs": [{"id": "out", "label": "Video", "type": "video"}], "x": 760, "y": 120, "width": 280, "height": 180, "data": {} } ], "edges": [ {"id": "e_prompt", "from_node_id": "ref_prompt", "from_port_id": "out", "to_node_id": "op_pi_h3_generate", "to_port_id": "prompt", "type": "text"}, {"id": "e_seed", "from_node_id": "ref_seed", "from_port_id": "out", "to_node_id": "op_pi_h3_generate", "to_port_id": "seed", "type": "text"}, {"id": "e_aspect", "from_node_id": "ref_aspect", "from_port_id": "out", "to_node_id": "op_pi_h3_generate", "to_port_id": "aspect_ratio", "type": "text"}, {"id": "e_duration", "from_node_id": "ref_duration", "from_port_id": "out", "to_node_id": "op_pi_h3_generate", "to_port_id": "duration", "type": "text"}, {"id": "e_steps", "from_node_id": "ref_steps", "from_port_id": "out", "to_node_id": "op_pi_h3_generate", "to_port_id": "steps", "type": "text"}, {"id": "e_decoder", "from_node_id": "ref_decoder", "from_port_id": "out", "to_node_id": "op_pi_h3_generate", "to_port_id": "remove_decoder", "type": "text"}, {"id": "e_first", "from_node_id": "ref_first", "from_port_id": "out", "to_node_id": "op_pi_h3_generate", "to_port_id": "in_image", "type": "image"}, {"id": "e_last", "from_node_id": "ref_last", "from_port_id": "out", "to_node_id": "op_pi_h3_generate", "to_port_id": "in_last_image", "type": "image"}, {"id": "e_video", "from_node_id": "op_pi_h3_generate", "from_port_id": "out_0", "to_node_id": "sub_video", "to_port_id": "in", "type": "video"} ]}app.py+349-11
studio.htmlAdded+714
workflow.jsonAdded+161