[Frontend] Add midway prompt update for streaming video generation (#4652)

Signed-off-by: Huang, Zeyu <11222265+fhfuih@users.noreply.github.com>
This commit is contained in:
Zeyu Huang | 黃澤宇 authored and GitHub committed 2026-07-27 14:07:43 +08:00
1 parent 1f01d05603
commit eaf90d8eff
28 files changed
+2615 -133

No files matched your search

@@ -14,8 +14,9 @@ from __future__ import annotations
import argparse
import json
import math
from pathlib import Path
from typing import Any
from typing import Any, TypedDict
try:
import gradio as gr
@@ -23,16 +24,49 @@ except ImportError:
raise ImportError("gradio is required to run this demo. Install it with: pip install 'vllm-omni[demo]'") from None
DEFAULT_PROMPT = "A vibrant tropical fish swimming gracefully among colorful coral reefs in a clear, turquoise ocean. The fish has bright blue and yellow scales with a small, distinctive orange spot on its side, its fins moving fluidly. The coral reefs are alive with a variety of marine life, including small schools of colorful fish and sea turtles gliding by. The water is crystal clear, allowing for a view of the sandy ocean floor below. The reef itself is adorned with a mix of hard and soft corals in shades of red, orange, and green. The photo captures the fish from a slightly elevated angle, emphasizing its lively movements and the vivid colors of its surroundings. A close-up shot with dynamic movement."
DEFAULT_TRANSITION_CHUNKS = 3
PROMPT_UPDATE_HEADERS = ["At (s)", "Prompt", "Transition Chunks"]
PROMPT_UPDATE_VALUES = [
[
4,
(
"An underwater tornado appears and affects the ocean floor in a dramatic and chaotic scene. "
"The water is murky, swirling violently, carrying debris and marine life into the vortex. "
"The tropical fish on the scene all swim in panic, trying to avoid the powerful currents. "
"The camera remains stationary, capturing the intensity of the underwater tornado as it disrupts the serene ocean floor. "
"Close-up shot emphasizing the turbulent motion and destruction."
),
3,
],
[
11,
(
"The swirling underwater vortex now seizes a heavy, encrusted treasure chest, its lid flapping open as it is smashed onto the ocean floor. "
"Gold coins and silver trinkets spill out, glittering briefly in the murky water before being swept instantly into the violent funnel. "
"The heavy wooden box tumbles end over end, colliding with floating rocks and adding to the debris field. "
"Swirling sediment and bubbles surround the spilling fortune, highlighting the chaotic power of the storm as it ravages the seabed. "
"Close-up shot emphasizing the turbulent motion and destruction."
),
3,
],
]
VIDEO_STREAM_VIEW_HTML = Path(__file__).with_name("video-stream-view.html")
VIDEO_STREAM_VIEW_JS = Path(__file__).with_name("video-stream-view.js")
class ScheduledPromptUpdate(TypedDict):
at: float
prompt: str
transition_chunks: int
def _maybe_set(payload: dict[str, Any], key: str, value: Any) -> None:
if value is None:
return
if isinstance(value, str) and not value.strip():
return
payload[key] = value
if isinstance(value, str):
payload[key] = value.strip()
else:
payload[key] = value
def _optional_int(value: int | float | None) -> int | None:
@@ -43,11 +77,83 @@ def _optional_float(value: int | float | None) -> float | None:
return None if value is None else float(value)
def _normalize_prompt_update_row(row: list[Any] | tuple[Any, ...]) -> tuple[Any, Any, Any]:
values = []
for v in row[: len(PROMPT_UPDATE_HEADERS)]:
if isinstance(v, str):
values.append(v.strip() or None)
elif isinstance(v, float) and math.isnan(v):
values.append(None)
else:
values.append(v)
while len(values) < len(PROMPT_UPDATE_HEADERS):
values.append(None)
return values[0], values[1], values[2] # pyright: ignore[reportIndexIssue]
def _prompt_update_row_is_empty(row: list[Any] | tuple[Any, ...]) -> bool:
at, prompt, transition = _normalize_prompt_update_row(row)
return at is None and prompt is None and transition is None
def _parse_prompt_update_row(row: list[Any] | tuple[Any, ...], *, index: int) -> ScheduledPromptUpdate:
at_val, prompt_val, transition_val = _normalize_prompt_update_row(row)
row_label = f"Prompt update row {index + 1}"
if at_val is None or prompt_val is None:
raise gr.Error(f"{row_label} requires both At (s) and Prompt.")
try:
at = float(at_val)
except (TypeError, ValueError) as exc:
raise gr.Error(f"{row_label}: At (s) must be a number.") from exc
if at < 0:
raise gr.Error(f"{row_label}: At (s) must be >= 0.")
prompt = str(prompt_val).strip()
if not prompt:
raise gr.Error(f"{row_label}: Prompt must be a non-empty string.")
transition_chunks = DEFAULT_TRANSITION_CHUNKS
if transition_val is not None:
try:
transition_chunks = int(transition_val)
except (TypeError, ValueError) as exc:
raise gr.Error(f"{row_label}: Transition Chunks must be an integer.") from exc
if transition_chunks < 0:
raise gr.Error(f"{row_label}: Transition Chunks must be >= 0.")
return {
"at": at,
"prompt": prompt,
"transition_chunks": transition_chunks,
}
def _parse_prompt_updates_dataframe(rows: list[list[Any]] | None) -> list[ScheduledPromptUpdate]:
if not rows:
return []
# Single row: empty or complete.
if len(rows) == 1:
row = rows[0]
if _prompt_update_row_is_empty(row):
return []
return [_parse_prompt_update_row(row, index=0)]
updates: list[ScheduledPromptUpdate] = []
for index, row in enumerate(rows):
if _prompt_update_row_is_empty(row):
raise gr.Error(f"Prompt update row {index + 1} cannot be empty when multiple rows are present.")
updates.append(_parse_prompt_update_row(row, index=index))
return sorted(updates, key=lambda update: update["at"])
def _build_session_start(
*,
model: str,
prompt: str,
negative_prompt: str,
negative_prompt: str | None,
width: int | float | None,
height: int | float | None,
fps: int | float | None,
@@ -101,6 +207,7 @@ def build_browser_stream_config(
model: str,
prompt: str,
negative_prompt: str,
include_negative_prompt: bool,
width: int | float | None,
height: int | float | None,
fps: int | float,
@@ -118,6 +225,7 @@ def build_browser_stream_config(
flow_shift: int | float | None,
include_true_cfg_scale: bool,
true_cfg_scale: int | float | None,
prompt_updates_df: list[list[Any]] | None,
) -> tuple[str, Any]:
"""Build the browser-side WebSocket request config for the HTML/MSE player."""
if not model.strip():
@@ -128,7 +236,7 @@ def build_browser_stream_config(
payload = _build_session_start(
model=model.strip(),
prompt=prompt.strip(),
negative_prompt=negative_prompt,
negative_prompt=negative_prompt if include_negative_prompt else None,
width=width,
height=height,
fps=fps,
@@ -141,15 +249,21 @@ def build_browser_stream_config(
true_cfg_scale=true_cfg_scale if include_true_cfg_scale else None,
seed=seed if include_seed else None,
)
prompt_updates = _parse_prompt_updates_dataframe(prompt_updates_df)
config = {
"url": f"ws://{host}:{int(port)}/v1/realtime/video",
"payload": payload,
}
if prompt_updates:
config["prompt_updates"] = prompt_updates
return json.dumps(config, ensure_ascii=False), gr.update(value="Streaming...", interactive=False)
def create_demo() -> gr.Blocks:
with gr.Blocks(title="Streaming Video Generation") as demo:
with gr.Blocks(
title="Streaming Video Generation",
css="textarea:disabled, input:disabled { color: var(--body-text-color-subdued); -webkit-text-fill-color: var(--body-text-color-subdued); background-color: var(--background-fill-secondary); }",
) as demo:
gr.Markdown("# Streaming Video Generation")
gr.Markdown(
"Connects to `WS /v1/realtime/video` in the browser and appends fMP4 chunks "
@@ -162,7 +276,6 @@ def create_demo() -> gr.Blocks:
port = gr.Number(label="Server Port", value=8000, precision=0)
model = gr.Textbox(label="Model", value="BestWishYsh/Helios-Distilled")
prompt = gr.Textbox(label="Prompt", value=DEFAULT_PROMPT, lines=3)
negative_prompt = gr.Textbox(label="Negative Prompt", value="", lines=2)
with gr.Row():
width = gr.Number(label="Width", value=640, precision=0)
@@ -170,49 +283,66 @@ def create_demo() -> gr.Blocks:
with gr.Row():
fps = gr.Number(label="FPS", value=16, precision=0)
num_frames = gr.Number(label="Num Frames", value=132, precision=0)
num_frames = gr.Number(label="Num Frames", value=429, precision=0)
num_inference_steps = gr.Number(label="Num Inference Steps", value=50, precision=0)
with gr.Row():
with gr.Column():
guidance_scale = gr.Number(
label="Guidance Scale",
value=1.0,
interactive=True,
scale=4,
)
include_guidance_scale = gr.Checkbox(label="Send", value=True, scale=1, min_width=70)
with gr.Column():
guidance_scale_2 = gr.Number(
label="Guidance Scale 2",
value=None,
interactive=False,
scale=4,
)
include_guidance_scale_2 = gr.Checkbox(label="Send", value=False, scale=1, min_width=70)
with gr.Column():
true_cfg_scale = gr.Number(
label="True CFG Scale",
value=None,
interactive=False,
scale=4,
)
include_true_cfg_scale = gr.Checkbox(label="Send", value=False, scale=1, min_width=70)
# I want it to wrap after 3, but it auto wrap after 2. I'll rather fully delegate to auto wrapping.
with gr.Column():
boundary_ratio = gr.Number(
label="Boundary Ratio",
value=None,
interactive=False,
scale=4,
)
include_boundary_ratio = gr.Checkbox(label="Send", value=False, scale=1, min_width=70)
with gr.Column():
flow_shift = gr.Number(label="Flow Shift", value=None, interactive=False, scale=4)
include_flow_shift = gr.Checkbox(label="Send", value=False, scale=1, min_width=70)
with gr.Column():
seed = gr.Number(label="Seed", value=42, precision=0, interactive=True, scale=4)
include_seed = gr.Checkbox(label="Send", value=True, scale=1, min_width=70)
with gr.Accordion("Advanced Sampling", open=False):
with gr.Row():
with gr.Column():
negative_prompt = gr.Textbox(label="Negative Prompt", value="", lines=1, interactive=False)
include_negative_prompt = gr.Checkbox(label="Send", value=False)
with gr.Row():
with gr.Column(scale=0, min_width=150):
guidance_scale = gr.Number(
label="Guidance Scale",
value=1.0,
interactive=True,
)
include_guidance_scale = gr.Checkbox(label="Send", value=True)
with gr.Column(scale=0, min_width=150):
guidance_scale_2 = gr.Number(
label="Guidance Scale 2",
value=None,
interactive=False,
)
include_guidance_scale_2 = gr.Checkbox(label="Send", value=False)
with gr.Column(scale=0, min_width=150):
true_cfg_scale = gr.Number(
label="True CFG Scale",
value=None,
interactive=False,
)
include_true_cfg_scale = gr.Checkbox(label="Send", value=False)
with gr.Column(scale=0, min_width=150):
boundary_ratio = gr.Number(
label="Boundary Ratio",
value=None,
interactive=False,
)
include_boundary_ratio = gr.Checkbox(label="Send", value=False)
with gr.Column(scale=0, min_width=150):
flow_shift = gr.Number(label="Flow Shift", value=None, interactive=False)
include_flow_shift = gr.Checkbox(label="Send", value=False)
with gr.Column(scale=0, min_width=150):
seed = gr.Number(label="Seed", value=42, precision=0, interactive=True)
include_seed = gr.Checkbox(label="Send", value=True)
with gr.Accordion("Prompt Updates", open=True):
gr.Markdown(
"Schedule midway text `session.interaction` messages after `video.start`. "
"With one row, leave it fully empty or fill both **At (s)** and **Prompt**. "
"With multiple rows, every row must be complete."
)
prompt_updates_df = gr.Dataframe(
headers=PROMPT_UPDATE_HEADERS,
datatype=["number", "str", "number"],
type="array",
row_count=1,
row_limits=(1, None),
column_count=3,
value=PROMPT_UPDATE_VALUES,
interactive=True,
)
with gr.Row():
start_button = gr.Button("Start", variant="primary", elem_id="streaming-video-start")
@@ -231,6 +361,7 @@ def create_demo() -> gr.Blocks:
model,
prompt,
negative_prompt,
include_negative_prompt,
width,
height,
fps,
@@ -248,8 +379,10 @@ def create_demo() -> gr.Blocks:
flow_shift,
include_true_cfg_scale,
true_cfg_scale,
prompt_updates_df,
]
for toggle, field in [
(include_negative_prompt, negative_prompt),
(include_guidance_scale, guidance_scale),
(include_guidance_scale_2, guidance_scale_2),
(include_seed, seed),