Tharun156 commited on
Commit
f54f126
Β·
verified Β·
1 Parent(s): b37b470

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -0
app.py CHANGED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import sys
4
+ import tarfile
5
+ import zipfile
6
+ from pathlib import Path
7
+
8
+ from huggingface_hub import snapshot_download
9
+
10
+ # ---------------------------------------------------------------------------
11
+ # Optional: pull checkpoints and auxiliary assets at startup. Set the
12
+ # HF_GESTURELSM_WEIGHTS_REPO environment variable in the Space settings to the
13
+ # dataset or model repo that hosts the pre-trained weights (e.g. "username/gesturelsm-assets").
14
+ # Files will be placed under ckpt/ so the existing config paths keep working.
15
+ # ---------------------------------------------------------------------------
16
+ BASE_DIR = Path(__file__).parent.resolve()
17
+ ROOT_DIR = BASE_DIR.parent.resolve()
18
+
19
+ # Ensure project root is on sys.path so intra-repo imports (e.g. `optimizers`) work.
20
+ if str(ROOT_DIR) not in sys.path:
21
+ sys.path.insert(0, str(ROOT_DIR))
22
+
23
+ # Quick sanity check in Space logs to confirm the repo root is visible at runtime.
24
+ print("[GestureLSM] sys.path:")
25
+ for entry in sys.path:
26
+ print(" ", entry)
27
+ ASSET_CACHE_DIR = BASE_DIR / "asset_cache"
28
+ ASSET_CACHE_DIR.mkdir(parents=True, exist_ok=True)
29
+
30
+ weights_repo = os.environ.get("HF_GESTURELSM_WEIGHTS_REPO", "").strip()
31
+ if weights_repo:
32
+ downloaded_root = Path(
33
+ snapshot_download(
34
+ repo_id=weights_repo,
35
+ repo_type="dataset",
36
+ local_dir=ASSET_CACHE_DIR,
37
+ local_dir_use_symlinks=False,
38
+ allow_patterns=[
39
+ "*.pth",
40
+ "*.pt",
41
+ "*.bin",
42
+ "*.npz",
43
+ "*.npy",
44
+ "*.tar",
45
+ "*.tar.gz",
46
+ "*.zip",
47
+ ],
48
+ )
49
+ )
50
+ else:
51
+ downloaded_root = ASSET_CACHE_DIR
52
+
53
+
54
+ def _sync_downloaded_assets(download_root: Path) -> None:
55
+ """Copy all downloaded asset files into the repo root."""
56
+
57
+ allowed_suffixes = {
58
+ ".pth",
59
+ ".pt",
60
+ ".bin",
61
+ ".npz",
62
+ ".npy",
63
+ ".tar",
64
+ ".gz",
65
+ ".zip",
66
+ }
67
+
68
+ anchor_dirs = {"ckpt", "mean_std", "datasets", "weights", "hf_assets"}
69
+
70
+ copied = 0
71
+ skipped = 0
72
+ for file_path in download_root.rglob("*"):
73
+ if not file_path.is_file():
74
+ continue
75
+ if file_path.suffix.lower() not in allowed_suffixes:
76
+ continue
77
+ relative = file_path.relative_to(download_root)
78
+ parts = relative.parts
79
+ anchor_index = next((idx for idx, part in enumerate(parts) if part in anchor_dirs), None)
80
+ if anchor_index is None:
81
+ skipped += 1
82
+ continue
83
+ destination = ROOT_DIR / Path(*parts[anchor_index:])
84
+ destination.parent.mkdir(parents=True, exist_ok=True)
85
+ try:
86
+ shutil.copy2(file_path, destination)
87
+ copied += 1
88
+ except Exception as exc: # pragma: no cover - defensive
89
+ print(f"[GestureLSM] Failed to copy {file_path} -> {destination}: {exc}")
90
+
91
+ if copied:
92
+ print(f"[GestureLSM] Synced {copied} asset files from {download_root} to repository root")
93
+ elif skipped:
94
+ print(f"[GestureLSM] Skipped {skipped} files with no anchor directory match in {download_root}")
95
+
96
+
97
+
98
+ _sync_downloaded_assets(downloaded_root)
99
+
100
+ # Ensure expected runtime directories exist so the demo can write outputs.
101
+ datasets_hub_dir = ROOT_DIR / "datasets" / "hub"
102
+ for relative in ["outputs/audio2pose", "datasets/BEAT_SMPL", "datasets/hub"]:
103
+ (ROOT_DIR / relative).mkdir(parents=True, exist_ok=True)
104
+
105
+ smplx_dest = datasets_hub_dir / "smplx_models"
106
+ smplx_dest.mkdir(parents=True, exist_ok=True)
107
+
108
+ if not any(smplx_dest.iterdir()):
109
+ smplx_sources = list(ASSET_CACHE_DIR.glob("**/smplx_models"))
110
+ if smplx_sources:
111
+ smplx_source = smplx_sources[0]
112
+ for item in smplx_source.iterdir():
113
+ target = smplx_dest / item.name
114
+ if item.is_dir():
115
+ shutil.copytree(item, target, dirs_exist_ok=True)
116
+ else:
117
+ shutil.copy2(item, target)
118
+
119
+ if not any(smplx_dest.iterdir()):
120
+ archives = list(ASSET_CACHE_DIR.glob("**/smplx_models.*"))
121
+ for archive in archives:
122
+ try:
123
+ if zipfile.is_zipfile(archive):
124
+ with zipfile.ZipFile(archive) as zf:
125
+ zf.extractall(smplx_dest)
126
+ elif tarfile.is_tarfile(archive):
127
+ with tarfile.open(archive) as tf:
128
+ tf.extractall(smplx_dest)
129
+ except Exception as exc:
130
+ print(f"[GestureLSM] Failed to extract {archive}: {exc}")
131
+
132
+ if not any(smplx_dest.iterdir()):
133
+ print("[GestureLSM] WARNING: smplx_models directory missing; ensure the weights repo contains it.")
134
+
135
+ nested_smplx = smplx_dest / "smplx"
136
+ if nested_smplx.exists() and nested_smplx.is_dir():
137
+ for item in nested_smplx.iterdir():
138
+ target = smplx_dest / item.name
139
+ if item.is_dir():
140
+ shutil.copytree(item, target, dirs_exist_ok=True)
141
+ shutil.rmtree(item)
142
+ else:
143
+ shutil.move(str(item), str(target))
144
+ try:
145
+ nested_smplx.rmdir()
146
+ except OSError:
147
+ pass
148
+
149
+ print("[GestureLSM] smplx_models contents:")
150
+ for item in smplx_dest.glob("*"):
151
+ print(" -", item.name)
152
+
153
+ # Reuse the existing Gradio interface defined in demo.py.
154
+ from demo import demo as gesture_demo # noqa: E402
155
+
156
+ if __name__ == "__main__":
157
+ gesture_demo.queue(concurrency_count=1).launch(server_name="0.0.0.0", share=False)