Spaces:
No application file
No application file
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,255 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
import sys
|
| 4 |
-
import numpy as np
|
| 5 |
-
import torch
|
| 6 |
-
from pathlib import Path
|
| 7 |
-
import tempfile
|
| 8 |
-
import zipfile
|
| 9 |
-
|
| 10 |
-
# Add utils to path
|
| 11 |
-
sys.path.append('utils')
|
| 12 |
-
|
| 13 |
-
# Import your utility modules
|
| 14 |
-
from model_loader import load_mdm_model, generate_motion
|
| 15 |
-
from visualization import create_stick_figure_video, create_3d_visualization
|
| 16 |
-
from blender_export import prepare_blender_export
|
| 17 |
-
|
| 18 |
-
# Initialize models dictionary
|
| 19 |
-
MODELS = {
|
| 20 |
-
"HumanML3D (50-steps Fast)": "humanml_trans_enc_512_50steps",
|
| 21 |
-
"HumanML3D (Original)": "humanml_trans_enc_512",
|
| 22 |
-
"KIT Dataset": "kit_encoder_512",
|
| 23 |
-
"HumanAct12": "humanact12",
|
| 24 |
-
"UESTC": "uestc",
|
| 25 |
-
"Unconstrained": "humanact12_unconstrained"
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
# Model loading function
|
| 29 |
-
def load_selected_model(model_name):
|
| 30 |
-
model_path = f"./save/{MODELS[model_name]}"
|
| 31 |
-
return load_mdm_model(model_name, model_path)
|
| 32 |
-
|
| 33 |
-
# Motion generation function
|
| 34 |
-
def generate_motion_interface(
|
| 35 |
-
model_name,
|
| 36 |
-
text_prompt,
|
| 37 |
-
action_name,
|
| 38 |
-
num_samples,
|
| 39 |
-
motion_length,
|
| 40 |
-
seed,
|
| 41 |
-
temperature
|
| 42 |
-
):
|
| 43 |
-
"""
|
| 44 |
-
Unified interface for motion generation
|
| 45 |
-
"""
|
| 46 |
-
try:
|
| 47 |
-
# Load model
|
| 48 |
-
model = load_selected_model(model_name)
|
| 49 |
-
|
| 50 |
-
# Generate based on model type
|
| 51 |
-
if "HumanML3D" in model_name or "KIT" in model_name:
|
| 52 |
-
# Text-to-motion
|
| 53 |
-
results = generate_motion(
|
| 54 |
-
model=model,
|
| 55 |
-
text_prompt=text_prompt,
|
| 56 |
-
num_samples=num_samples,
|
| 57 |
-
motion_length=motion_length,
|
| 58 |
-
seed=seed
|
| 59 |
-
)
|
| 60 |
-
else:
|
| 61 |
-
# Action-to-motion or unconstrained
|
| 62 |
-
results = generate_motion(
|
| 63 |
-
model=model,
|
| 64 |
-
action_name=action_name,
|
| 65 |
-
num_samples=num_samples,
|
| 66 |
-
seed=seed
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
# Create visualization
|
| 70 |
-
video_path = create_stick_figure_video(results)
|
| 71 |
-
plot_path = create_3d_visualization(results)
|
| 72 |
-
|
| 73 |
-
# Prepare export data
|
| 74 |
-
export_data = prepare_blender_export(results)
|
| 75 |
-
|
| 76 |
-
return video_path, plot_path, export_data, " Generation successful!"
|
| 77 |
-
|
| 78 |
-
except Exception as e:
|
| 79 |
-
return None, None, None, f" Error: {str(e)}"
|
| 80 |
-
|
| 81 |
-
# Blender export function
|
| 82 |
-
def export_to_blender(export_data, format_type="obj"):
|
| 83 |
-
"""
|
| 84 |
-
Prepare files for Blender import
|
| 85 |
-
"""
|
| 86 |
-
if export_data is None:
|
| 87 |
-
return None, "No motion data to export"
|
| 88 |
-
|
| 89 |
-
# Create temporary directory
|
| 90 |
-
with tempfile.TemporaryDirectory() as tmpdir:
|
| 91 |
-
# Prepare files based on format
|
| 92 |
-
if format_type == "obj":
|
| 93 |
-
# Save OBJ sequences
|
| 94 |
-
export_path = export_data.save_as_obj_sequence(tmpdir)
|
| 95 |
-
elif format_type == "smpl_params":
|
| 96 |
-
# Save SMPL parameters
|
| 97 |
-
export_path = export_data.save_smpl_parameters(tmpdir)
|
| 98 |
-
elif format_type == "fbx":
|
| 99 |
-
# Convert to FBX (would need additional conversion)
|
| 100 |
-
export_path = export_data.convert_to_fbx(tmpdir)
|
| 101 |
-
|
| 102 |
-
# Create ZIP archive
|
| 103 |
-
zip_path = f"{tmpdir}/blender_export.zip"
|
| 104 |
-
with zipfile.ZipFile(zip_path, 'w') as zipf:
|
| 105 |
-
for root, dirs, files in os.walk(export_path):
|
| 106 |
-
for file in files:
|
| 107 |
-
zipf.write(
|
| 108 |
-
os.path.join(root, file),
|
| 109 |
-
os.path.relpath(os.path.join(root, file), tmpdir)
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
return zip_path, " Export ready! Download and extract for Blender."
|
| 113 |
-
|
| 114 |
-
# Gradio Interface
|
| 115 |
-
with gr.Blocks(
|
| 116 |
-
title="Motion Diffusion Model",
|
| 117 |
-
theme=gr.themes.Soft(),
|
| 118 |
-
css=".gradio-container {max-width: 1200px !important;}"
|
| 119 |
-
) as demo:
|
| 120 |
-
|
| 121 |
-
gr.Markdown("# Motion Diffusion Model")
|
| 122 |
-
gr.Markdown("Generate 3D human motions from text or actions")
|
| 123 |
-
|
| 124 |
-
with gr.Row():
|
| 125 |
-
with gr.Column(scale=1):
|
| 126 |
-
# Model selection
|
| 127 |
-
model_dropdown = gr.Dropdown(
|
| 128 |
-
choices=list(MODELS.keys()),
|
| 129 |
-
value="HumanML3D (50-steps Fast)",
|
| 130 |
-
label="Select Model",
|
| 131 |
-
info="HumanML3D-50steps is 20x faster"
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
# Common parameters
|
| 135 |
-
num_samples = gr.Slider(
|
| 136 |
-
minimum=1, maximum=10, value=3, step=1,
|
| 137 |
-
label="Number of Samples"
|
| 138 |
-
)
|
| 139 |
-
seed = gr.Number(value=42, label="Random Seed")
|
| 140 |
-
motion_length = gr.Slider(
|
| 141 |
-
minimum=1.0, maximum=9.8, value=3.0, step=0.5,
|
| 142 |
-
label="Motion Length (seconds)",
|
| 143 |
-
visible=True # Only for text-to-motion
|
| 144 |
-
)
|
| 145 |
-
|
| 146 |
-
with gr.Column(scale=2):
|
| 147 |
-
# Text input for text-to-motion models
|
| 148 |
-
text_input = gr.Textbox(
|
| 149 |
-
label="Text Prompt",
|
| 150 |
-
placeholder="the person walked forward and is picking up his toolbox...",
|
| 151 |
-
lines=3,
|
| 152 |
-
value="a person is walking slowly and then starts running"
|
| 153 |
-
)
|
| 154 |
-
|
| 155 |
-
# Action input for action-to-motion models
|
| 156 |
-
action_input = gr.Textbox(
|
| 157 |
-
label="Action Name",
|
| 158 |
-
placeholder="walk, run, jump, drink, ...",
|
| 159 |
-
visible=False # Shown based on model selection
|
| 160 |
-
)
|
| 161 |
-
|
| 162 |
-
# Generate button
|
| 163 |
-
generate_btn = gr.Button(" Generate Motion", variant="primary")
|
| 164 |
-
|
| 165 |
-
# Results area
|
| 166 |
-
with gr.Row():
|
| 167 |
-
video_output = gr.Video(
|
| 168 |
-
label="Stick Figure Animation",
|
| 169 |
-
format="mp4"
|
| 170 |
-
)
|
| 171 |
-
plot_output = gr.Plot(
|
| 172 |
-
label="3D Motion Visualization"
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
-
# Status message
|
| 176 |
-
status_msg = gr.Markdown("")
|
| 177 |
-
|
| 178 |
-
# Export section
|
| 179 |
-
with gr.Accordion(" Export to Blender", open=False):
|
| 180 |
-
with gr.Row():
|
| 181 |
-
export_format = gr.Radio(
|
| 182 |
-
choices=["OBJ Sequence", "SMPL Parameters", "FBX"],
|
| 183 |
-
value="OBJ Sequence",
|
| 184 |
-
label="Export Format"
|
| 185 |
-
)
|
| 186 |
-
export_btn = gr.Button("Prepare Blender Export")
|
| 187 |
-
|
| 188 |
-
export_download = gr.File(label="Download Export Package")
|
| 189 |
-
export_status = gr.Markdown("")
|
| 190 |
-
|
| 191 |
-
# Model-specific UI updates
|
| 192 |
-
def update_ui(model_name):
|
| 193 |
-
"""Show/hide relevant inputs based on model type"""
|
| 194 |
-
is_text_model = "HumanML3D" in model_name or "KIT" in model_name
|
| 195 |
-
return [
|
| 196 |
-
gr.Textbox(visible=is_text_model), # text_input
|
| 197 |
-
gr.Textbox(visible=not is_text_model), # action_input
|
| 198 |
-
gr.Slider(visible=is_text_model) # motion_length
|
| 199 |
-
]
|
| 200 |
-
|
| 201 |
-
# Connect events
|
| 202 |
-
model_dropdown.change(
|
| 203 |
-
update_ui,
|
| 204 |
-
inputs=[model_dropdown],
|
| 205 |
-
outputs=[text_input, action_input, motion_length]
|
| 206 |
-
)
|
| 207 |
-
|
| 208 |
-
generate_btn.click(
|
| 209 |
-
generate_motion_interface,
|
| 210 |
-
inputs=[
|
| 211 |
-
model_dropdown,
|
| 212 |
-
text_input,
|
| 213 |
-
action_input,
|
| 214 |
-
num_samples,
|
| 215 |
-
motion_length,
|
| 216 |
-
seed,
|
| 217 |
-
gr.Slider(minimum=0.1, maximum=2.0, value=1.0, label="Temperature", visible=False)
|
| 218 |
-
],
|
| 219 |
-
outputs=[video_output, plot_output, gr.State(), status_msg]
|
| 220 |
-
)
|
| 221 |
-
|
| 222 |
-
export_btn.click(
|
| 223 |
-
export_to_blender,
|
| 224 |
-
inputs=[gr.State(), export_format],
|
| 225 |
-
outputs=[export_download, export_status]
|
| 226 |
-
)
|
| 227 |
-
|
| 228 |
-
# Examples
|
| 229 |
-
gr.Examples(
|
| 230 |
-
examples=[
|
| 231 |
-
["HumanML3D (50-steps Fast)", "a person is doing jumping jacks", None, 2, 3.0, 123],
|
| 232 |
-
["HumanML3D (50-steps Fast)", "someone walks, stops to tie their shoe, then continues", None, 1, 5.0, 456],
|
| 233 |
-
["HumanAct12", None, "walk", 3, 3.0, 789],
|
| 234 |
-
["UESTC", None, "kick", 2, 2.5, 101],
|
| 235 |
-
],
|
| 236 |
-
inputs=[model_dropdown, text_input, action_input, num_samples, motion_length, seed],
|
| 237 |
-
outputs=[video_output, plot_output],
|
| 238 |
-
fn=generate_motion_interface,
|
| 239 |
-
cache_examples=False
|
| 240 |
-
)
|
| 241 |
-
|
| 242 |
-
# Launch for Hugging Face Spaces
|
| 243 |
-
if __name__ == "__main__":
|
| 244 |
-
# Check and download models on first run
|
| 245 |
-
if not os.path.exists("./save"):
|
| 246 |
-
print(" Models not found. Running download script...")
|
| 247 |
-
os.system("python download_models.py --lite")
|
| 248 |
-
|
| 249 |
-
# Launch with Space-appropriate settings
|
| 250 |
-
demo.launch(
|
| 251 |
-
server_name="0.0.0.0",
|
| 252 |
-
server_port=int(os.getenv("PORT", 7860)),
|
| 253 |
-
share=False,
|
| 254 |
-
debug=False
|
| 255 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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