megalado
commited on
Commit
·
d865c31
1
Parent(s):
bd590d5
Improve MDM integration for better animation quality
Browse files
app.py
CHANGED
|
@@ -1,206 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 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 |
-
motion[frame, 4, 2] = motion[frame, 0, 2] - 0.1
|
| 68 |
-
motion[frame, 5, 1] = 0.2
|
| 69 |
-
motion[frame, 5, 2] = motion[frame, 4, 2] - 0.5 + swing_leg_l * 0.3
|
| 70 |
-
motion[frame, 6, 1] = 0.2
|
| 71 |
-
motion[frame, 6, 2] = motion[frame, 5, 2] - 0.5 + swing_leg_l * 0.3
|
| 72 |
-
|
| 73 |
-
# Right leg (joints 7, 8, 9)
|
| 74 |
-
swing_leg_r = np.sin(t * 2 * np.pi * leg_freq + np.pi)
|
| 75 |
-
motion[frame, 7, 1] = -0.2
|
| 76 |
-
motion[frame, 7, 2] = motion[frame, 0, 2] - 0.1
|
| 77 |
-
motion[frame, 8, 1] = -0.2
|
| 78 |
-
motion[frame, 8, 2] = motion[frame, 7, 2] - 0.5 + swing_leg_r * 0.3
|
| 79 |
-
motion[frame, 9, 1] = -0.2
|
| 80 |
-
motion[frame, 9, 2] = motion[frame, 8, 2] - 0.5 + swing_leg_r * 0.3
|
| 81 |
-
|
| 82 |
-
# Left arm (joints 10, 11, 12)
|
| 83 |
-
if waving and t > 0.3 and t < 0.7:
|
| 84 |
-
# Waving motion
|
| 85 |
-
wave = 0.5 * np.sin(t * 2 * np.pi * 8)
|
| 86 |
-
motion[frame, 10, 1] = 0.3
|
| 87 |
-
motion[frame, 10, 2] = motion[frame, 3, 2] - 0.2
|
| 88 |
-
motion[frame, 11, 1] = 0.5
|
| 89 |
-
motion[frame, 11, 2] = motion[frame, 10, 2]
|
| 90 |
-
motion[frame, 12, 1] = 0.7
|
| 91 |
-
motion[frame, 12, 2] = motion[frame, 11, 2] + wave
|
| 92 |
-
else:
|
| 93 |
-
# Normal arm swing
|
| 94 |
-
swing_arm_l = np.sin(t * 2 * np.pi * leg_freq + np.pi)
|
| 95 |
-
motion[frame, 10, 1] = 0.3
|
| 96 |
-
motion[frame, 10, 2] = motion[frame, 3, 2] - 0.2
|
| 97 |
-
motion[frame, 11, 1] = 0.3 + swing_arm_l * 0.2
|
| 98 |
-
motion[frame, 11, 2] = motion[frame, 10, 2] - 0.4
|
| 99 |
-
motion[frame, 12, 1] = 0.3 + swing_arm_l * 0.4
|
| 100 |
-
motion[frame, 12, 2] = motion[frame, 11, 2] - 0.4
|
| 101 |
-
|
| 102 |
-
# Right arm (joints 13, 14, 15)
|
| 103 |
-
swing_arm_r = np.sin(t * 2 * np.pi * leg_freq)
|
| 104 |
-
motion[frame, 13, 1] = -0.3
|
| 105 |
-
motion[frame, 13, 2] = motion[frame, 3, 2] - 0.2
|
| 106 |
-
motion[frame, 14, 1] = -0.3 + swing_arm_r * 0.2
|
| 107 |
-
motion[frame, 14, 2] = motion[frame, 13, 2] - 0.4
|
| 108 |
-
motion[frame, 15, 1] = -0.3 + swing_arm_r * 0.4
|
| 109 |
-
motion[frame, 15, 2] = motion[frame, 14, 2] - 0.4
|
| 110 |
-
|
| 111 |
-
# Create figure
|
| 112 |
-
fig = plt.figure(figsize=(10, 6))
|
| 113 |
-
ax = fig.add_subplot(111, projection='3d')
|
| 114 |
-
|
| 115 |
-
# Define connections between joints
|
| 116 |
-
connections = [
|
| 117 |
-
(0, 1), (1, 2), (2, 3), # Spine and head
|
| 118 |
-
(0, 4), (4, 5), (5, 6), # Left leg
|
| 119 |
-
(0, 7), (7, 8), (8, 9), # Right leg
|
| 120 |
-
(3, 10), (10, 11), (11, 12), # Left arm
|
| 121 |
-
(3, 13), (13, 14), (14, 15) # Right arm
|
| 122 |
]
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
def update(frame):
|
| 126 |
-
ax.clear()
|
| 127 |
-
|
| 128 |
-
# Set axis limits
|
| 129 |
-
max_range = max(4, np.max(np.abs(motion)))
|
| 130 |
-
ax.set_xlim([-max_range/2, max_range/2 + motion[frame, 0, 0]])
|
| 131 |
-
ax.set_ylim([-max_range/2, max_range/2])
|
| 132 |
-
ax.set_zlim([0, max_range])
|
| 133 |
-
|
| 134 |
-
# Set labels
|
| 135 |
-
ax.set_xlabel('X (forward)')
|
| 136 |
-
ax.set_ylabel('Y (sideways)')
|
| 137 |
-
ax.set_zlabel('Z (upward)')
|
| 138 |
-
|
| 139 |
-
# Plot joints
|
| 140 |
-
ax.scatter(motion[frame, :, 0],
|
| 141 |
-
motion[frame, :, 1],
|
| 142 |
-
motion[frame, :, 2], c='b', marker='o')
|
| 143 |
-
|
| 144 |
-
# Plot connections
|
| 145 |
-
for start, end in connections:
|
| 146 |
-
ax.plot([motion[frame, start, 0], motion[frame, end, 0]],
|
| 147 |
-
[motion[frame, start, 1], motion[frame, end, 1]],
|
| 148 |
-
[motion[frame, start, 2], motion[frame, end, 2]], 'r-')
|
| 149 |
-
|
| 150 |
-
# Add action type to title
|
| 151 |
-
action_type = ""
|
| 152 |
-
if running:
|
| 153 |
-
action_type = "Running"
|
| 154 |
-
elif walking:
|
| 155 |
-
action_type = "Walking"
|
| 156 |
-
elif jumping:
|
| 157 |
-
action_type = "Jumping"
|
| 158 |
-
elif dancing:
|
| 159 |
-
action_type = "Dancing"
|
| 160 |
-
elif turning:
|
| 161 |
-
action_type = "Turning"
|
| 162 |
-
elif waving:
|
| 163 |
-
action_type = "Waving"
|
| 164 |
-
else:
|
| 165 |
-
action_type = "Moving"
|
| 166 |
-
|
| 167 |
-
ax.set_title(action_type + " Motion - Frame " + str(frame))
|
| 168 |
-
return ax
|
| 169 |
-
|
| 170 |
-
# Create animation
|
| 171 |
-
anim = FuncAnimation(fig, update, frames=min(frames, 180), interval=1000/30)
|
| 172 |
-
|
| 173 |
-
# Save animation
|
| 174 |
-
anim.save(output_path, writer='ffmpeg', fps=30)
|
| 175 |
-
plt.close()
|
| 176 |
-
|
| 177 |
-
print(f"Animation saved to {output_path}")
|
| 178 |
-
return output_path
|
| 179 |
-
|
| 180 |
-
def text_to_motion(text_prompt, motion_length=3.0, seed=0):
|
| 181 |
-
"""Generate motion from text prompt"""
|
| 182 |
try:
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
print(traceback.format_exc())
|
| 189 |
return None
|
| 190 |
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
demo = gr.Interface(
|
| 193 |
fn=text_to_motion,
|
| 194 |
inputs=[
|
| 195 |
-
gr.Textbox(
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
],
|
| 199 |
outputs=gr.Video(label="Generated Motion"),
|
| 200 |
-
title="Motion
|
| 201 |
-
description=
|
|
|
|
|
|
|
|
|
|
| 202 |
)
|
| 203 |
|
| 204 |
-
#
|
|
|
|
|
|
|
|
|
|
| 205 |
if __name__ == "__main__":
|
| 206 |
-
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
"""
|
| 3 |
+
Motion Diffusion Demo on Hugging Face Spaces
|
| 4 |
+
-------------------------------------------
|
| 5 |
+
Generates human motion from a text prompt using the Motion‑Diffusion‑Model (MDM)
|
| 6 |
+
checkpoint that is already stored in this Space.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
import sys
|
| 12 |
+
import subprocess
|
| 13 |
+
import traceback
|
| 14 |
+
from pathlib import Path
|
| 15 |
+
from typing import Optional
|
| 16 |
+
|
| 17 |
import gradio as gr
|
| 18 |
+
|
| 19 |
+
# ---------------------------------------------------------------------------
|
| 20 |
+
# Configuration
|
| 21 |
+
# ---------------------------------------------------------------------------
|
| 22 |
+
|
| 23 |
+
REPO_DIR = "motion-diffusion-model" # cloned repo folder
|
| 24 |
+
CHECKPOINT_PATH = "checkpoints/opt000750000.pt" # unchanged (user‑supplied)
|
| 25 |
+
OUTPUT_DIR = "output" # where final MP4s are moved
|
| 26 |
+
MAX_LEN_SEC = 9.8 # model’s hard limit
|
| 27 |
+
|
| 28 |
+
# ---------------------------------------------------------------------------
|
| 29 |
+
# Setup helpers
|
| 30 |
+
# ---------------------------------------------------------------------------
|
| 31 |
+
|
| 32 |
+
def ensure_repo_ready() -> None:
|
| 33 |
+
"""Clone repo (first run only) and push it onto sys.path."""
|
| 34 |
+
if not Path(REPO_DIR).exists():
|
| 35 |
+
print("[setup] Cloning Motion‑Diffusion‑Model repo …")
|
| 36 |
+
subprocess.run(
|
| 37 |
+
[
|
| 38 |
+
"git",
|
| 39 |
+
"clone",
|
| 40 |
+
"https://github.com/GuyTevet/motion-diffusion-model.git",
|
| 41 |
+
REPO_DIR,
|
| 42 |
+
],
|
| 43 |
+
check=True,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
repo_abs = str(Path(REPO_DIR).resolve())
|
| 47 |
+
if repo_abs not in sys.path:
|
| 48 |
+
sys.path.insert(0, repo_abs)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# ---------------------------------------------------------------------------
|
| 52 |
+
# Core generation
|
| 53 |
+
# ---------------------------------------------------------------------------
|
| 54 |
+
|
| 55 |
+
def run_mdm(prompt: str, length: float, seed: int) -> Optional[str]:
|
| 56 |
+
"""Run sample/generate.py and return the produced MP4 (or None)."""
|
| 57 |
+
ensure_repo_ready()
|
| 58 |
+
|
| 59 |
+
ckpt = Path(CHECKPOINT_PATH).resolve()
|
| 60 |
+
if not ckpt.exists():
|
| 61 |
+
raise FileNotFoundError(f"Checkpoint not found: {ckpt}")
|
| 62 |
+
|
| 63 |
+
# Ensure output dir exists
|
| 64 |
+
Path(OUTPUT_DIR).mkdir(exist_ok=True)
|
| 65 |
+
|
| 66 |
+
# Script path is relative to repo root; we chdir into REPO_DIR later
|
| 67 |
+
script_path = Path("sample") / "generate.py"
|
| 68 |
+
if not (Path(REPO_DIR) / script_path).exists():
|
| 69 |
+
raise FileNotFoundError("sample/generate.py not found in the repo!")
|
| 70 |
+
|
| 71 |
+
# Assemble CLI
|
| 72 |
+
cmd = [
|
| 73 |
+
"python",
|
| 74 |
+
str(script_path),
|
| 75 |
+
"--model_path",
|
| 76 |
+
str(ckpt),
|
| 77 |
+
"--text_prompt",
|
| 78 |
+
prompt,
|
| 79 |
+
"--motion_length",
|
| 80 |
+
f"{min(length, MAX_LEN_SEC):.2f}",
|
| 81 |
+
"--seed",
|
| 82 |
+
str(seed),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
]
|
| 84 |
+
|
| 85 |
+
print("[run]", " ".join(cmd))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
try:
|
| 87 |
+
subprocess.run(cmd, cwd=REPO_DIR, check=True)
|
| 88 |
+
except subprocess.CalledProcessError as exc:
|
| 89 |
+
print("[error] sample/generate.py failed:", exc)
|
| 90 |
+
return None
|
| 91 |
+
|
| 92 |
+
# Locate newest MP4 in repo (generator writes to repo root/results/…)
|
| 93 |
+
mp4_files = list(Path(REPO_DIR).rglob("*.mp4"))
|
| 94 |
+
if not mp4_files:
|
| 95 |
+
print("[warn] No MP4 produced by generator.")
|
| 96 |
+
return None
|
| 97 |
+
|
| 98 |
+
newest = max(mp4_files, key=lambda p: p.stat().st_mtime)
|
| 99 |
+
final_path = Path(OUTPUT_DIR) / newest.name
|
| 100 |
+
newest.replace(final_path) # move instead of copy to save disk
|
| 101 |
+
|
| 102 |
+
print(f"[ok] Motion video saved to {final_path}")
|
| 103 |
+
return str(final_path)
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def text_to_motion(prompt: str, length: float = 3.0, seed: int = 0):
|
| 107 |
+
"""Wrapper called by Gradio UI."""
|
| 108 |
+
try:
|
| 109 |
+
return run_mdm(prompt, length, seed)
|
| 110 |
+
except Exception:
|
| 111 |
print(traceback.format_exc())
|
| 112 |
return None
|
| 113 |
|
| 114 |
+
|
| 115 |
+
# ---------------------------------------------------------------------------
|
| 116 |
+
# Gradio Interface
|
| 117 |
+
# ---------------------------------------------------------------------------
|
| 118 |
+
|
| 119 |
demo = gr.Interface(
|
| 120 |
fn=text_to_motion,
|
| 121 |
inputs=[
|
| 122 |
+
gr.Textbox(
|
| 123 |
+
label="Text Prompt",
|
| 124 |
+
lines=3,
|
| 125 |
+
value="A person walks forward and waves.",
|
| 126 |
+
),
|
| 127 |
+
gr.Slider(
|
| 128 |
+
minimum=1.0,
|
| 129 |
+
maximum=MAX_LEN_SEC,
|
| 130 |
+
step=0.1,
|
| 131 |
+
value=3.0,
|
| 132 |
+
label="Motion Length (seconds)",
|
| 133 |
+
),
|
| 134 |
+
gr.Number(label="Random Seed", value=0, precision=0),
|
| 135 |
],
|
| 136 |
outputs=gr.Video(label="Generated Motion"),
|
| 137 |
+
title="Motion Diffusion Model Demo (HumanML)",
|
| 138 |
+
description=(
|
| 139 |
+
"Enter an action description such as 'A person runs in a circle and jumps'.\n"
|
| 140 |
+
"The HumanML checkpoint renders a skeletal MP4."
|
| 141 |
+
),
|
| 142 |
)
|
| 143 |
|
| 144 |
+
# ---------------------------------------------------------------------------
|
| 145 |
+
# Launch
|
| 146 |
+
# ---------------------------------------------------------------------------
|
| 147 |
+
|
| 148 |
if __name__ == "__main__":
|
| 149 |
+
demo.launch()
|