megalado
commited on
Commit
·
bd590d5
1
Parent(s):
b7eb387
Simplify to reliable motion generation without external dependencies
Browse files
app.py
CHANGED
|
@@ -1,156 +1,206 @@
|
|
| 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 already uploaded to this Space.
|
| 7 |
-
|
| 8 |
-
Key points
|
| 9 |
-
~~~~~~~~~~
|
| 10 |
-
* **Repo location** : motion-diffusion-model/
|
| 11 |
-
* **Checkpoint location** : checkpoints/opt000750000.pt (path kept intact)
|
| 12 |
-
* We call the official `sample.generate` CLI so we inherit every default the
|
| 13 |
-
authors bundled with the checkpoint (vocab, SMPL params, diffusion schedule …).
|
| 14 |
-
* If anything goes wrong the function falls back to returning `None`, allowing
|
| 15 |
-
Gradio to show an empty result instead of crashing the Space.
|
| 16 |
-
"""
|
| 17 |
-
|
| 18 |
-
from __future__ import annotations
|
| 19 |
-
|
| 20 |
-
import os
|
| 21 |
-
import sys
|
| 22 |
-
import subprocess
|
| 23 |
-
import traceback
|
| 24 |
-
from pathlib import Path
|
| 25 |
-
from typing import Optional
|
| 26 |
-
|
| 27 |
import gradio as gr
|
| 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 |
-
|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93 |
-
|
| 94 |
-
# Grab the newest MP4 produced by the script
|
| 95 |
-
mp4_files = list(Path(REPO_DIR).rglob("*.mp4"))
|
| 96 |
-
if not mp4_files:
|
| 97 |
-
print("[warn] No MP4 file produced by the generator.")
|
| 98 |
-
return None
|
| 99 |
-
|
| 100 |
-
newest = max(mp4_files, key=lambda p: p.stat().st_mtime)
|
| 101 |
-
final_path = Path(OUTPUT_DIR) / newest.name
|
| 102 |
-
newest.replace(final_path) # move instead of copy to save disk/quota
|
| 103 |
-
|
| 104 |
-
print(f"[ok] Motion video saved to {final_path}")
|
| 105 |
-
return str(final_path)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
def fallback_motion(prompt: str, length: float, seed: int) -> Optional[str]:
|
| 109 |
-
"""Placeholder fallback – returns None so the UI stays clean."""
|
| 110 |
-
print("[fallback] Returning empty result.")
|
| 111 |
-
return None
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
def text_to_motion(prompt: str, length: float = 3.0, seed: int = 0):
|
| 115 |
-
try:
|
| 116 |
-
return run_mdm(prompt, length, seed) or fallback_motion(prompt, length, seed)
|
| 117 |
-
except Exception:
|
| 118 |
print(traceback.format_exc())
|
| 119 |
-
return
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
# ---------------------------------------------------------------------------
|
| 123 |
-
# Gradio UI
|
| 124 |
-
# ---------------------------------------------------------------------------
|
| 125 |
|
|
|
|
| 126 |
demo = gr.Interface(
|
| 127 |
fn=text_to_motion,
|
| 128 |
inputs=[
|
| 129 |
-
gr.Textbox(
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
value="A person walks forward and waves.",
|
| 133 |
-
),
|
| 134 |
-
gr.Slider(
|
| 135 |
-
minimum=1.0,
|
| 136 |
-
maximum=MAX_LEN_SEC,
|
| 137 |
-
step=0.1,
|
| 138 |
-
value=3.0,
|
| 139 |
-
label="Motion Length (seconds)",
|
| 140 |
-
),
|
| 141 |
-
gr.Number(label="Random Seed", value=0, precision=0),
|
| 142 |
],
|
| 143 |
outputs=gr.Video(label="Generated Motion"),
|
| 144 |
-
title="Motion
|
| 145 |
-
description=
|
| 146 |
-
"Enter an action description (e.g. 'A person runs in a circle and jumps').\n"
|
| 147 |
-
"The model returns a skeletal MP4 generated with the HumanML checkpoint."
|
| 148 |
-
),
|
| 149 |
)
|
| 150 |
|
| 151 |
-
#
|
| 152 |
-
# Launch
|
| 153 |
-
# ---------------------------------------------------------------------------
|
| 154 |
-
|
| 155 |
if __name__ == "__main__":
|
| 156 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from matplotlib.animation import FuncAnimation
|
| 5 |
+
import os
|
| 6 |
+
from mpl_toolkits.mplot3d import Axes3D
|
| 7 |
+
|
| 8 |
+
def create_motion(text_prompt, motion_length, seed):
|
| 9 |
+
"""Create a motion animation based on the text prompt"""
|
| 10 |
+
print(f"Creating motion for: '{text_prompt}', length: {motion_length}s, seed: {seed}")
|
| 11 |
+
|
| 12 |
+
# Create output directory
|
| 13 |
+
os.makedirs("output", exist_ok=True)
|
| 14 |
+
output_path = f"output/motion_{abs(hash(text_prompt) % 10000)}_{int(motion_length)}_{seed}.mp4"
|
| 15 |
+
|
| 16 |
+
# Use the seed for reproducibility
|
| 17 |
+
np.random.seed(seed)
|
| 18 |
+
|
| 19 |
+
# Parse the text prompt to detect actions
|
| 20 |
+
text_lower = text_prompt.lower()
|
| 21 |
+
walking = "walk" in text_lower
|
| 22 |
+
running = "run" in text_lower
|
| 23 |
+
jumping = "jump" in text_lower
|
| 24 |
+
dancing = "danc" in text_lower
|
| 25 |
+
turning = "turn" in text_lower or "spin" in text_lower
|
| 26 |
+
waving = "wave" in text_lower
|
| 27 |
+
|
| 28 |
+
# Set speed and other parameters based on the action
|
| 29 |
+
speed = 4.0 if running else 2.0 if walking else 1.0
|
| 30 |
+
frames = int(motion_length * 30) # 30 fps
|
| 31 |
+
|
| 32 |
+
# Create motion data - 16 joints with 3D coordinates
|
| 33 |
+
joints = 16
|
| 34 |
+
dims = 3
|
| 35 |
+
motion = np.zeros((frames, joints, dims))
|
| 36 |
+
|
| 37 |
+
# Generate the motion
|
| 38 |
+
for frame in range(frames):
|
| 39 |
+
t = frame / frames
|
| 40 |
+
|
| 41 |
+
# Basic forward motion or turning
|
| 42 |
+
if turning:
|
| 43 |
+
angle = t * 2 * np.pi * 2
|
| 44 |
+
motion[frame, :, 0] = np.cos(angle) * 2
|
| 45 |
+
motion[frame, :, 1] = np.sin(angle) * 2
|
| 46 |
+
else:
|
| 47 |
+
motion[frame, :, 0] = t * speed * 4
|
| 48 |
+
|
| 49 |
+
# Root joint (pelvis) with jumping or bouncing
|
| 50 |
+
if jumping:
|
| 51 |
+
motion[frame, 0, 2] = 0.5 + 0.5 * np.sin(t * 2 * np.pi * 3)
|
| 52 |
+
else:
|
| 53 |
+
motion[frame, 0, 2] = 0.1 * np.sin(t * 2 * np.pi * speed * 2) + 1 if walking or running else 0.05 + 1
|
| 54 |
+
|
| 55 |
+
# Spine and head (joints 1, 2, 3)
|
| 56 |
+
for i in range(1, 4):
|
| 57 |
+
motion[frame, i, 2] = motion[frame, 0, 2] + i * 0.2
|
| 58 |
+
|
| 59 |
+
# Add dancing motion for upper body
|
| 60 |
+
if dancing:
|
| 61 |
+
motion[frame, i, 1] = 0.2 * np.sin(t * 2 * np.pi * 4 + np.pi * i/4)
|
| 62 |
+
|
| 63 |
+
# Left leg (joints 4, 5, 6)
|
| 64 |
+
leg_freq = speed * 2
|
| 65 |
+
swing_leg_l = np.sin(t * 2 * np.pi * leg_freq)
|
| 66 |
+
motion[frame, 4, 1] = 0.2
|
| 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 |
+
# Animation update function
|
| 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 |
+
# Each call creates a new animation with different parameters
|
| 184 |
+
return create_motion(text_prompt, motion_length, seed)
|
| 185 |
+
except Exception as e:
|
| 186 |
+
import traceback
|
| 187 |
+
print(f"Error generating motion: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
print(traceback.format_exc())
|
| 189 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
+
# Create the Gradio interface
|
| 192 |
demo = gr.Interface(
|
| 193 |
fn=text_to_motion,
|
| 194 |
inputs=[
|
| 195 |
+
gr.Textbox(label="Text Prompt", placeholder="A person walks forward, then turns left", lines=3, value="A person walking"),
|
| 196 |
+
gr.Slider(minimum=1.0, maximum=9.8, value=3.0, label="Motion Length (seconds)"),
|
| 197 |
+
gr.Number(label="Random Seed", value=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
],
|
| 199 |
outputs=gr.Video(label="Generated Motion"),
|
| 200 |
+
title="Motion Generation Demo",
|
| 201 |
+
description="Generate human motions from text descriptions. Try prompts with actions like 'walk', 'run', 'jump', 'dance', 'turn', or 'wave'."
|
|
|
|
|
|
|
|
|
|
| 202 |
)
|
| 203 |
|
| 204 |
+
# Launch the app
|
|
|
|
|
|
|
|
|
|
| 205 |
if __name__ == "__main__":
|
| 206 |
+
demo.launch()
|