megalado
commited on
Commit
·
40972d1
1
Parent(s):
434c5bc
Simplify app.py and fix path issues
Browse files
app.py
CHANGED
|
@@ -3,67 +3,28 @@ import torch
|
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import numpy as np
|
| 6 |
-
import subprocess
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def setup_environment():
|
| 13 |
-
"""Set up the environment and download necessary files"""
|
| 14 |
-
global progress_status
|
| 15 |
-
|
| 16 |
-
# Clone the MDM repository during the first run
|
| 17 |
-
if not Path("motion-diffusion-model").exists():
|
| 18 |
-
progress_status = "Cloning Motion Diffusion Model repository..."
|
| 19 |
-
# Clone the repository
|
| 20 |
-
subprocess.run(["git", "clone", "https://github.com/GuyTevet/motion-diffusion-model.git"])
|
| 21 |
-
|
| 22 |
-
# Install spacy language model (required by MDM)
|
| 23 |
-
progress_status = "Installing Spacy language model..."
|
| 24 |
-
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
| 25 |
-
|
| 26 |
-
# Download other necessary files if they don't exist
|
| 27 |
-
if not Path("motion-diffusion-model/data/smpl").exists():
|
| 28 |
-
progress_status = "Downloading SMPL files..."
|
| 29 |
-
subprocess.run(["bash", "motion-diffusion-model/prepare/download_smpl_files.sh"])
|
| 30 |
-
|
| 31 |
-
if not Path("motion-diffusion-model/data/glove").exists():
|
| 32 |
-
progress_status = "Downloading Glove embeddings..."
|
| 33 |
-
subprocess.run(["bash", "motion-diffusion-model/prepare/download_glove.sh"])
|
| 34 |
-
|
| 35 |
-
if not Path("motion-diffusion-model/save/t2m_evaluators").exists():
|
| 36 |
-
progress_status = "Downloading Text-to-Motion evaluators..."
|
| 37 |
-
subprocess.run(["bash", "motion-diffusion-model/prepare/download_t2m_evaluators.sh"])
|
| 38 |
-
|
| 39 |
-
# Add the repository to the Python path
|
| 40 |
sys.path.append('./motion-diffusion-model')
|
| 41 |
-
|
| 42 |
-
progress_status = "Setup complete! Ready to generate animations."
|
| 43 |
-
return "Environment setup complete"
|
| 44 |
-
|
| 45 |
-
def get_setup_status():
|
| 46 |
-
"""Return the current setup status"""
|
| 47 |
-
global progress_status
|
| 48 |
-
return progress_status
|
| 49 |
-
|
| 50 |
-
# Initialize setup in background
|
| 51 |
-
try:
|
| 52 |
-
setup_environment()
|
| 53 |
-
except Exception as e:
|
| 54 |
-
progress_status = f"Setup error: {str(e)}"
|
| 55 |
|
| 56 |
def text_to_motion(text_prompt, motion_length=3.0, seed=0):
|
| 57 |
"""Generate motion from text prompt using MDM"""
|
| 58 |
-
global progress_status
|
| 59 |
-
|
| 60 |
try:
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Set the seed for reproducibility
|
| 69 |
if seed is not None:
|
|
@@ -73,7 +34,7 @@ def text_to_motion(text_prompt, motion_length=3.0, seed=0):
|
|
| 73 |
model_path = "checkpoints/mld_humanml.pt"
|
| 74 |
|
| 75 |
# Generate the motion
|
| 76 |
-
|
| 77 |
motion_data = generate(
|
| 78 |
model_path=model_path,
|
| 79 |
text_prompt=text_prompt,
|
|
@@ -82,57 +43,28 @@ def text_to_motion(text_prompt, motion_length=3.0, seed=0):
|
|
| 82 |
)
|
| 83 |
|
| 84 |
# Render the animation
|
| 85 |
-
|
| 86 |
output_path = "output.mp4"
|
| 87 |
visualize(motion_data, output_path)
|
| 88 |
|
| 89 |
-
|
| 90 |
return output_path
|
| 91 |
except Exception as e:
|
| 92 |
-
|
| 93 |
return None
|
| 94 |
|
| 95 |
# Create the Gradio interface
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
length_slider = gr.Slider(
|
| 108 |
-
minimum=1.0,
|
| 109 |
-
maximum=9.8,
|
| 110 |
-
value=3.0,
|
| 111 |
-
label="Motion Length (seconds)"
|
| 112 |
-
)
|
| 113 |
-
seed_number = gr.Number(
|
| 114 |
-
label="Random Seed",
|
| 115 |
-
value=0
|
| 116 |
-
)
|
| 117 |
-
generate_btn = gr.Button("Generate Motion")
|
| 118 |
-
status_text = gr.Textbox(
|
| 119 |
-
label="Status",
|
| 120 |
-
value=progress_status,
|
| 121 |
-
interactive=False
|
| 122 |
-
)
|
| 123 |
-
|
| 124 |
-
with gr.Column():
|
| 125 |
-
video_output = gr.Video(label="Generated Motion")
|
| 126 |
-
|
| 127 |
-
# Update the status periodically
|
| 128 |
-
status_text.change(get_setup_status, inputs=None, outputs=status_text, every=1)
|
| 129 |
-
|
| 130 |
-
# Connect the button to the generation function
|
| 131 |
-
generate_btn.click(
|
| 132 |
-
text_to_motion,
|
| 133 |
-
inputs=[text_input, length_slider, seed_number],
|
| 134 |
-
outputs=video_output
|
| 135 |
-
)
|
| 136 |
|
| 137 |
# Launch the app
|
| 138 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
+
# Add the motion-diffusion-model repository to the Python path if it exists
|
| 9 |
+
if Path("motion-diffusion-model").exists():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
sys.path.append('./motion-diffusion-model')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def text_to_motion(text_prompt, motion_length=3.0, seed=0):
|
| 13 |
"""Generate motion from text prompt using MDM"""
|
|
|
|
|
|
|
| 14 |
try:
|
| 15 |
+
# First, check if we need to clone the repository
|
| 16 |
+
if not Path("motion-diffusion-model").exists():
|
| 17 |
+
import subprocess
|
| 18 |
+
print("Cloning Motion Diffusion Model repository...")
|
| 19 |
+
subprocess.run(["git", "clone", "https://github.com/GuyTevet/motion-diffusion-model.git"])
|
| 20 |
+
# Add the repository to the Python path after cloning
|
| 21 |
+
sys.path.append('./motion-diffusion-model')
|
| 22 |
+
print("Installed Spacy language model...")
|
| 23 |
+
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
|
| 24 |
|
| 25 |
+
# Now import the necessary functions
|
| 26 |
+
from motion_diffusion_model.sample.generate import generate
|
| 27 |
+
from motion_diffusion_model.visualization.visualize import visualize
|
| 28 |
|
| 29 |
# Set the seed for reproducibility
|
| 30 |
if seed is not None:
|
|
|
|
| 34 |
model_path = "checkpoints/mld_humanml.pt"
|
| 35 |
|
| 36 |
# Generate the motion
|
| 37 |
+
print("Running MDM generation...")
|
| 38 |
motion_data = generate(
|
| 39 |
model_path=model_path,
|
| 40 |
text_prompt=text_prompt,
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
# Render the animation
|
| 46 |
+
print("Rendering animation...")
|
| 47 |
output_path = "output.mp4"
|
| 48 |
visualize(motion_data, output_path)
|
| 49 |
|
| 50 |
+
print("Animation generated successfully!")
|
| 51 |
return output_path
|
| 52 |
except Exception as e:
|
| 53 |
+
print(f"Error generating motion: {str(e)}")
|
| 54 |
return None
|
| 55 |
|
| 56 |
# Create the Gradio interface
|
| 57 |
+
demo = gr.Interface(
|
| 58 |
+
fn=text_to_motion,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.Textbox(label="Text Prompt", placeholder="A person walks forward, then turns left", lines=3),
|
| 61 |
+
gr.Slider(minimum=1.0, maximum=9.8, value=3.0, label="Motion Length (seconds)"),
|
| 62 |
+
gr.Number(label="Random Seed", value=0)
|
| 63 |
+
],
|
| 64 |
+
outputs=gr.Video(label="Generated Motion"),
|
| 65 |
+
title="Motion Diffusion Model (MDM)",
|
| 66 |
+
description="Generate human motions from text descriptions using MDM"
|
| 67 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# Launch the app
|
| 70 |
if __name__ == "__main__":
|