Download scripts/make_demo_project.py from raianand/SCQC: direct link, hf CLI and curl.
- Browser
- Download file 2.7 kB
-
https://huggingface.co/spaces/raianand/SCQC/resolve/main/scripts/make_demo_project.py
- Command line
-
hf download hf://spaces/raianand/SCQC/scripts/make_demo_project.py
-
curl -L -o make_demo_project.py https://huggingface.co/spaces/raianand/SCQC/resolve/main/scripts/make_demo_project.py
2.7 kB
| """Load the example manifests into a running SCQC server and upload local assets. | |
| python scripts/make_demo_project.py --server http://127.0.0.1:8000 --token admin-dev | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import mimetypes | |
| import sys | |
| import urllib.error | |
| import urllib.request | |
| import uuid | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parents[1] | |
| EXAMPLES = ROOT / "examples" | |
| def call(method: str, url: str, token: str, body: bytes | None = None, ctype: str = "application/json"): | |
| req = urllib.request.Request(url, data=body, method=method, | |
| headers={"X-SCQC-Token": token, "Content-Type": ctype}) | |
| try: | |
| with urllib.request.urlopen(req, timeout=60) as r: | |
| return r.status, json.loads(r.read() or b"{}") | |
| except urllib.error.HTTPError as e: | |
| return e.code, json.loads(e.read() or b"{}") | |
| def upload_file(url: str, token: str, path: Path): | |
| boundary = uuid.uuid4().hex | |
| mime = mimetypes.guess_type(path.name)[0] or "application/octet-stream" | |
| body = (f"--{boundary}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{path.name}\"\r\n" | |
| f"Content-Type: {mime}\r\n\r\n").encode() + path.read_bytes() + f"\r\n--{boundary}--\r\n".encode() | |
| return call("PUT", url, token, body, f"multipart/form-data; boundary={boundary}") | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--server", default="http://127.0.0.1:8000") | |
| ap.add_argument("--token", default="admin-dev") | |
| ap.add_argument("manifests", nargs="*", default=[str(p) for p in sorted(EXAMPLES.glob("manifest_*.json"))]) | |
| args = ap.parse_args() | |
| ok = True | |
| for mpath in map(Path, args.manifests): | |
| data = json.loads(mpath.read_text(encoding="utf-8")) | |
| status, res = call("POST", f"{args.server}/api/projects?replace=true", args.token, | |
| json.dumps(data, ensure_ascii=False).encode()) | |
| if status >= 300: | |
| ok = False | |
| print(f"✗ {mpath.name}: HTTP {status}") | |
| for e in res.get("errors", [res]): | |
| print(" ", e) | |
| continue | |
| print(f"✓ {data['project_id']}: {res['n_tasks']} tasks") | |
| for aid, asset in data.get("assets", {}).items(): | |
| local = (mpath.parent / asset["url"]) if not asset["url"].startswith("http") else None | |
| if local and local.exists(): | |
| s, r = upload_file(f"{args.server}/api/projects/{data['project_id']}/assets/{aid}", args.token, local) | |
| print(f" asset {aid}: {'uploaded' if s < 300 else f'HTTP {s} {r}'}") | |
| sys.exit(0 if ok else 1) | |
| if __name__ == "__main__": | |
| main() | |