megalado
commited on
Commit
·
96802a8
1
Parent(s):
d865c31
Improve MDM integration for better animation quality
Browse files
app.py
CHANGED
|
@@ -2,8 +2,14 @@
|
|
| 2 |
"""
|
| 3 |
Motion Diffusion Demo on Hugging Face Spaces
|
| 4 |
-------------------------------------------
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
"""
|
| 8 |
|
| 9 |
from __future__ import annotations
|
|
@@ -17,20 +23,19 @@ from typing import Optional
|
|
| 17 |
import gradio as gr
|
| 18 |
|
| 19 |
# ---------------------------------------------------------------------------
|
| 20 |
-
#
|
| 21 |
# ---------------------------------------------------------------------------
|
| 22 |
|
| 23 |
-
REPO_DIR = "motion-diffusion-model" # cloned repo folder
|
| 24 |
-
CHECKPOINT_PATH = "checkpoints/opt000750000.pt" #
|
| 25 |
-
OUTPUT_DIR = "output" #
|
| 26 |
-
MAX_LEN_SEC = 9.8 # model’s
|
| 27 |
|
| 28 |
# ---------------------------------------------------------------------------
|
| 29 |
-
#
|
| 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(
|
|
@@ -47,31 +52,24 @@ def ensure_repo_ready() -> None:
|
|
| 47 |
if repo_abs not in sys.path:
|
| 48 |
sys.path.insert(0, repo_abs)
|
| 49 |
|
| 50 |
-
|
| 51 |
# ---------------------------------------------------------------------------
|
| 52 |
-
# Core
|
| 53 |
# ---------------------------------------------------------------------------
|
| 54 |
|
| 55 |
def run_mdm(prompt: str, length: float, seed: int) -> Optional[str]:
|
| 56 |
-
"""
|
| 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 |
-
|
|
|
|
| 75 |
"--model_path",
|
| 76 |
str(ckpt),
|
| 77 |
"--text_prompt",
|
|
@@ -86,34 +84,35 @@ def run_mdm(prompt: str, length: float, seed: int) -> Optional[str]:
|
|
| 86 |
try:
|
| 87 |
subprocess.run(cmd, cwd=REPO_DIR, check=True)
|
| 88 |
except subprocess.CalledProcessError as exc:
|
| 89 |
-
print("[error]
|
| 90 |
return None
|
| 91 |
|
| 92 |
-
#
|
| 93 |
mp4_files = list(Path(REPO_DIR).rglob("*.mp4"))
|
| 94 |
if not mp4_files:
|
| 95 |
-
print("[warn] No MP4 produced
|
| 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)
|
| 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 |
-
#
|
| 117 |
# ---------------------------------------------------------------------------
|
| 118 |
|
| 119 |
demo = gr.Interface(
|
|
@@ -136,8 +135,8 @@ demo = gr.Interface(
|
|
| 136 |
outputs=gr.Video(label="Generated Motion"),
|
| 137 |
title="Motion Diffusion Model Demo (HumanML)",
|
| 138 |
description=(
|
| 139 |
-
"
|
| 140 |
-
"The HumanML checkpoint
|
| 141 |
),
|
| 142 |
)
|
| 143 |
|
|
|
|
| 2 |
"""
|
| 3 |
Motion Diffusion Demo on Hugging Face Spaces
|
| 4 |
-------------------------------------------
|
| 5 |
+
Generate human motion from a text prompt with **Motion‑Diffusion‑Model (MDM)**.
|
| 6 |
+
We keep the user‑supplied layout:
|
| 7 |
+
|
| 8 |
+
* repo folder → motion-diffusion-model/
|
| 9 |
+
* checkpoint file → checkpoints/opt000750000.pt
|
| 10 |
+
|
| 11 |
+
The script simply changes into the repo and executes `python -m sample.generate`,
|
| 12 |
+
appearing exactly like the original workflow that was already working for you.
|
| 13 |
"""
|
| 14 |
|
| 15 |
from __future__ import annotations
|
|
|
|
| 23 |
import gradio as gr
|
| 24 |
|
| 25 |
# ---------------------------------------------------------------------------
|
| 26 |
+
# Config – edit only if your Space layout changes
|
| 27 |
# ---------------------------------------------------------------------------
|
| 28 |
|
| 29 |
+
REPO_DIR = "motion-diffusion-model" # cloned repo folder name
|
| 30 |
+
CHECKPOINT_PATH = "checkpoints/opt000750000.pt" # user‑supplied ckpt path
|
| 31 |
+
OUTPUT_DIR = "output" # final MP4 destination
|
| 32 |
+
MAX_LEN_SEC = 9.8 # model’s max sequence length
|
| 33 |
|
| 34 |
# ---------------------------------------------------------------------------
|
| 35 |
+
# Helper: make sure repo exists + on sys.path
|
| 36 |
# ---------------------------------------------------------------------------
|
| 37 |
|
| 38 |
def ensure_repo_ready() -> None:
|
|
|
|
| 39 |
if not Path(REPO_DIR).exists():
|
| 40 |
print("[setup] Cloning Motion‑Diffusion‑Model repo …")
|
| 41 |
subprocess.run(
|
|
|
|
| 52 |
if repo_abs not in sys.path:
|
| 53 |
sys.path.insert(0, repo_abs)
|
| 54 |
|
|
|
|
| 55 |
# ---------------------------------------------------------------------------
|
| 56 |
+
# Core Generation
|
| 57 |
# ---------------------------------------------------------------------------
|
| 58 |
|
| 59 |
def run_mdm(prompt: str, length: float, seed: int) -> Optional[str]:
|
| 60 |
+
"""Call the official generator inside the repo and return path to MP4."""
|
| 61 |
ensure_repo_ready()
|
| 62 |
|
| 63 |
ckpt = Path(CHECKPOINT_PATH).resolve()
|
| 64 |
if not ckpt.exists():
|
| 65 |
raise FileNotFoundError(f"Checkpoint not found: {ckpt}")
|
| 66 |
|
|
|
|
| 67 |
Path(OUTPUT_DIR).mkdir(exist_ok=True)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
cmd = [
|
| 70 |
"python",
|
| 71 |
+
"-m",
|
| 72 |
+
"sample.generate",
|
| 73 |
"--model_path",
|
| 74 |
str(ckpt),
|
| 75 |
"--text_prompt",
|
|
|
|
| 84 |
try:
|
| 85 |
subprocess.run(cmd, cwd=REPO_DIR, check=True)
|
| 86 |
except subprocess.CalledProcessError as exc:
|
| 87 |
+
print("[error] Generator failed:", exc)
|
| 88 |
return None
|
| 89 |
|
| 90 |
+
# Look for the newest MP4 inside the repo after generation
|
| 91 |
mp4_files = list(Path(REPO_DIR).rglob("*.mp4"))
|
| 92 |
if not mp4_files:
|
| 93 |
+
print("[warn] No MP4 produced.")
|
| 94 |
return None
|
| 95 |
|
| 96 |
newest = max(mp4_files, key=lambda p: p.stat().st_mtime)
|
| 97 |
final_path = Path(OUTPUT_DIR) / newest.name
|
| 98 |
+
newest.replace(final_path)
|
| 99 |
|
| 100 |
print(f"[ok] Motion video saved to {final_path}")
|
| 101 |
return str(final_path)
|
| 102 |
|
| 103 |
+
# ---------------------------------------------------------------------------
|
| 104 |
+
# Gradio wrapper
|
| 105 |
+
# ---------------------------------------------------------------------------
|
| 106 |
|
| 107 |
def text_to_motion(prompt: str, length: float = 3.0, seed: int = 0):
|
|
|
|
| 108 |
try:
|
| 109 |
return run_mdm(prompt, length, seed)
|
| 110 |
except Exception:
|
| 111 |
print(traceback.format_exc())
|
| 112 |
return None
|
| 113 |
|
|
|
|
| 114 |
# ---------------------------------------------------------------------------
|
| 115 |
+
# Interface
|
| 116 |
# ---------------------------------------------------------------------------
|
| 117 |
|
| 118 |
demo = gr.Interface(
|
|
|
|
| 135 |
outputs=gr.Video(label="Generated Motion"),
|
| 136 |
title="Motion Diffusion Model Demo (HumanML)",
|
| 137 |
description=(
|
| 138 |
+
"Describe an action — e.g. 'A person runs in a circle and jumps'. "
|
| 139 |
+
"The HumanML checkpoint returns a skeletal MP4."
|
| 140 |
),
|
| 141 |
)
|
| 142 |
|