File size: 1,689 Bytes
2274b3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Read-only runtime/log snapshot for the single approved Phase 1 Space."""
import argparse
import json
import time
from pathlib import Path
from huggingface_hub import HfApi
import requests

ROOT = Path(__file__).resolve().parents[2]
OUT = ROOT / 'development-support/zerogpu-phase1'
REPO = 'Wizzas/DefectRAG'


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--logs', choices=('build', 'run'))
    args = parser.parse_args()
    api = HfApi()
    runtime = api.get_space_runtime(REPO).raw
    result = {'runtime': runtime, 'commit': api.space_info(REPO).sha}
    (OUT / 'space-runtime.json').write_text(json.dumps(result, indent=2))
    print(json.dumps(result, indent=2))
    if args.logs:
        lines = []
        try:
            with requests.get(f'https://huggingface.co/api/spaces/{REPO}/logs/{args.logs}',
                              headers={'Authorization': f'Bearer {api.token or __import__("huggingface_hub").get_token()}'},
                              stream=True, timeout=(15, 12)) as response:
                response.raise_for_status()
                started = time.monotonic()
                for line in response.iter_lines(chunk_size=1):
                    if line:
                        lines.append(line.decode('utf8', errors='replace'))
                    if len(lines) >= 700 or time.monotonic() - started >= 20:
                        break
        except requests.RequestException as exc:
            lines.append(f'Log stream ended: {type(exc).__name__}')
        (OUT / f'space-{args.logs}.log').write_text('\n'.join(lines), encoding='utf8')
        print('\n'.join(lines[-45:]))


if __name__ == '__main__':
    main()