baseball-engine / tools /prepare_gvhmr_assets.py
github-actions[bot]
Automated Deploy: 759e1f2c3b1e3cd4217894adb0dd9808d7b67b8a
4bf9ad1
Raw History Blame Contribute Delete
4.31 kB
"""Startup-only GVHMR assets. Licensed SMPL-X must be supplied by the operator.
Never fetch licensed body models from public mirrors. An explicit operator-owned
HF model repository or mounted file is required; HF_TOKEN uses normal hub auth.
"""
import os
from pathlib import Path
import subprocess
import sys
from baseball_batting.storage import atomic_json,sha256_file
from baseball_batting.motion_adapters import GVHMR_REVISION
WEIGHTS={'gvhmr_checkpoint':'gvhmr/gvhmr_siga24_release.ckpt',
'hmr2_checkpoint':'hmr2/epoch=10-step=25000.ckpt',
'vitpose_checkpoint':'vitpose/vitpose-h-multi-coco.pth'}
def link(source,destination):
destination.parent.mkdir(parents=True,exist_ok=True)
source=Path(source).resolve()
if destination.exists() or destination.is_symlink():
if destination.resolve()==source:return
if not destination.is_symlink():raise ValueError('Existing GVHMR asset differs; register an isolated source checkout')
destination.unlink()
destination.symlink_to(source)
def main():
root=Path(os.environ.get('BASEBALL_DATA_ROOT','/data'));root.mkdir(parents=True,exist_ok=True)
status=root/'gvhmr-setup-status.json'
destination=Path(os.environ.get('BASEBALL_GVHMR_WORKER_RECORD',str(root/'gvhmr-api-worker.json')))
def state(reason=None):
if reason:destination.unlink(missing_ok=True)
atomic_json(status,{'status':'configured' if reason is None else 'unavailable','reason_code':reason})
try:
source=Path(os.environ.get('BASEBALL_GVHMR_SOURCE','/opt/gvhmr-source'))
if not source.is_dir():state('gvhmr_runtime_not_installed');return 1
rev=subprocess.check_output(['git','-C',str(source),'rev-parse','HEAD'],text=True).strip()
if rev!=GVHMR_REVISION:state('gvhmr_source_revision_mismatch');return 1
body=os.environ.get('BASEBALL_SMPLX_MODEL_PATH')
body_repo=os.environ.get('BASEBALL_SMPLX_REPO')
if not body and not body_repo:
state('gvhmr_smplx_license_asset_required');return 1
from huggingface_hub import HfApi,hf_hub_download
api=HfApi();body_revision=None
if body_repo:
body_revision=os.environ.get('BASEBALL_SMPLX_REVISION') or api.model_info(body_repo).sha
body=hf_hub_download(body_repo,os.environ.get('BASEBALL_SMPLX_FILENAME','SMPLX_NEUTRAL.npz'),
revision=body_revision,cache_dir=root/'gvhmr-cache')
body=Path(body)
if not body.is_file():state('gvhmr_smplx_file_missing');return 1
# This is the checkpoint mirror referenced by the upstream project's own
# linked HF demo. Only the three non-body-model assets are downloaded.
weights_repo=os.environ.get('BASEBALL_GVHMR_WEIGHTS_REPO','camenduru/GVHMR')
weights_revision=os.environ.get('BASEBALL_GVHMR_WEIGHTS_REVISION') or api.model_info(weights_repo).sha
assets={key:Path(hf_hub_download(weights_repo,filename,revision=weights_revision,
cache_dir=root/'gvhmr-cache')) for key,filename in WEIGHTS.items()}
assets['smplx_model']=body
for key,filename in WEIGHTS.items():link(assets[key],source/'inputs/checkpoints'/filename)
link(body,source/'inputs/checkpoints/body_models/smplx/SMPLX_NEUTRAL.npz')
record={'worker_python':sys.executable,'gvhmr_repo':str(source.resolve()),'source_revision':rev,
**{key:str(path.resolve()) for key,path in assets.items()},
'artifact_hashes':{key:sha256_file(path) for key,path in assets.items()},
'weights_repository':weights_repo,'weights_revision':weights_revision,'body_model_revision':body_revision,
'status':'installed_requires_inference','license_notice':'Operator supplies licensed SMPL-X; GVHMR research/non-commercial license applies'}
atomic_json(destination,record)
state();return 0
except Exception as error:
code=getattr(getattr(error,'response',None),'status_code',None)
reason={401:'gvhmr_asset_authentication_failed',403:'gvhmr_asset_access_denied',404:'gvhmr_asset_not_found'}.get(code,'gvhmr_asset_setup_failed')
state(reason);print('GVHMR setup unavailable: '+reason,flush=True);return 1
if __name__=='__main__':raise SystemExit(main())