File size: 1,172 Bytes
84cbfcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Observe a deployed Space to readiness without restarting or retrying inference."""
import argparse
from datetime import datetime, timezone
import json
from pathlib import Path
import time
from huggingface_hub import HfApi

p=argparse.ArgumentParser(description=__doc__)
p.add_argument('space');p.add_argument('--timeout',type=int,default=600)
a=p.parse_args();api=HfApi();started=time.perf_counter();rows=[]
out=Path('artifacts/deployment');out.mkdir(parents=True,exist_ok=True)
stamp=datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%S')
while True:
 info=api.space_info(a.space)
 row={'at':datetime.now(timezone.utc).isoformat(),'elapsed_seconds':time.perf_counter()-started,
      'stage':str(info.runtime.stage),'hub_commit':info.sha,'private':info.private,
      'hardware':str(info.runtime.hardware)}
 rows.append(row);print(json.dumps(row),flush=True)
 (out/f'{stamp}-readiness.json').write_text(json.dumps(rows,indent=2))
 if row['stage']=='RUNNING':break
 if row['stage'] in {'BUILD_ERROR','RUNTIME_ERROR','CONFIG_ERROR'} or row['elapsed_seconds']>a.timeout:
  raise SystemExit('Inspect logs before any further mutation')
 time.sleep(10)