Spaces:
Running on T4
Running on T4
File size: 3,094 Bytes
b13d358 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | """Startup-only approved-provider model acquisition. Never receives a video."""
import json
import os
from pathlib import Path
import subprocess
import sys
from tools.setup_full_worker import SAM_REVISION,checksum
def main():
root=Path(os.environ.get('BASEBALL_DATA_ROOT','/data'));root.mkdir(parents=True,exist_ok=True)
record_path=Path(os.environ.get('BASEBALL_SAM_WORKER_RECORD',str(root/'sam-api-worker.json')))
status=root/'sam-setup-status.json'
def state(value,reason=None):status.write_text(json.dumps({'status':value,'reason_code':reason}))
try:
if record_path.is_file():
record=json.loads(record_path.read_text())
if all(Path(record.get(key,'/does-not-exist')).exists() for key in
('worker_python','sam_repo','sam_checkpoint','sam_config','mhr_path')):
state('configured');return 0
if not os.environ.get('HF_TOKEN'):
state('unavailable','hf_token_required_for_approved_sam_weights');return 1
source=Path(os.environ.get('BASEBALL_SAM_SOURCE','/opt/sam-source'))
revision=subprocess.check_output(['git','-C',str(source),'rev-parse','HEAD'],text=True).strip()
if revision!=SAM_REVISION:state('unavailable','sam_source_revision_mismatch');return 1
state('downloading_approved_sam_assets')
from huggingface_hub import HfApi,snapshot_download
model_revision=os.environ.get('BASEBALL_SAM_MODEL_REVISION') or HfApi().model_info('facebook/sam-3d-body-vith').sha
snapshot=Path(snapshot_download('facebook/sam-3d-body-vith',revision=model_revision,
allow_patterns=['model.ckpt','model_config.yaml','assets/mhr_model.pt'],
cache_dir=root/'sam-cache'))
assets={'sam_checkpoint':snapshot/'model.ckpt','sam_config':snapshot/'model_config.yaml','mhr_path':snapshot/'assets/mhr_model.pt'}
if not all(path.is_file() for path in assets.values()):raise ValueError('missing_assets')
record={'worker_python':sys.executable,'sam_repo':str(source),'source_revision':revision,
'hf_revision':model_revision,**{k:str(v) for k,v in assets.items()},
'artifact_hashes':{key:checksum(value) for key,value in assets.items()},
'status':'installed_requires_inference','scope':'SAM body and hand inference; no anatomy or fitting'}
record_path.parent.mkdir(parents=True,exist_ok=True)
tmp=record_path.with_suffix('.tmp');tmp.write_text(json.dumps(record,indent=2));tmp.replace(record_path)
state('configured');return 0
except Exception as error:
# Do not echo provider exceptions, request headers or credential values.
code=getattr(getattr(error,'response',None),'status_code',None)
reason={401:'sam_provider_authentication_failed',403:'sam_provider_access_denied',
404:'sam_model_or_revision_not_found'}.get(code,'sam_asset_setup_failed')
state('unavailable',reason)
print('SAM setup unavailable: '+reason,flush=True)
return 1
if __name__=='__main__':raise SystemExit(main())
|