Spaces:
Running on Zero
Running on Zero
Deploy ARDY ZeroGPU Blender motion API
Browse files- app.py +125 -0
- tests/test_timeline_utils.py +43 -0
- timeline_utils.py +82 -0
app.py
CHANGED
|
@@ -576,6 +576,94 @@ def generate_blender_motion(
|
|
| 576 |
return str(bvh_path), str(npz_path), str(metadata_path), seed
|
| 577 |
|
| 578 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 579 |
# -----------------------------------------------------------------------------
|
| 580 |
# Front-end: a self-contained Three.js playground, delivered as a Gradio-native
|
| 581 |
# custom HTML component (Gradio 6 `gr.HTML` templates + js_on_load).
|
|
@@ -1060,6 +1148,35 @@ with gr.Blocks() as demo:
|
|
| 1060 |
npz_file = gr.File(label="ARDY NPZ")
|
| 1061 |
metadata_file = gr.File(label="Metadata")
|
| 1062 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1063 |
gr.Examples(
|
| 1064 |
examples=EXAMPLES,
|
| 1065 |
inputs=[prompt, rig, duration],
|
|
@@ -1094,10 +1211,18 @@ with gr.Blocks() as demo:
|
|
| 1094 |
outputs=[bvh_file, npz_file, metadata_file, seed],
|
| 1095 |
api_name=False,
|
| 1096 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1097 |
|
| 1098 |
# Clean single-shot endpoint for the HTTP API / MCP tool (no session state).
|
| 1099 |
gr.api(generate_motion, api_name="generate")
|
| 1100 |
gr.api(generate_blender_motion, api_name="generate_blender")
|
|
|
|
| 1101 |
|
| 1102 |
demo.queue()
|
| 1103 |
|
|
|
|
| 576 |
return str(bvh_path), str(npz_path), str(metadata_path), seed
|
| 577 |
|
| 578 |
|
| 579 |
+
@spaces.GPU
|
| 580 |
+
def generate_timeline_motion(
|
| 581 |
+
timeline,
|
| 582 |
+
diffusion_steps: int = NUM_BASE_STEPS,
|
| 583 |
+
cfg_weight: float = 2.0,
|
| 584 |
+
seed: int = 0,
|
| 585 |
+
randomize_seed: bool = True,
|
| 586 |
+
) -> tuple[str, str, str, int]:
|
| 587 |
+
"""Generate sequential prompts as one autoregressively continuous motion."""
|
| 588 |
+
from timeline_utils import normalize_timeline
|
| 589 |
+
|
| 590 |
+
r = RIGS["human"]
|
| 591 |
+
segments = normalize_timeline(timeline, fps=float(r["fps"]))
|
| 592 |
+
motion_np = None
|
| 593 |
+
resolved_seed = int(seed)
|
| 594 |
+
|
| 595 |
+
for index, segment in enumerate(segments):
|
| 596 |
+
_, resolved_seed, motion_np = _core_generate(
|
| 597 |
+
"human",
|
| 598 |
+
segment["prompt"],
|
| 599 |
+
segment["duration_seconds"],
|
| 600 |
+
diffusion_steps,
|
| 601 |
+
cfg_weight,
|
| 602 |
+
resolved_seed,
|
| 603 |
+
bool(randomize_seed) if index == 0 else False,
|
| 604 |
+
motion_np,
|
| 605 |
+
)
|
| 606 |
+
|
| 607 |
+
if motion_np is None:
|
| 608 |
+
raise gr.Error("The timeline did not contain any motion segments.")
|
| 609 |
+
|
| 610 |
+
motion_tensor = torch.from_numpy(motion_np).to("cuda")
|
| 611 |
+
out = _decode_motion("human", motion_tensor)
|
| 612 |
+
skeleton = r["skeleton"]
|
| 613 |
+
joint_names = list(skeleton.bone_order_names)
|
| 614 |
+
parents = list(r["parents"])
|
| 615 |
+
rest_positions = to_numpy(skeleton.neutral_joints)
|
| 616 |
+
|
| 617 |
+
output_dir = Path(tempfile.mkdtemp(prefix="ardy_timeline_"))
|
| 618 |
+
npz_path = output_dir / "ardy_timeline_motion.npz"
|
| 619 |
+
bvh_path = output_dir / "ardy_timeline_motion.bvh"
|
| 620 |
+
metadata_path = output_dir / "timeline_metadata.json"
|
| 621 |
+
|
| 622 |
+
timeline_json = json.dumps(segments, ensure_ascii=False)
|
| 623 |
+
np.savez_compressed(
|
| 624 |
+
npz_path,
|
| 625 |
+
**out,
|
| 626 |
+
fps=np.asarray(r["fps"], dtype=np.float32),
|
| 627 |
+
text=np.asarray(timeline_json),
|
| 628 |
+
seed=np.asarray(resolved_seed, dtype=np.int64),
|
| 629 |
+
rig=np.asarray("human"),
|
| 630 |
+
joint_names=np.asarray(joint_names),
|
| 631 |
+
parents=np.asarray(parents, dtype=np.int32),
|
| 632 |
+
rest_positions=np.asarray(rest_positions, dtype=np.float32),
|
| 633 |
+
)
|
| 634 |
+
write_bvh(
|
| 635 |
+
bvh_path,
|
| 636 |
+
joint_names=joint_names,
|
| 637 |
+
parents=parents,
|
| 638 |
+
rest_positions=rest_positions,
|
| 639 |
+
local_rot_mats=out["local_rot_mats"],
|
| 640 |
+
root_positions=out["root_positions"],
|
| 641 |
+
fps=r["fps"],
|
| 642 |
+
)
|
| 643 |
+
metadata_path.write_text(
|
| 644 |
+
json.dumps(
|
| 645 |
+
{
|
| 646 |
+
"model": "nvidia/ARDY-Core-RP-20FPS-Horizon40",
|
| 647 |
+
"rig": "human",
|
| 648 |
+
"seed": resolved_seed,
|
| 649 |
+
"fps": r["fps"],
|
| 650 |
+
"frames": int(out["posed_joints"].shape[0]),
|
| 651 |
+
"duration_seconds": float(out["posed_joints"].shape[0]) / float(r["fps"]),
|
| 652 |
+
"segments": segments,
|
| 653 |
+
"joints": joint_names,
|
| 654 |
+
"parents": parents,
|
| 655 |
+
"coordinate_system": "Y-up, metric",
|
| 656 |
+
"generation_mode": "autoregressive timeline",
|
| 657 |
+
"post_processing": False,
|
| 658 |
+
},
|
| 659 |
+
ensure_ascii=False,
|
| 660 |
+
indent=2,
|
| 661 |
+
),
|
| 662 |
+
encoding="utf-8",
|
| 663 |
+
)
|
| 664 |
+
return str(bvh_path), str(npz_path), str(metadata_path), resolved_seed
|
| 665 |
+
|
| 666 |
+
|
| 667 |
# -----------------------------------------------------------------------------
|
| 668 |
# Front-end: a self-contained Three.js playground, delivered as a Gradio-native
|
| 669 |
# custom HTML component (Gradio 6 `gr.HTML` templates + js_on_load).
|
|
|
|
| 1148 |
npz_file = gr.File(label="ARDY NPZ")
|
| 1149 |
metadata_file = gr.File(label="Metadata")
|
| 1150 |
|
| 1151 |
+
with gr.Accordion("Continuous action timeline", open=True):
|
| 1152 |
+
gr.Markdown(
|
| 1153 |
+
"Add one short instruction per row. ARDY carries the previous pose and "
|
| 1154 |
+
"root trajectory into the next row, producing one continuous motion. "
|
| 1155 |
+
"The combined duration may not exceed 8 seconds."
|
| 1156 |
+
)
|
| 1157 |
+
timeline_input = gr.Dataframe(
|
| 1158 |
+
headers=["Action prompt", "Duration (seconds)"],
|
| 1159 |
+
datatype=["str", "number"],
|
| 1160 |
+
type="array",
|
| 1161 |
+
row_count=(4, "dynamic"),
|
| 1162 |
+
col_count=(2, "fixed"),
|
| 1163 |
+
value=[
|
| 1164 |
+
["Walk forward two steps.", 2.0],
|
| 1165 |
+
["Turn around smoothly.", 2.0],
|
| 1166 |
+
["Wave the right hand.", 2.0],
|
| 1167 |
+
["Sit down on the ground.", 2.0],
|
| 1168 |
+
],
|
| 1169 |
+
label="Action segments",
|
| 1170 |
+
)
|
| 1171 |
+
generate_timeline = gr.Button(
|
| 1172 |
+
"Generate continuous timeline",
|
| 1173 |
+
variant="primary",
|
| 1174 |
+
)
|
| 1175 |
+
with gr.Row():
|
| 1176 |
+
timeline_bvh = gr.File(label="Continuous Blender BVH")
|
| 1177 |
+
timeline_npz = gr.File(label="Continuous ARDY NPZ")
|
| 1178 |
+
timeline_metadata = gr.File(label="Timeline metadata")
|
| 1179 |
+
|
| 1180 |
gr.Examples(
|
| 1181 |
examples=EXAMPLES,
|
| 1182 |
inputs=[prompt, rig, duration],
|
|
|
|
| 1211 |
outputs=[bvh_file, npz_file, metadata_file, seed],
|
| 1212 |
api_name=False,
|
| 1213 |
)
|
| 1214 |
+
generate_timeline.click(
|
| 1215 |
+
fn=generate_timeline_motion,
|
| 1216 |
+
inputs=[timeline_input, diffusion_steps, cfg_weight, seed, randomize_seed],
|
| 1217 |
+
outputs=[timeline_bvh, timeline_npz, timeline_metadata, seed],
|
| 1218 |
+
api_name=False,
|
| 1219 |
+
concurrency_limit=1,
|
| 1220 |
+
)
|
| 1221 |
|
| 1222 |
# Clean single-shot endpoint for the HTTP API / MCP tool (no session state).
|
| 1223 |
gr.api(generate_motion, api_name="generate")
|
| 1224 |
gr.api(generate_blender_motion, api_name="generate_blender")
|
| 1225 |
+
gr.api(generate_timeline_motion, api_name="generate_timeline")
|
| 1226 |
|
| 1227 |
demo.queue()
|
| 1228 |
|
tests/test_timeline_utils.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import unittest
|
| 2 |
+
|
| 3 |
+
from timeline_utils import normalize_timeline
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
class TimelineValidationTests(unittest.TestCase):
|
| 7 |
+
def test_builds_contiguous_frame_ranges(self):
|
| 8 |
+
result = normalize_timeline(
|
| 9 |
+
[
|
| 10 |
+
["Walk forward two steps.", 2],
|
| 11 |
+
["Turn around.", 1.5],
|
| 12 |
+
["Wave.", 0.5],
|
| 13 |
+
],
|
| 14 |
+
fps=20,
|
| 15 |
+
)
|
| 16 |
+
self.assertEqual(result[0]["start_frame"], 0)
|
| 17 |
+
self.assertEqual(result[0]["end_frame"], 40)
|
| 18 |
+
self.assertEqual(result[1]["start_frame"], 40)
|
| 19 |
+
self.assertEqual(result[1]["end_frame"], 70)
|
| 20 |
+
self.assertEqual(result[2]["start_frame"], 70)
|
| 21 |
+
self.assertEqual(result[2]["end_frame"], 80)
|
| 22 |
+
|
| 23 |
+
def test_ignores_blank_rows(self):
|
| 24 |
+
result = normalize_timeline(
|
| 25 |
+
[["Stand still.", 1], ["", None]],
|
| 26 |
+
fps=20,
|
| 27 |
+
)
|
| 28 |
+
self.assertEqual(len(result), 1)
|
| 29 |
+
|
| 30 |
+
def test_rejects_more_than_eight_seconds(self):
|
| 31 |
+
with self.assertRaisesRegex(ValueError, "may not exceed 8 seconds"):
|
| 32 |
+
normalize_timeline(
|
| 33 |
+
[["Walk.", 4], ["Wave.", 4], ["Sit.", 1]],
|
| 34 |
+
fps=20,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
def test_rejects_missing_prompt(self):
|
| 38 |
+
with self.assertRaisesRegex(ValueError, "action prompt"):
|
| 39 |
+
normalize_timeline([["", 2]], fps=20)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
unittest.main()
|
timeline_utils.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Validation helpers for ARDY's continuous action timeline."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import math
|
| 6 |
+
from typing import Any
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
MIN_SEGMENT_SECONDS = 0.5
|
| 10 |
+
MAX_SEGMENT_SECONDS = 4.0
|
| 11 |
+
MAX_TOTAL_SECONDS = 8.0
|
| 12 |
+
MAX_SEGMENTS = 8
|
| 13 |
+
MAX_PROMPT_CHARS = 500
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def _rows(value: Any) -> list:
|
| 17 |
+
if value is None:
|
| 18 |
+
return []
|
| 19 |
+
if hasattr(value, "values"):
|
| 20 |
+
return value.values.tolist()
|
| 21 |
+
if hasattr(value, "tolist") and not isinstance(value, list):
|
| 22 |
+
return value.tolist()
|
| 23 |
+
return list(value)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def normalize_timeline(value: Any, *, fps: float) -> list[dict]:
|
| 27 |
+
"""Normalize Gradio table rows and calculate exact frame ranges."""
|
| 28 |
+
if not math.isfinite(float(fps)) or float(fps) <= 0:
|
| 29 |
+
raise ValueError("FPS must be greater than zero.")
|
| 30 |
+
|
| 31 |
+
clean_rows = []
|
| 32 |
+
for row in _rows(value):
|
| 33 |
+
if row is None:
|
| 34 |
+
continue
|
| 35 |
+
row = list(row)
|
| 36 |
+
prompt = " ".join(str(row[0] if row else "").split())
|
| 37 |
+
duration_value = row[1] if len(row) > 1 else None
|
| 38 |
+
if not prompt and duration_value in (None, ""):
|
| 39 |
+
continue
|
| 40 |
+
if not prompt:
|
| 41 |
+
raise ValueError("Every timeline row must contain an action prompt.")
|
| 42 |
+
if len(prompt) > MAX_PROMPT_CHARS:
|
| 43 |
+
raise ValueError(
|
| 44 |
+
f"Each action prompt must contain at most {MAX_PROMPT_CHARS} characters."
|
| 45 |
+
)
|
| 46 |
+
try:
|
| 47 |
+
duration = float(duration_value)
|
| 48 |
+
except (TypeError, ValueError) as exc:
|
| 49 |
+
raise ValueError(
|
| 50 |
+
f"Duration for '{prompt}' must be a number."
|
| 51 |
+
) from exc
|
| 52 |
+
if not math.isfinite(duration) or not MIN_SEGMENT_SECONDS <= duration <= MAX_SEGMENT_SECONDS:
|
| 53 |
+
raise ValueError(
|
| 54 |
+
f"Each segment must be between {MIN_SEGMENT_SECONDS:g} "
|
| 55 |
+
f"and {MAX_SEGMENT_SECONDS:g} seconds."
|
| 56 |
+
)
|
| 57 |
+
clean_rows.append((prompt, duration))
|
| 58 |
+
|
| 59 |
+
if not clean_rows:
|
| 60 |
+
raise ValueError("Add at least one action to the timeline.")
|
| 61 |
+
if len(clean_rows) > MAX_SEGMENTS:
|
| 62 |
+
raise ValueError(f"The timeline supports at most {MAX_SEGMENTS} segments.")
|
| 63 |
+
if sum(duration for _, duration in clean_rows) > MAX_TOTAL_SECONDS + 1e-6:
|
| 64 |
+
raise ValueError(
|
| 65 |
+
f"The combined timeline may not exceed {MAX_TOTAL_SECONDS:g} seconds."
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
segments = []
|
| 69 |
+
start_frame = 0
|
| 70 |
+
for prompt, duration in clean_rows:
|
| 71 |
+
frame_count = max(1, int(round(duration * float(fps))))
|
| 72 |
+
end_frame = start_frame + frame_count
|
| 73 |
+
segments.append(
|
| 74 |
+
{
|
| 75 |
+
"prompt": prompt,
|
| 76 |
+
"duration_seconds": frame_count / float(fps),
|
| 77 |
+
"start_frame": start_frame,
|
| 78 |
+
"end_frame": end_frame,
|
| 79 |
+
}
|
| 80 |
+
)
|
| 81 |
+
start_frame = end_frame
|
| 82 |
+
return segments
|