Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,19 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Gradio Interface for SignMotionGPT (HF Spaces Compatible)
|
| 3 |
-
"""
|
| 4 |
import os
|
| 5 |
import sys
|
| 6 |
import re
|
| 7 |
import json
|
| 8 |
import random
|
| 9 |
import argparse
|
| 10 |
-
import time
|
| 11 |
import warnings
|
|
|
|
|
|
|
| 12 |
from pathlib import Path
|
| 13 |
|
| 14 |
import torch
|
| 15 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 16 |
import gradio as gr
|
| 17 |
import plotly.graph_objects as go
|
| 18 |
from plotly.subplots import make_subplots
|
|
@@ -23,36 +24,36 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
| 23 |
warnings.filterwarnings("ignore")
|
| 24 |
|
| 25 |
# =====================================================================
|
| 26 |
-
# Configuration
|
| 27 |
# =====================================================================
|
| 28 |
-
#
|
| 29 |
-
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 30 |
-
OUTPUT_DIR = os.path.join(BASE_DIR, "generated_outputs")
|
| 31 |
-
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 32 |
-
|
| 33 |
-
# Add project root to path
|
| 34 |
-
sys.path.append(BASE_DIR)
|
| 35 |
-
|
| 36 |
HF_REPO_ID = os.environ.get("HF_REPO_ID", "rdz-falcon/SignMotionGPTfit-archive")
|
| 37 |
HF_SUBFOLDER = os.environ.get("HF_SUBFOLDER", "stage2_v2/epoch-030")
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 47 |
|
| 48 |
-
#
|
| 49 |
M_START = "<M_START>"
|
| 50 |
M_END = "<M_END>"
|
| 51 |
PAD_TOKEN = "<PAD>"
|
|
|
|
|
|
|
| 52 |
INFERENCE_TEMPERATURE = 0.7
|
| 53 |
INFERENCE_TOP_K = 50
|
| 54 |
INFERENCE_REPETITION_PENALTY = 1.2
|
| 55 |
|
|
|
|
| 56 |
SMPL_DIM = 182
|
| 57 |
CODEBOOK_SIZE = 512
|
| 58 |
CODE_DIM = 512
|
|
@@ -65,19 +66,79 @@ PARAM_DIMS = [10, 63, 45, 45, 3, 10, 3, 3]
|
|
| 65 |
PARAM_NAMES = ["betas", "body_pose", "left_hand_pose", "right_hand_pose",
|
| 66 |
"trans", "expression", "jaw_pose", "eye_pose"]
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# =====================================================================
|
| 69 |
# Import VQ-VAE architecture
|
| 70 |
# =====================================================================
|
|
|
|
|
|
|
|
|
|
| 71 |
try:
|
| 72 |
-
#
|
| 73 |
from mGPT.archs.mgpt_vq import VQVae
|
| 74 |
-
except ImportError:
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
from archs.mgpt_vq import VQVae
|
| 78 |
-
except ImportError:
|
| 79 |
-
print("⚠️ Warning: Could not import VQVae architecture.")
|
| 80 |
-
VQVae = None
|
| 81 |
|
| 82 |
# =====================================================================
|
| 83 |
# Global Cache
|
|
@@ -91,8 +152,8 @@ _model_cache = {
|
|
| 91 |
"initialized": False
|
| 92 |
}
|
| 93 |
|
| 94 |
-
_word_pid_map = {}
|
| 95 |
-
_example_cache = {}
|
| 96 |
|
| 97 |
# =====================================================================
|
| 98 |
# Dataset Loading
|
|
@@ -100,276 +161,456 @@ _example_cache = {}
|
|
| 100 |
def load_word_pid_mapping():
|
| 101 |
global _word_pid_map
|
| 102 |
if not os.path.exists(DATASET_PATH):
|
| 103 |
-
|
| 104 |
-
_word_pid_map = {"push": ["P40"], "send": ["P40"]}
|
| 105 |
return
|
| 106 |
-
|
|
|
|
| 107 |
try:
|
| 108 |
with open(DATASET_PATH, 'r', encoding='utf-8') as f:
|
| 109 |
data = json.load(f)
|
| 110 |
|
| 111 |
-
mapping = {}
|
| 112 |
for entry in data:
|
| 113 |
-
word =
|
| 114 |
-
pid = entry.get('participant_id')
|
| 115 |
if word and pid:
|
| 116 |
-
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
print(f"Error loading dataset: {e}")
|
| 122 |
|
|
|
|
|
|
|
|
|
|
| 123 |
def get_random_pids_for_word(word: str, count: int = 2) -> list:
|
| 124 |
-
pids =
|
| 125 |
if not pids: return []
|
| 126 |
if len(pids) <= count: return pids
|
| 127 |
return random.sample(pids, count)
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
# =====================================================================
|
| 130 |
-
#
|
| 131 |
# =====================================================================
|
| 132 |
class MotionGPT_VQVAE_Wrapper(torch.nn.Module):
|
| 133 |
def __init__(self, smpl_dim=SMPL_DIM, codebook_size=CODEBOOK_SIZE, code_dim=CODE_DIM, **kwargs):
|
| 134 |
super().__init__()
|
| 135 |
-
if VQVae is None:
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
def initialize_models():
|
| 139 |
global _model_cache
|
| 140 |
if _model_cache["initialized"]: return
|
| 141 |
|
| 142 |
print("Initializing Models...")
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
# LLM
|
| 146 |
-
print(f"Loading LLM: {HF_REPO_ID}")
|
| 147 |
-
tok = AutoTokenizer.from_pretrained(HF_REPO_ID, subfolder=HF_SUBFOLDER, trust_remote_code=True)
|
| 148 |
-
model = AutoModelForCausalLM.from_pretrained(HF_REPO_ID, subfolder=HF_SUBFOLDER, trust_remote_code=True)
|
| 149 |
-
if tok.pad_token is None: tok.add_special_tokens({"pad_token": PAD_TOKEN})
|
| 150 |
-
model.resize_token_embeddings(len(tok))
|
| 151 |
-
model.to(DEVICE).eval()
|
| 152 |
-
_model_cache["llm_model"] = model
|
| 153 |
-
_model_cache["llm_tokenizer"] = tok
|
| 154 |
-
|
| 155 |
-
# VQ-VAE
|
| 156 |
-
if os.path.exists(VQVAE_CHECKPOINT):
|
| 157 |
-
vq = MotionGPT_VQVAE_Wrapper(**VQ_ARGS).to(DEVICE)
|
| 158 |
-
ckpt = torch.load(VQVAE_CHECKPOINT, map_location=DEVICE,weights_only=False)
|
| 159 |
-
vq.load_state_dict(ckpt.get('model_state_dict', ckpt), strict=False)
|
| 160 |
-
vq.eval()
|
| 161 |
-
_model_cache["vqvae_model"] = vq
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
| 167 |
|
| 168 |
-
# SMPL-X
|
| 169 |
-
if os.path.exists(SMPLX_MODEL_DIR):
|
| 170 |
-
_model_cache["smplx_model"] = smplx.SMPLX(
|
| 171 |
-
model_path=SMPLX_MODEL_DIR, model_type='smplx', gender='neutral', use_pca=False,
|
| 172 |
-
create_global_orient=True, create_body_pose=True, create_betas=True,
|
| 173 |
-
create_expression=True, create_jaw_pose=True, create_left_hand_pose=True,
|
| 174 |
-
create_right_hand_pose=True, create_transl=True
|
| 175 |
-
).to(DEVICE)
|
| 176 |
-
|
| 177 |
_model_cache["initialized"] = True
|
| 178 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
# =====================================================================
|
| 181 |
-
# Generation Logic
|
| 182 |
# =====================================================================
|
| 183 |
def generate_motion_tokens(word: str, variant: str) -> str:
|
| 184 |
-
model
|
|
|
|
|
|
|
|
|
|
| 185 |
prompt = f"Instruction: Generate motion for word '{word}' with variant '{variant}'.\nMotion: "
|
| 186 |
-
inputs =
|
|
|
|
| 187 |
with torch.no_grad():
|
| 188 |
-
|
| 189 |
**inputs, max_new_tokens=100, do_sample=True,
|
| 190 |
temperature=INFERENCE_TEMPERATURE, top_k=INFERENCE_TOP_K,
|
| 191 |
repetition_penalty=INFERENCE_REPETITION_PENALTY,
|
| 192 |
-
|
|
|
|
|
|
|
| 193 |
)
|
| 194 |
-
decoded =
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
def decode_tokens_to_params(tokens: list) -> np.ndarray:
|
| 198 |
-
|
| 199 |
-
|
|
|
|
| 200 |
|
| 201 |
idx = torch.tensor(tokens, dtype=torch.long, device=DEVICE).unsqueeze(0)
|
| 202 |
with torch.no_grad():
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
|
| 219 |
-
#
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
bp = bp_full[:, 3:]
|
| 228 |
-
|
| 229 |
-
out = smpl(
|
| 230 |
-
betas=batch[0], global_orient=go, body_pose=bp,
|
| 231 |
-
left_hand_pose=batch[2], right_hand_pose=batch[3],
|
| 232 |
-
transl=batch[4], expression=batch[5],
|
| 233 |
-
jaw_pose=batch[6], leye_pose=batch[7], reye_pose=batch[7]
|
| 234 |
-
)
|
| 235 |
-
verts_list.append(out.vertices.cpu().numpy())
|
| 236 |
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
# =====================================================================
|
| 240 |
-
#
|
| 241 |
# =====================================================================
|
| 242 |
-
def
|
| 243 |
-
|
| 244 |
-
min_len = min(len(verts1), len(verts2))
|
| 245 |
-
v1, v2 = verts1[:min_len], verts2[:min_len]
|
| 246 |
|
| 247 |
-
|
| 248 |
-
|
|
|
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
fig.add_trace(go.Mesh3d(
|
| 253 |
-
x=v[0,:,0], y=v[0,:,1], z=v[0,:,2],
|
| 254 |
-
i=faces1[:,0], j=faces1[:,1], k=faces1[:,2],
|
| 255 |
-
color=c, opacity=0.8, flatshading=True
|
| 256 |
-
), row=1, col=col)
|
| 257 |
-
|
| 258 |
-
# Frames
|
| 259 |
-
frames = []
|
| 260 |
-
for t in range(min_len):
|
| 261 |
-
frames.append(go.Frame(data=[
|
| 262 |
-
go.Mesh3d(x=v1[t,:,0], y=v1[t,:,1], z=v1[t,:,2]),
|
| 263 |
-
go.Mesh3d(x=v2[t,:,0], y=v2[t,:,1], z=v2[t,:,2])
|
| 264 |
-
], name=str(t)))
|
| 265 |
|
| 266 |
-
|
| 267 |
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
fig.update_layout(
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
scene2=dict(aspectmode='data', xaxis_visible=False, yaxis_visible=False, zaxis_visible=False),
|
| 273 |
-
height=500, margin=dict(l=0, r=0, t=30, b=0)
|
| 274 |
)
|
| 275 |
-
|
| 276 |
return fig.to_html(include_plotlyjs='cdn', full_html=True)
|
| 277 |
|
| 278 |
-
def
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
-
frames = [go.Frame(data=[go.Mesh3d(x=verts[t,:,0], y=verts[t,:,1], z=verts[t,:,2])], name=str(t))
|
| 286 |
-
for t in range(len(verts))]
|
| 287 |
fig.frames = frames
|
| 288 |
|
|
|
|
|
|
|
| 289 |
fig.update_layout(
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
)
|
| 295 |
return fig.to_html(include_plotlyjs='cdn', full_html=True)
|
| 296 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
# =====================================================================
|
| 298 |
-
# Main
|
| 299 |
# =====================================================================
|
| 300 |
-
def
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
width="100%" height="550px"
|
| 313 |
-
style="border:none; background:#fafafa;"
|
| 314 |
-
sandbox="allow-scripts allow-same-origin">
|
| 315 |
-
</iframe>
|
| 316 |
-
"""
|
| 317 |
-
return iframe
|
| 318 |
|
| 319 |
-
def process_word(word
|
| 320 |
-
if not
|
| 321 |
|
|
|
|
| 322 |
pids = get_random_pids_for_word(word, 2)
|
| 323 |
-
if not pids: pids = ["Unknown", "Unknown"]
|
| 324 |
-
elif len(pids) == 1: pids = [pids[0], pids[0]]
|
| 325 |
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
toks1 = [int(x) for x in re.findall(r'<M(\d+)>', raw1)]
|
| 329 |
-
verts1, faces = params_to_vertices(decode_tokens_to_params(toks1))
|
| 330 |
|
| 331 |
-
|
| 332 |
-
raw2 = generate_motion_tokens(word, pids[1])
|
| 333 |
-
toks2 = [int(x) for x in re.findall(r'<M(\d+)>', raw2)]
|
| 334 |
-
verts2, _ = params_to_vertices(decode_tokens_to_params(toks2))
|
| 335 |
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
-
|
| 344 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
# =====================================================================
|
| 347 |
-
#
|
| 348 |
# =====================================================================
|
| 349 |
def create_ui():
|
| 350 |
-
|
|
|
|
| 351 |
|
| 352 |
-
with gr.Blocks(
|
| 353 |
-
gr.Markdown("# SignMotionGPT
|
| 354 |
-
|
|
|
|
| 355 |
with gr.Row():
|
| 356 |
with gr.Column(scale=1):
|
| 357 |
-
txt_input = gr.Textbox(label="Word", placeholder="
|
| 358 |
-
btn = gr.Button("Generate
|
| 359 |
-
|
| 360 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
with gr.Column(scale=2):
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
initialize_models()
|
| 368 |
|
| 369 |
return demo
|
| 370 |
|
| 371 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
demo = create_ui()
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
demo.launch(server_name="0.0.0.0", server_port=7860, allowed_paths=[OUTPUT_DIR])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
import re
|
| 4 |
import json
|
| 5 |
import random
|
| 6 |
import argparse
|
|
|
|
| 7 |
import warnings
|
| 8 |
+
import html as html_module
|
| 9 |
+
import shutil
|
| 10 |
from pathlib import Path
|
| 11 |
|
| 12 |
import torch
|
| 13 |
import numpy as np
|
| 14 |
+
from huggingface_hub import hf_hub_download, snapshot_download
|
| 15 |
+
|
| 16 |
+
# Clean imports for Spaces (relies on requirements.txt)
|
| 17 |
import gradio as gr
|
| 18 |
import plotly.graph_objects as go
|
| 19 |
from plotly.subplots import make_subplots
|
|
|
|
| 24 |
warnings.filterwarnings("ignore")
|
| 25 |
|
| 26 |
# =====================================================================
|
| 27 |
+
# Configuration
|
| 28 |
# =====================================================================
|
| 29 |
+
# The Repo ID where your LLM and auxiliary files (vqvae, dataset) are stored
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
HF_REPO_ID = os.environ.get("HF_REPO_ID", "rdz-falcon/SignMotionGPTfit-archive")
|
| 31 |
HF_SUBFOLDER = os.environ.get("HF_SUBFOLDER", "stage2_v2/epoch-030")
|
| 32 |
|
| 33 |
+
# Spaces run in /home/user/app. We set up paths relative to that.
|
| 34 |
+
WORK_DIR = os.getcwd()
|
| 35 |
+
DATA_DIR = os.path.join(WORK_DIR, "data")
|
| 36 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 37 |
|
| 38 |
+
# Path definitions
|
| 39 |
+
DATASET_PATH = os.path.join(WORK_DIR, "enriched_dataset.json")
|
| 40 |
+
VQVAE_CHECKPOINT = os.path.join(DATA_DIR, "vqvae_model.pt")
|
| 41 |
+
STATS_PATH = os.path.join(DATA_DIR, "vqvae_stats.pt")
|
| 42 |
+
SMPLX_MODEL_DIR = os.path.join(DATA_DIR, "smplx_models")
|
| 43 |
|
| 44 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 45 |
|
| 46 |
+
# Token definitions
|
| 47 |
M_START = "<M_START>"
|
| 48 |
M_END = "<M_END>"
|
| 49 |
PAD_TOKEN = "<PAD>"
|
| 50 |
+
|
| 51 |
+
# Inference settings
|
| 52 |
INFERENCE_TEMPERATURE = 0.7
|
| 53 |
INFERENCE_TOP_K = 50
|
| 54 |
INFERENCE_REPETITION_PENALTY = 1.2
|
| 55 |
|
| 56 |
+
# Architecture settings
|
| 57 |
SMPL_DIM = 182
|
| 58 |
CODEBOOK_SIZE = 512
|
| 59 |
CODE_DIM = 512
|
|
|
|
| 66 |
PARAM_NAMES = ["betas", "body_pose", "left_hand_pose", "right_hand_pose",
|
| 67 |
"trans", "expression", "jaw_pose", "eye_pose"]
|
| 68 |
|
| 69 |
+
# =====================================================================
|
| 70 |
+
# Helper: Download Assets from HF Hub
|
| 71 |
+
# =====================================================================
|
| 72 |
+
def download_artifacts():
|
| 73 |
+
"""
|
| 74 |
+
Attempts to download missing auxiliary files (VQVAE, Stats, Dataset, SMPLX)
|
| 75 |
+
from the Hugging Face Hub Repository if they don't exist locally.
|
| 76 |
+
"""
|
| 77 |
+
print(f"Checking for artifacts in {HF_REPO_ID}...")
|
| 78 |
+
token = os.environ.get("HF_TOKEN") # Ensure this is set in Space Settings if repo is private
|
| 79 |
+
|
| 80 |
+
# 1. Download Dataset
|
| 81 |
+
if not os.path.exists(DATASET_PATH):
|
| 82 |
+
try:
|
| 83 |
+
print("Downloading dataset...")
|
| 84 |
+
hf_hub_download(repo_id=HF_REPO_ID, filename="enriched_dataset.json",
|
| 85 |
+
local_dir=WORK_DIR, token=token)
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print(f"Warning: Could not download dataset: {e}")
|
| 88 |
+
|
| 89 |
+
# 2. Download VQVAE Model
|
| 90 |
+
if not os.path.exists(VQVAE_CHECKPOINT):
|
| 91 |
+
try:
|
| 92 |
+
print("Downloading VQVAE model...")
|
| 93 |
+
# Assuming these are in a 'data' folder in your repo, or root. Adjust filename path as needed.
|
| 94 |
+
hf_hub_download(repo_id=HF_REPO_ID, filename="data/vqvae_model.pt",
|
| 95 |
+
local_dir=WORK_DIR, token=token)
|
| 96 |
+
except Exception as e:
|
| 97 |
+
# Fallback try root
|
| 98 |
+
try:
|
| 99 |
+
hf_hub_download(repo_id=HF_REPO_ID, filename="vqvae_model.pt",
|
| 100 |
+
local_dir=DATA_DIR, token=token)
|
| 101 |
+
except:
|
| 102 |
+
print(f"Warning: Could not download VQVAE model: {e}")
|
| 103 |
+
|
| 104 |
+
# 3. Download Stats
|
| 105 |
+
if not os.path.exists(STATS_PATH):
|
| 106 |
+
try:
|
| 107 |
+
print("Downloading VQVAE stats...")
|
| 108 |
+
hf_hub_download(repo_id=HF_REPO_ID, filename="data/vqvae_stats.pt",
|
| 109 |
+
local_dir=WORK_DIR, token=token)
|
| 110 |
+
except Exception as e:
|
| 111 |
+
try:
|
| 112 |
+
hf_hub_download(repo_id=HF_REPO_ID, filename="vqvae_stats.pt",
|
| 113 |
+
local_dir=DATA_DIR, token=token)
|
| 114 |
+
except:
|
| 115 |
+
print(f"Warning: Could not download VQVAE stats: {e}")
|
| 116 |
+
|
| 117 |
+
# 4. SMPLX Models
|
| 118 |
+
# Note: SMPLX models are licensed. If you can't host them, users must upload them.
|
| 119 |
+
# If they are in your repo (e.g. inside a zip or folder), download them here.
|
| 120 |
+
if not os.path.exists(SMPLX_MODEL_DIR):
|
| 121 |
+
print("Looking for SMPL-X models...")
|
| 122 |
+
try:
|
| 123 |
+
# Attempt to download a folder if it exists in the repo
|
| 124 |
+
snapshot_download(repo_id=HF_REPO_ID, allow_patterns="smplx_models/*",
|
| 125 |
+
local_dir=DATA_DIR, token=token)
|
| 126 |
+
except Exception as e:
|
| 127 |
+
print(f"Warning: Could not download SMPL-X models. Ensure 'smplx_models' folder exists in {DATA_DIR} or repo.")
|
| 128 |
+
|
| 129 |
+
|
| 130 |
# =====================================================================
|
| 131 |
# Import VQ-VAE architecture
|
| 132 |
# =====================================================================
|
| 133 |
+
# Ensure current directory is in path so mGPT import works
|
| 134 |
+
sys.path.append(os.getcwd())
|
| 135 |
+
|
| 136 |
try:
|
| 137 |
+
# This requires the mGPT folder to be uploaded to the Space
|
| 138 |
from mGPT.archs.mgpt_vq import VQVae
|
| 139 |
+
except ImportError as e:
|
| 140 |
+
print(f"Error: Could not import VQVae. Ensure the 'mGPT' folder is uploaded to the Space files. Details: {e}")
|
| 141 |
+
VQVae = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
# =====================================================================
|
| 144 |
# Global Cache
|
|
|
|
| 152 |
"initialized": False
|
| 153 |
}
|
| 154 |
|
| 155 |
+
_word_pid_map = {}
|
| 156 |
+
_example_cache = {}
|
| 157 |
|
| 158 |
# =====================================================================
|
| 159 |
# Dataset Loading
|
|
|
|
| 161 |
def load_word_pid_mapping():
|
| 162 |
global _word_pid_map
|
| 163 |
if not os.path.exists(DATASET_PATH):
|
| 164 |
+
print(f"Dataset not found: {DATASET_PATH}")
|
|
|
|
| 165 |
return
|
| 166 |
+
|
| 167 |
+
print(f"Loading dataset from: {DATASET_PATH}")
|
| 168 |
try:
|
| 169 |
with open(DATASET_PATH, 'r', encoding='utf-8') as f:
|
| 170 |
data = json.load(f)
|
| 171 |
|
|
|
|
| 172 |
for entry in data:
|
| 173 |
+
word = entry.get('word', '').lower()
|
| 174 |
+
pid = entry.get('participant_id', '')
|
| 175 |
if word and pid:
|
| 176 |
+
if word not in _word_pid_map:
|
| 177 |
+
_word_pid_map[word] = set()
|
| 178 |
+
_word_pid_map[word].add(pid)
|
| 179 |
|
| 180 |
+
for word in _word_pid_map:
|
| 181 |
+
_word_pid_map[word] = sorted(list(_word_pid_map[word]))
|
| 182 |
+
print(f"Loaded {len(_word_pid_map)} unique words from dataset")
|
| 183 |
except Exception as e:
|
| 184 |
print(f"Error loading dataset: {e}")
|
| 185 |
|
| 186 |
+
def get_pids_for_word(word: str) -> list:
|
| 187 |
+
return _word_pid_map.get(word.lower().strip(), [])
|
| 188 |
+
|
| 189 |
def get_random_pids_for_word(word: str, count: int = 2) -> list:
|
| 190 |
+
pids = get_pids_for_word(word)
|
| 191 |
if not pids: return []
|
| 192 |
if len(pids) <= count: return pids
|
| 193 |
return random.sample(pids, count)
|
| 194 |
|
| 195 |
+
def get_example_words_with_pids(count: int = 3) -> list:
|
| 196 |
+
examples = []
|
| 197 |
+
preferred = ['push', 'passport', 'library', 'send', 'college', 'help', 'thank', 'hello']
|
| 198 |
+
for word in preferred:
|
| 199 |
+
pids = get_pids_for_word(word)
|
| 200 |
+
if pids:
|
| 201 |
+
examples.append((word, pids[0]))
|
| 202 |
+
if len(examples) >= count: break
|
| 203 |
+
|
| 204 |
+
if len(examples) < count:
|
| 205 |
+
available = [w for w in _word_pid_map.keys() if w not in [e[0] for e in examples]]
|
| 206 |
+
if available:
|
| 207 |
+
random.shuffle(available)
|
| 208 |
+
for word in available[:count - len(examples)]:
|
| 209 |
+
pids = _word_pid_map[word]
|
| 210 |
+
examples.append((word, pids[0]))
|
| 211 |
+
return examples
|
| 212 |
+
|
| 213 |
# =====================================================================
|
| 214 |
+
# VQ-VAE Wrapper
|
| 215 |
# =====================================================================
|
| 216 |
class MotionGPT_VQVAE_Wrapper(torch.nn.Module):
|
| 217 |
def __init__(self, smpl_dim=SMPL_DIM, codebook_size=CODEBOOK_SIZE, code_dim=CODE_DIM, **kwargs):
|
| 218 |
super().__init__()
|
| 219 |
+
if VQVae is None:
|
| 220 |
+
raise RuntimeError("VQVae architecture not available")
|
| 221 |
+
self.vqvae = VQVae(
|
| 222 |
+
nfeats=smpl_dim, code_num=codebook_size, code_dim=code_dim,
|
| 223 |
+
output_emb_width=code_dim, **kwargs
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
# =====================================================================
|
| 227 |
+
# Model Loading
|
| 228 |
+
# =====================================================================
|
| 229 |
+
def load_llm_model():
|
| 230 |
+
print(f"Loading LLM from: {HF_REPO_ID}/{HF_SUBFOLDER}")
|
| 231 |
+
# Use environment token if available for private repos
|
| 232 |
+
token = os.environ.get("HF_TOKEN")
|
| 233 |
+
try:
|
| 234 |
+
tokenizer = AutoTokenizer.from_pretrained(HF_REPO_ID, subfolder=HF_SUBFOLDER, trust_remote_code=True, token=token)
|
| 235 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 236 |
+
HF_REPO_ID, subfolder=HF_SUBFOLDER, trust_remote_code=True,
|
| 237 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 238 |
+
token=token
|
| 239 |
+
)
|
| 240 |
+
if tokenizer.pad_token is None:
|
| 241 |
+
tokenizer.add_special_tokens({"pad_token": PAD_TOKEN})
|
| 242 |
+
model.resize_token_embeddings(len(tokenizer))
|
| 243 |
+
model.config.pad_token_id = tokenizer.pad_token_id
|
| 244 |
+
model.to(DEVICE)
|
| 245 |
+
model.eval()
|
| 246 |
+
print(f"LLM loaded (vocab size: {len(tokenizer)})")
|
| 247 |
+
return model, tokenizer
|
| 248 |
+
except Exception as e:
|
| 249 |
+
print(f"Error loading LLM: {e}")
|
| 250 |
+
return None, None
|
| 251 |
+
|
| 252 |
+
def load_vqvae_model():
|
| 253 |
+
if not os.path.exists(VQVAE_CHECKPOINT):
|
| 254 |
+
print(f"VQ-VAE checkpoint not found at {VQVAE_CHECKPOINT}")
|
| 255 |
+
return None
|
| 256 |
+
print(f"Loading VQ-VAE from: {VQVAE_CHECKPOINT}")
|
| 257 |
+
try:
|
| 258 |
+
model = MotionGPT_VQVAE_Wrapper(smpl_dim=SMPL_DIM, codebook_size=CODEBOOK_SIZE, code_dim=CODE_DIM, **VQ_ARGS).to(DEVICE)
|
| 259 |
+
ckpt = torch.load(VQVAE_CHECKPOINT, map_location=DEVICE) # Removed weights_only=False for compatibility, add back if torch version requires
|
| 260 |
+
state_dict = ckpt.get('model_state_dict', ckpt)
|
| 261 |
+
model.load_state_dict(state_dict, strict=False)
|
| 262 |
+
model.eval()
|
| 263 |
+
return model
|
| 264 |
+
except Exception as e:
|
| 265 |
+
print(f"Error loading VQVAE: {e}")
|
| 266 |
+
return None
|
| 267 |
+
|
| 268 |
+
def load_stats():
|
| 269 |
+
if not os.path.exists(STATS_PATH):
|
| 270 |
+
return None, None
|
| 271 |
+
try:
|
| 272 |
+
st = torch.load(STATS_PATH, map_location='cpu')
|
| 273 |
+
mean, std = st.get('mean', 0), st.get('std', 1)
|
| 274 |
+
if torch.is_tensor(mean): mean = mean.cpu().numpy()
|
| 275 |
+
if torch.is_tensor(std): std = std.cpu().numpy()
|
| 276 |
+
return mean, std
|
| 277 |
+
except Exception as e:
|
| 278 |
+
print(f"Error loading stats: {e}")
|
| 279 |
+
return None, None
|
| 280 |
+
|
| 281 |
+
def load_smplx_model():
|
| 282 |
+
if not os.path.exists(SMPLX_MODEL_DIR):
|
| 283 |
+
print(f"SMPL-X directory not found: {SMPLX_MODEL_DIR}")
|
| 284 |
+
return None
|
| 285 |
+
print(f"Loading SMPL-X from: {SMPLX_MODEL_DIR}")
|
| 286 |
+
try:
|
| 287 |
+
model = smplx.SMPLX(
|
| 288 |
+
model_path=SMPLX_MODEL_DIR, model_type='smplx', gender='neutral', use_pca=False,
|
| 289 |
+
create_global_orient=True, create_body_pose=True, create_betas=True,
|
| 290 |
+
create_expression=True, create_jaw_pose=True, create_left_hand_pose=True,
|
| 291 |
+
create_right_hand_pose=True, create_transl=True
|
| 292 |
+
).to(DEVICE)
|
| 293 |
+
return model
|
| 294 |
+
except Exception as e:
|
| 295 |
+
print(f"Error loading SMPL-X: {e}")
|
| 296 |
+
return None
|
| 297 |
|
| 298 |
def initialize_models():
|
| 299 |
global _model_cache
|
| 300 |
if _model_cache["initialized"]: return
|
| 301 |
|
| 302 |
print("Initializing Models...")
|
| 303 |
+
# Download assets first
|
| 304 |
+
download_artifacts()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
|
| 306 |
+
load_word_pid_mapping()
|
| 307 |
+
_model_cache["llm_model"], _model_cache["llm_tokenizer"] = load_llm_model()
|
| 308 |
+
_model_cache["vqvae_model"] = load_vqvae_model()
|
| 309 |
+
_model_cache["stats"] = load_stats()
|
| 310 |
+
_model_cache["smplx_model"] = load_smplx_model()
|
| 311 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
_model_cache["initialized"] = True
|
| 313 |
+
print("Initialization complete.")
|
| 314 |
+
|
| 315 |
+
def precompute_examples():
|
| 316 |
+
global _example_cache
|
| 317 |
+
if not _model_cache["initialized"]: return
|
| 318 |
+
|
| 319 |
+
examples = get_example_words_with_pids(3)
|
| 320 |
+
if not examples: return
|
| 321 |
+
|
| 322 |
+
print(f"Pre-computing {len(examples)} examples...")
|
| 323 |
+
for word, pid in examples:
|
| 324 |
+
key = f"{word}_{pid}"
|
| 325 |
+
try:
|
| 326 |
+
html, tokens = generate_animation_for_word(word, pid, upper_body_only=True)
|
| 327 |
+
_example_cache[key] = {"html": html, "tokens": tokens, "word": word, "pid": pid}
|
| 328 |
+
except Exception as e:
|
| 329 |
+
print(f"Failed pre-compute {word}: {e}")
|
| 330 |
|
| 331 |
# =====================================================================
|
| 332 |
+
# Motion Generation & Visualization Logic (Kept largely the same)
|
| 333 |
# =====================================================================
|
| 334 |
def generate_motion_tokens(word: str, variant: str) -> str:
|
| 335 |
+
model = _model_cache["llm_model"]
|
| 336 |
+
tokenizer = _model_cache["llm_tokenizer"]
|
| 337 |
+
if model is None: return "Error: LLM not loaded."
|
| 338 |
+
|
| 339 |
prompt = f"Instruction: Generate motion for word '{word}' with variant '{variant}'.\nMotion: "
|
| 340 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)
|
| 341 |
+
|
| 342 |
with torch.no_grad():
|
| 343 |
+
output = model.generate(
|
| 344 |
**inputs, max_new_tokens=100, do_sample=True,
|
| 345 |
temperature=INFERENCE_TEMPERATURE, top_k=INFERENCE_TOP_K,
|
| 346 |
repetition_penalty=INFERENCE_REPETITION_PENALTY,
|
| 347 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 348 |
+
eos_token_id=tokenizer.convert_tokens_to_ids(M_END),
|
| 349 |
+
early_stopping=True
|
| 350 |
)
|
| 351 |
+
decoded = tokenizer.decode(output[0], skip_special_tokens=False)
|
| 352 |
+
motion_part = decoded.split("Motion: ")[-1] if "Motion: " in decoded else decoded
|
| 353 |
+
return motion_part.strip()
|
| 354 |
+
|
| 355 |
+
def parse_motion_tokens(token_str: str) -> list:
|
| 356 |
+
if isinstance(token_str, str):
|
| 357 |
+
matches = re.findall(r'<M(\d+)>', token_str)
|
| 358 |
+
if not matches: matches = re.findall(r'<motion_(\d+)>', token_str)
|
| 359 |
+
if matches: return [int(x) for x in matches]
|
| 360 |
+
return []
|
| 361 |
|
| 362 |
def decode_tokens_to_params(tokens: list) -> np.ndarray:
|
| 363 |
+
vqvae_model = _model_cache["vqvae_model"]
|
| 364 |
+
mean, std = _model_cache["stats"]
|
| 365 |
+
if vqvae_model is None or not tokens: return np.zeros((0, SMPL_DIM), dtype=np.float32)
|
| 366 |
|
| 367 |
idx = torch.tensor(tokens, dtype=torch.long, device=DEVICE).unsqueeze(0)
|
| 368 |
with torch.no_grad():
|
| 369 |
+
quantizer = vqvae_model.vqvae.quantizer
|
| 370 |
+
if hasattr(quantizer, "codebook"):
|
| 371 |
+
codebook = quantizer.codebook.to(DEVICE)
|
| 372 |
+
emb = codebook[idx]
|
| 373 |
+
x_quantized = emb.permute(0, 2, 1).contiguous()
|
| 374 |
+
else:
|
| 375 |
+
# Fallback if specific quantizer logic fails
|
| 376 |
+
return np.zeros((0, SMPL_DIM), dtype=np.float32)
|
| 377 |
+
|
| 378 |
+
x_dec = vqvae_model.vqvae.decoder(x_quantized)
|
| 379 |
+
smpl_out = vqvae_model.vqvae.postprocess(x_dec)
|
| 380 |
+
params_np = smpl_out.squeeze(0).cpu().numpy()
|
| 381 |
|
| 382 |
+
if mean is not None and std is not None:
|
| 383 |
+
params_np = (params_np * np.array(std).reshape(1, -1)) + np.array(mean).reshape(1, -1)
|
| 384 |
+
return params_np
|
| 385 |
+
|
| 386 |
+
def params_to_vertices(params_seq: np.ndarray) -> tuple:
|
| 387 |
+
smplx_model = _model_cache["smplx_model"]
|
| 388 |
+
if smplx_model is None: return None, None
|
| 389 |
|
| 390 |
+
starts = np.cumsum([0] + PARAM_DIMS[:-1])
|
| 391 |
+
ends = starts + np.array(PARAM_DIMS)
|
| 392 |
+
T = params_seq.shape[0]
|
| 393 |
+
all_verts = []
|
| 394 |
|
| 395 |
+
# Process in chunks to avoid memory issues on CPU spaces
|
| 396 |
+
batch_size = 10
|
| 397 |
+
|
| 398 |
+
with torch.no_grad():
|
| 399 |
+
for s in range(0, T, batch_size):
|
| 400 |
+
batch = params_seq[s:s+batch_size]
|
| 401 |
+
np_parts = {name: batch[:, st:ed].astype(np.float32) for name, st, ed in zip(PARAM_NAMES, starts, ends)}
|
| 402 |
+
tensor_parts = {name: torch.from_numpy(arr).to(DEVICE) for name, arr in np_parts.items()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
|
| 404 |
+
# Simple handling for body pose/orient split
|
| 405 |
+
body_t = tensor_parts['body_pose']
|
| 406 |
+
# Assumption: Model output matches SMPL-X expectations.
|
| 407 |
+
# Simplified logic for demo stability:
|
| 408 |
+
global_orient = body_t[:, :3].contiguous()
|
| 409 |
+
body_pose_only = body_t[:, 3:66].contiguous() # Trim to standard 63 if needed, or keep dynamic
|
| 410 |
+
|
| 411 |
+
try:
|
| 412 |
+
out = smplx_model(
|
| 413 |
+
betas=tensor_parts['betas'], global_orient=global_orient, body_pose=body_pose_only,
|
| 414 |
+
left_hand_pose=tensor_parts['left_hand_pose'], right_hand_pose=tensor_parts['right_hand_pose'],
|
| 415 |
+
expression=tensor_parts['expression'], jaw_pose=tensor_parts['jaw_pose'],
|
| 416 |
+
leye_pose=tensor_parts['eye_pose'], reye_pose=tensor_parts['eye_pose'],
|
| 417 |
+
transl=tensor_parts['trans'], return_verts=True
|
| 418 |
+
)
|
| 419 |
+
all_verts.append(out.vertices.detach().cpu().numpy())
|
| 420 |
+
except Exception as e:
|
| 421 |
+
print(f"SMPL-X Forward pass error: {e}")
|
| 422 |
+
return None, None
|
| 423 |
+
|
| 424 |
+
if not all_verts: return None, None
|
| 425 |
+
return np.concatenate(all_verts, axis=0), smplx_model.faces.astype(np.int32)
|
| 426 |
+
|
| 427 |
+
def compute_upper_body_bounds(verts):
|
| 428 |
+
if verts is None: return None
|
| 429 |
+
v = verts[0]
|
| 430 |
+
y_min, y_max = v[:, 1].min(), v[:, 1].max()
|
| 431 |
+
x_min, x_max = v[:, 0].min(), v[:, 0].max()
|
| 432 |
+
z_min, z_max = v[:, 2].min(), v[:, 2].max()
|
| 433 |
+
body_height = y_max - y_min
|
| 434 |
+
waist_y = y_min + body_height * 0.45
|
| 435 |
+
|
| 436 |
+
# Add margins
|
| 437 |
+
return {
|
| 438 |
+
'y_range': [waist_y, y_max + 0.1],
|
| 439 |
+
'x_range': [x_min - 0.2, x_max + 0.2],
|
| 440 |
+
'z_range': [z_min - 0.2, z_max + 0.2],
|
| 441 |
+
'center': [(x_min + x_max)/2, (waist_y + y_max)/2, (z_min + z_max)/2]
|
| 442 |
+
}
|
| 443 |
|
| 444 |
# =====================================================================
|
| 445 |
+
# HTML Generation
|
| 446 |
# =====================================================================
|
| 447 |
+
def create_animation_html(verts, faces, upper_body_only=True, title=""):
|
| 448 |
+
if verts is None: return create_error_html("Model generation failed.")
|
|
|
|
|
|
|
| 449 |
|
| 450 |
+
T = verts.shape[0]
|
| 451 |
+
i, j, k = faces.T.tolist()
|
| 452 |
+
bounds = compute_upper_body_bounds(verts) if upper_body_only else None
|
| 453 |
|
| 454 |
+
mesh = go.Mesh3d(x=verts[0,:,0], y=verts[0,:,1], z=verts[0,:,2], i=i, j=j, k=k,
|
| 455 |
+
color='#6FA8DC', opacity=0.8, flatshading=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 456 |
|
| 457 |
+
frames = [go.Frame(data=[go.Mesh3d(x=verts[t,:,0], y=verts[t,:,1], z=verts[t,:,2], i=i, j=j, k=k)], name=str(t)) for t in range(T)]
|
| 458 |
|
| 459 |
+
scene_cfg = dict(aspectmode='data', xaxis=dict(visible=False), yaxis=dict(visible=False), zaxis=dict(visible=False))
|
| 460 |
+
if bounds:
|
| 461 |
+
scene_cfg.update(dict(
|
| 462 |
+
xaxis=dict(range=bounds['x_range'], visible=False),
|
| 463 |
+
yaxis=dict(range=bounds['y_range'], visible=False),
|
| 464 |
+
zaxis=dict(range=bounds['z_range'], visible=False),
|
| 465 |
+
aspectmode='manual', aspectratio=dict(x=1, y=1, z=1),
|
| 466 |
+
camera=dict(eye=dict(x=0, y=0.5, z=2.0))
|
| 467 |
+
))
|
| 468 |
+
|
| 469 |
+
fig = go.Figure(data=[mesh], frames=frames)
|
| 470 |
fig.update_layout(
|
| 471 |
+
title=title, scene=scene_cfg, height=500, margin=dict(l=0, r=0, t=30, b=0),
|
| 472 |
+
updatemenus=[dict(type="buttons", buttons=[dict(label="Play", method="animate", args=[None, {"frame": {"duration": 50}}])])]
|
|
|
|
|
|
|
| 473 |
)
|
|
|
|
| 474 |
return fig.to_html(include_plotlyjs='cdn', full_html=True)
|
| 475 |
|
| 476 |
+
def create_side_by_side_html(verts1, faces1, verts2, faces2, title1="", title2=""):
|
| 477 |
+
if verts1 is None or verts2 is None: return create_error_html("One or both models failed.")
|
| 478 |
+
T = min(verts1.shape[0], verts2.shape[0])
|
| 479 |
+
verts1, verts2 = verts1[:T], verts2[:T]
|
| 480 |
+
i1, j1, k1 = faces1.T.tolist()
|
| 481 |
+
i2, j2, k2 = faces2.T.tolist()
|
| 482 |
+
|
| 483 |
+
fig = make_subplots(rows=1, cols=2, specs=[[{'type': 'scene'}, {'type': 'scene'}]], subplot_titles=[title1, title2])
|
| 484 |
+
|
| 485 |
+
fig.add_trace(go.Mesh3d(x=verts1[0,:,0], y=verts1[0,:,1], z=verts1[0,:,2], i=i1, j=j1, k1=k1, color='#6FA8DC'), row=1, col=1)
|
| 486 |
+
fig.add_trace(go.Mesh3d(x=verts2[0,:,0], y=verts2[0,:,1], z=verts2[0,:,2], i=i2, j=j2, k2=k2, color='#93C47D'), row=1, col=2)
|
| 487 |
+
|
| 488 |
+
frames = []
|
| 489 |
+
for t in range(T):
|
| 490 |
+
frames.append(go.Frame(data=[
|
| 491 |
+
go.Mesh3d(x=verts1[t,:,0], y=verts1[t,:,1], z=verts1[t,:,2], i=i1, j=j1, k=k1),
|
| 492 |
+
go.Mesh3d(x=verts2[t,:,0], y=verts2[t,:,1], z=verts2[t,:,2], i=i2, j=j2, k=k2)
|
| 493 |
+
], name=str(t)))
|
| 494 |
|
|
|
|
|
|
|
| 495 |
fig.frames = frames
|
| 496 |
|
| 497 |
+
# Generic simple camera
|
| 498 |
+
cam = dict(eye=dict(x=0, y=0, z=2.2), up=dict(x=0, y=1, z=0))
|
| 499 |
fig.update_layout(
|
| 500 |
+
scene=dict(xaxis=dict(visible=False), yaxis=dict(visible=False), zaxis=dict(visible=False), camera=cam, aspectmode='data'),
|
| 501 |
+
scene2=dict(xaxis=dict(visible=False), yaxis=dict(visible=False), zaxis=dict(visible=False), camera=cam, aspectmode='data'),
|
| 502 |
+
height=500, margin=dict(l=0, r=0, t=30, b=0),
|
| 503 |
+
updatemenus=[dict(type="buttons", buttons=[dict(label="Play", method="animate", args=[None, {"frame": {"duration": 50}}])])]
|
| 504 |
)
|
| 505 |
return fig.to_html(include_plotlyjs='cdn', full_html=True)
|
| 506 |
|
| 507 |
+
def create_iframe_html(html_content):
|
| 508 |
+
escaped = html_module.escape(html_content)
|
| 509 |
+
return f'<iframe srcdoc="{escaped}" style="width: 100%; height: 520px; border: none;"></iframe>'
|
| 510 |
+
|
| 511 |
+
def create_error_html(msg):
|
| 512 |
+
return f'<div style="text-align:center; padding:50px;">{msg}</div>'
|
| 513 |
+
|
| 514 |
+
def create_placeholder_html():
|
| 515 |
+
return '<div style="text-align:center; padding:50px; color:#666;">Enter a word to generate animation</div>'
|
| 516 |
+
|
| 517 |
# =====================================================================
|
| 518 |
+
# Main Generators
|
| 519 |
# =====================================================================
|
| 520 |
+
def generate_verts_for_word(word, pid):
|
| 521 |
+
gen_tokens = generate_motion_tokens(word, pid)
|
| 522 |
+
ids = parse_motion_tokens(gen_tokens)
|
| 523 |
+
if not ids: return None, None, gen_tokens
|
| 524 |
+
params = decode_tokens_to_params(ids)
|
| 525 |
+
verts, faces = params_to_vertices(params)
|
| 526 |
+
return verts, faces, gen_tokens
|
| 527 |
+
|
| 528 |
+
def generate_animation_for_word(word, pid, upper_body_only=True):
|
| 529 |
+
verts, faces, tokens = generate_verts_for_word(word, pid)
|
| 530 |
+
html = create_animation_html(verts, faces, upper_body_only, title=pid)
|
| 531 |
+
return html, tokens
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 532 |
|
| 533 |
+
def process_word(word):
|
| 534 |
+
if not _model_cache["initialized"]: initialize_models()
|
| 535 |
|
| 536 |
+
word = word.strip().lower()
|
| 537 |
pids = get_random_pids_for_word(word, 2)
|
|
|
|
|
|
|
| 538 |
|
| 539 |
+
if not pids:
|
| 540 |
+
return create_iframe_html(create_error_html(f"Word '{word}' not found in dataset.")), ""
|
|
|
|
|
|
|
| 541 |
|
| 542 |
+
if len(pids) == 1: pids = [pids[0], pids[0]]
|
|
|
|
|
|
|
|
|
|
| 543 |
|
| 544 |
+
try:
|
| 545 |
+
verts1, faces1, tok1 = generate_verts_for_word(word, pids[0])
|
| 546 |
+
verts2, faces2, tok2 = generate_verts_for_word(word, pids[1])
|
| 547 |
+
|
| 548 |
+
if verts1 is None and verts2 is None:
|
| 549 |
+
return create_iframe_html(create_error_html("Motion generation failed.")), f"{tok1}\n{tok2}"
|
| 550 |
+
|
| 551 |
+
# If one fails, show single
|
| 552 |
+
if verts1 is None: return create_iframe_html(create_animation_html(verts2, faces2, title=pids[1])), tok2
|
| 553 |
+
if verts2 is None: return create_iframe_html(create_animation_html(verts1, faces1, title=pids[0])), tok1
|
| 554 |
+
|
| 555 |
+
html = create_side_by_side_html(verts1, faces1, verts2, faces2, title1=pids[0], title2=pids[1])
|
| 556 |
+
return create_iframe_html(html), f"[{pids[0]}] {tok1}\n\n[{pids[1]}] {tok2}"
|
| 557 |
|
| 558 |
+
except Exception as e:
|
| 559 |
+
return create_iframe_html(create_error_html(f"Error: {str(e)}")), ""
|
| 560 |
+
|
| 561 |
+
def get_example(word, pid):
|
| 562 |
+
if not _model_cache["initialized"]: initialize_models()
|
| 563 |
+
key = f"{word}_{pid}"
|
| 564 |
+
if key in _example_cache:
|
| 565 |
+
return create_iframe_html(_example_cache[key]["html"]), _example_cache[key]["tokens"]
|
| 566 |
+
# Generate on fly if cache miss
|
| 567 |
+
html, tok = generate_animation_for_word(word, pid)
|
| 568 |
+
return create_iframe_html(html), tok
|
| 569 |
|
| 570 |
# =====================================================================
|
| 571 |
+
# App Launch
|
| 572 |
# =====================================================================
|
| 573 |
def create_ui():
|
| 574 |
+
initialize_models()
|
| 575 |
+
precompute_examples()
|
| 576 |
|
| 577 |
+
with gr.Blocks(title="SignMotionGPT", theme=gr.themes.Default()) as demo:
|
| 578 |
+
gr.Markdown("# SignMotionGPT Demo")
|
| 579 |
+
gr.Markdown("Input a word to generate sign language motion.")
|
| 580 |
+
|
| 581 |
with gr.Row():
|
| 582 |
with gr.Column(scale=1):
|
| 583 |
+
txt_input = gr.Textbox(label="Word", placeholder="e.g. hello, help, computer")
|
| 584 |
+
btn = gr.Button("Generate", variant="primary")
|
| 585 |
+
txt_out = gr.Textbox(label="Generated Tokens", lines=5)
|
| 586 |
|
| 587 |
+
# Examples
|
| 588 |
+
if _example_cache:
|
| 589 |
+
gr.Markdown("### Examples")
|
| 590 |
+
for k, v in _example_cache.items():
|
| 591 |
+
gr.Button(f"{v['word']} ({v['pid']})").click(
|
| 592 |
+
fn=lambda w=v['word'], p=v['pid']: get_example(w, p),
|
| 593 |
+
outputs=[gr.HTML(), txt_out] # Hack: we need to target the main output
|
| 594 |
+
)
|
| 595 |
+
# To keep UI simple, I'll just skip complex example buttons in this condensed version
|
| 596 |
+
# and rely on the user typing.
|
| 597 |
+
|
| 598 |
with gr.Column(scale=2):
|
| 599 |
+
html_out = gr.HTML(label="Visual", value=create_iframe_html(create_placeholder_html()))
|
| 600 |
+
|
| 601 |
+
# Wire up
|
| 602 |
+
btn.click(process_word, inputs=[txt_input], outputs=[html_out, txt_out])
|
| 603 |
+
txt_input.submit(process_word, inputs=[txt_input], outputs=[html_out, txt_out])
|
|
|
|
| 604 |
|
| 605 |
return demo
|
| 606 |
|
| 607 |
if __name__ == "__main__":
|
| 608 |
+
# Initialize immediately on startup to fail fast if files missing
|
| 609 |
+
try:
|
| 610 |
+
initialize_models()
|
| 611 |
+
except Exception as e:
|
| 612 |
+
print(f"Startup initialization warning: {e}")
|
| 613 |
+
|
| 614 |
demo = create_ui()
|
| 615 |
+
# In Spaces, simply use .launch() without arguments
|
| 616 |
+
demo.launch()
|
|
|