Spaces:
Running on Zero
Running on Zero
File size: 10,828 Bytes
2001557 7580dd2 2001557 7580dd2 2001557 7580dd2 2001557 09f33ca 2001557 e4159cf d346bdc 2001557 d346bdc 2001557 d346bdc 2001557 d346bdc c142fc6 d346bdc c142fc6 d346bdc 2001557 d346bdc c142fc6 d346bdc c142fc6 d346bdc 2001557 d346bdc c142fc6 d346bdc c142fc6 d346bdc c142fc6 d346bdc c142fc6 d346bdc c142fc6 d346bdc 2001557 d346bdc 2001557 d346bdc c142fc6 d346bdc 2001557 d346bdc 7580dd2 3ec40ad 287a7db 7580dd2 502143b d346bdc 502143b 7580dd2 09f33ca 7580dd2 d346bdc 7580dd2 502143b 3ec40ad 502143b e0fb844 09f33ca e0fb844 09f33ca 502143b 287a7db 502143b 7580dd2 d346bdc 326f401 7580dd2 287a7db | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | """Gradio and ZeroGPU wiring for the 4DAnyone Rerun streaming runtime.
The four-link event chain is ``begin -> prepare_cpu -> run_gpu -> publish_cpu``.
ZeroGPU runs decorated callbacks in forked children, so each link creates its
own recording after ``rr.cleanup_if_forked_child()`` and uses the shared token
as its recording ID. The merged ``run_gpu`` remains one allocation because its
motion result contains live state that cannot cross a process boundary.
"""
from __future__ import annotations
import logging
from collections.abc import Iterator
from dataclasses import dataclass
from pathlib import Path
from typing import Any
import gradio as gr
import rerun as rr
import spaces
from gradio_rerun import Rerun
from fdanyone.errors import FourDAnyoneError
from fdanyone.rerun_streaming import (
APPLICATION_ID,
EXAMPLE_DIR,
GPU_DURATION,
STREAM_SMOKE,
RunSpec,
Session,
begin_phase,
generate_phase,
motion_phase,
new_spec,
publish_phase,
request_stop,
smoke_generate_phase,
smoke_motion_phase,
smoke_publish_phase,
source_phase,
)
@dataclass(frozen=True)
class Link:
"""One chain link's own recording, and the sink its bytes are drained from."""
recording: rr.RecordingStream
"""Explicit stream this link and its pipeline hooks log through."""
stream: rr.BinaryStream
"""Binary sink feeding the Gradio viewer."""
def read(self) -> bytes | None:
"""Return bytes buffered since the last read."""
return self.stream.read()
def open_link(token: str) -> Link:
"""Open this callback's own recording, in whatever process the callback runs in.
Called first thing in every link, GPU ones included, where "this callback"
means the forked child. A stream inherited across the fork cannot be used:
the SDK compares pids on flush and raises "Fork detected during flush". A
stream built here belongs to this process, and sharing ``token`` as the
recording id is what makes the viewer treat all four links as one recording.
``cleanup_if_forked_child`` drops whatever streams were inherited. The SDK
registers it with ``os.register_at_fork`` already, and ``rr.init`` calls it
outright for the same reason, so this is belt and braces and a no-op outside
a child. It is called unconditionally: guarding it on the name existing
would turn a rename in the SDK into a silent return of the very bug this
function exists to prevent.
"""
rr.cleanup_if_forked_child()
recording: rr.RecordingStream = rr.RecordingStream(APPLICATION_ID, recording_id=token)
return Link(recording=recording, stream=recording.binary_stream())
def begin(
video: str | None, start_time: float, seed: int
) -> Iterator[tuple[RunSpec, bytes | None, str, Any]]:
"""Validate the input on CPU, open a recording, and switch to the outputs."""
if video is None:
raise gr.Error("Upload a video, or pick the bundled example.")
try:
spec: RunSpec = new_spec(Path(video), float(start_time), int(seed))
except FourDAnyoneError as exc:
raise gr.Error(str(exc)) from None
link: Link = open_link(spec.token)
begin_phase(link.recording, spec)
yield spec, link.read(), "Preparing the source clip.", gr.Tabs(selected="outputs")
def prepare_cpu(spec: RunSpec) -> Iterator[tuple[bytes | None, str]]:
"""Put the source clip on the frame timeline before any GPU work starts."""
link: Link = open_link(spec.token)
label: str = source_phase(link.recording, spec)
yield link.read(), label
@spaces.GPU(duration=GPU_DURATION)
def run_gpu(spec: RunSpec) -> Iterator[tuple[dict | None, bytes | None, str]]:
"""Recover the motion and generate the six views, inside one allocation.
The two phases have to share a process. ``PreparedRun`` carries the decoded
clip, the open conditioning artifacts, and a completion barrier for the
skeleton renderer, so it cannot be pickled from one ZeroGPU worker into
another — and every ``@spaces.GPU`` function gets a worker of its own.
Everything in here runs in the forked child, including ``open_link``: this
is the only process that may build the recording it logs through.
"""
link: Link = open_link(spec.token)
session: Session = Session(spec)
try:
motion: Iterator[str] = (
smoke_motion_phase(link.recording, session)
if STREAM_SMOKE
else motion_phase(link.recording, session)
)
for label in motion:
yield None, link.read(), label
generate: Iterator[str] = (
smoke_generate_phase(link.recording, session)
if STREAM_SMOKE
else generate_phase(link.recording, session)
)
for label in generate:
yield None, link.read(), label
except FourDAnyoneError as exc:
raise gr.Error(str(exc)) from None
yield session.summary, link.read(), "Publishing the result."
def publish_cpu(spec: RunSpec, summary: dict | None) -> Iterator[tuple[bytes | None, str]]:
"""Attach the finished rig and its six videos, off the GPU allocation."""
if summary is None:
raise gr.Error("The generation phase did not publish a result.")
link: Link = open_link(spec.token)
label: str = (
smoke_publish_phase(link.recording, spec)
if STREAM_SMOKE
else publish_phase(link.recording, spec, summary)
)
yield link.read(), label
DESCRIPTION: str = """
# 4DAnyone × Rerun
One monocular clip of a person becomes six synchronized novel views. GVHMR
recovers the SMPL-X motion, a Wan 2.2 diffusion transformer generates every
view, and each phase streams into the Rerun viewer as it happens.
"""
SOURCE_CLIP_HEIGHT: int = 360
"""Display height of the source preview. A portrait clip scaled to the column
width is taller than the fold, which pushes every control below it out of sight."""
STATUS_CSS: str = """
#run-status {
display: flex;
flex-direction: column;
justify-content: center;
/* Tall enough for the progress overlay Gradio draws over a running event. */
min-height: 5rem;
/* Gradio writes `overflow: auto` inline on every block. */
overflow: visible !important;
padding: 0.75rem 1rem;
border-radius: var(--radius-lg);
background: var(--background-fill-secondary);
}
#run-status p {
font-size: 1.15rem;
line-height: 1.5;
margin: 0;
}
"""
"""Banner styling for the run status, the only readout of a multi-minute run.
Gradio otherwise gives a Markdown block the body font and a height its own text
overflows, which turns the line into a scrolling sliver. Gradio 6 takes ``css``
on ``launch``, not on the ``Blocks`` constructor."""
def build_demo() -> gr.Blocks:
"""Assemble the persistent viewer, the inputs, and the run chain.
Controls sit in a narrow column and the viewer fills a wide one beside it,
the same split every other Rerun Space here uses. Stacking them instead
pushes the viewer off the fold on a laptop, and the viewer is the demo. It
stays outside the tabs for the same reason it is created once: a tab switch
would tear down the component and drop the stream mid-run.
The chain is joined by ``success`` rather than ``then``: each link's inputs
are the previous link's outputs, so a link that failed leaves the next one
nothing to work with, and running it anyway would bury the real error under
a second, meaningless one.
"""
# Serve the example clips in place; without this Gradio copies all 209 MB
# of them into its temp cache at construction.
gr.set_static_paths([EXAMPLE_DIR])
with gr.Blocks(title="4DAnyone × Rerun") as demo:
gr.Markdown(DESCRIPTION)
spec: gr.State = gr.State(None)
summary: gr.State = gr.State(None)
with gr.Row():
with gr.Column(scale=1):
with gr.Tabs() as tabs:
with gr.Tab("Input", id="input"):
video: gr.Video = gr.Video(
label="Source clip", sources=["upload"], height=SOURCE_CLIP_HEIGHT
)
start_time: gr.Number = gr.Number(
value=0.0, label="Start time (seconds)", minimum=0.0
)
seed: gr.Number = gr.Number(value=0, label="Seed", precision=0, minimum=0)
# A single input keeps the compact thumbnail gallery; a
# second column would turn it into a labeled table.
gr.Examples(
examples=[[str(clip)] for clip in sorted(EXAMPLE_DIR.glob("*.mp4"))],
inputs=[video],
cache_examples=False,
examples_per_page=4,
)
with gr.Tab("Outputs", id="outputs"):
gr.Markdown(
"The viewer beside this switches layout with the run: detections on the "
"source clip, then a grid of per-step previews, then the finished rig "
"playing on a loop."
)
with gr.Row():
run_button: gr.Button = gr.Button("Run", variant="primary")
stop_button: gr.Button = gr.Button("Stop", variant="stop")
status: gr.Markdown = gr.Markdown(
"Upload a clip, or pick the example, then press Run.", elem_id="run-status"
)
with gr.Column(scale=3):
viewer: Rerun = Rerun(
streaming=True,
height=760,
panel_states={"time": "collapsed", "blueprint": "hidden", "selection": "hidden"},
)
started = run_button.click(begin, [video, start_time, seed], [spec, viewer, status, tabs])
prepared = started.success(prepare_cpu, spec, [viewer, status])
generated = prepared.success(run_gpu, spec, [summary, viewer, status])
published = generated.success(publish_cpu, [spec, summary], [viewer, status])
# `cancels` detaches the UI at once, but the pipeline itself only halts
# when it reads the sentinel `request_stop` writes: the worker thread
# (and on ZeroGPU, the forked child) never sees a Gradio cancellation.
stop_button.click(
request_stop, spec, status, cancels=[started, prepared, generated, published]
)
return demo
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="%(asctime)s | %(levelname)s | %(message)s")
build_demo().queue(default_concurrency_limit=1).launch(ssr_mode=False, css=STATUS_CSS)
|