Download apps/api/scripts/smoke_small_book.py from aliroya/contiai: direct link, hf CLI and curl.
- Browser
- Download file 7.75 kB
-
https://huggingface.co/spaces/aliroya/contiai/resolve/main/apps/api/scripts/smoke_small_book.py
- Command line
-
hf download hf://spaces/aliroya/contiai/apps/api/scripts/smoke_small_book.py
-
curl -L -o smoke_small_book.py https://huggingface.co/spaces/aliroya/contiai/resolve/main/apps/api/scripts/smoke_small_book.py
7.75 kB
| # ruff: noqa: T201 | |
| """A whole book, small enough to run often. | |
| `smoke_full_book.py` is a hundred pages across three units and costs what a | |
| real book costs, so it gets run once before a release and not while chasing a | |
| defect. This one asks for two chapters and about thirty-five pages - every step | |
| of the pipeline runs, in the same order, through the same API the site calls, | |
| and the whole thing finishes in a fraction of the time and the spend. | |
| It goes through HTTP on purpose. Calling the services directly would skip the | |
| gate, the artifact rows, the ownership checks and the cost ledger - which is | |
| most of what a person actually touches. | |
| Usage: .venv/Scripts/python.exe scripts/smoke_small_book.py [--pages 35] | |
| .venv/Scripts/python.exe scripts/smoke_small_book.py --run <id> | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import sys | |
| import time | |
| sys.path.insert(0, ".") | |
| # Everything this prints is Arabic, and a redirected stdout on Windows is | |
| # cp1252 - which cannot encode a single letter of it. Without this the script | |
| # dies on its first status line while the run it started carries on unwatched. | |
| for stream in (sys.stdout, sys.stderr): | |
| if hasattr(stream, "reconfigure"): | |
| stream.reconfigure(encoding="utf-8", errors="replace") | |
| from scripts.smoke_local import ( # noqa: E402 | |
| ADMIN_EMAIL, | |
| client, | |
| existing_user_id, | |
| token_for, | |
| ) | |
| TOPIC = "ุงูุชุณููู ูุงูุฐูุงุก ุงูุงุตุทูุงุนู" | |
| DESCRIPTION = ( | |
| "ูุชุงุจ ุนู ูู ูุดุฑุญ ููู ูุบููุฑ ุงูุฐูุงุก ุงูุงุตุทูุงุนู ุนู ู ุงูุชุณููู: ู ู ููู ุงูุฌู ููุฑ " | |
| "ูุชูุณูู ูุ ุฅูู ุชูููุฏ ุงูู ุญุชูู ูููุงุณ ุฃุซุฑู. ู ูุฌูู ูู ู ูุนู ู ูู ุงูุชุณููู ููุง " | |
| "ุฎูููุฉ ุชูููุฉ ููุ ูููุฏูู ุงูู ูุงููู ุจูุบุฉ ุงูุนู ู ูุง ุจูุบุฉ ุงูุฎูุงุฑุฒู ูุงุชุ ููุนุฑุถ " | |
| "ููู ุฃุฏุงุฉ ู ุง ุชุตูุญ ูู ูู ุง ูุง ุชุตูุญ." | |
| ) | |
| AUDIENCE = "ู ุฎุชุตู ุงูุชุณููู ูู ุฏูุฑู ุงูุชุณููู ุงูุฑูู ู ูู ุงูุดุฑูุงุช ุงูู ุชูุณุทุฉ" | |
| NOTES = ( | |
| "ูุตูุงู ุงุซูุงู ููุท. ุงูุฃูู ุนู ุฃุซุฑ ุงูุฐูุงุก ุงูุงุตุทูุงุนู ูู ููู ุงูุนู ูู ูุงุณุชูุฏุงููุ " | |
| "ูุงูุซุงูู ุนู ุชูููุฏ ุงูู ุญุชูู ุงูุชุณูููู ูููุงุณ ุฃุซุฑู. ุงุฌุนู ูู ูุตู ุซูุงุซุฉ ู ุญุงูุฑ." | |
| ) | |
| MARK = {"running": "..", "succeeded": "ok", "failed": "!!", | |
| "awaiting_review": "??", "skipped": "--", "queued": " "} | |
| def watch(user, run_id: str, until, limit: int, seen: dict[str, str]) -> dict: | |
| """Print each step as it changes state; return the run when `until` holds.""" | |
| begun = time.time() | |
| body: dict = {} | |
| while time.time() - begun < limit: | |
| response = user.get(f"/v1/runs/{run_id}") | |
| if response.status_code != 200: | |
| print(f" ! run unreadable: {response.status_code} {response.text[:160]}") | |
| time.sleep(5) | |
| continue | |
| body = response.json() | |
| for step in body.get("steps", []): | |
| key, state = step["step_key"], step["status"] | |
| if seen.get(key) != state and state != "pending": | |
| minutes = int((time.time() - begun) // 60) | |
| print(f" {MARK.get(state, state)} {key} ({minutes}ุฏ)") | |
| seen[key] = state | |
| if until(body["status"]): | |
| return body | |
| time.sleep(6) | |
| print(f" ! timed out after {limit}s") | |
| return body | |
| def show_failure(body: dict) -> None: | |
| print("\nerror:") | |
| print(str(body.get("error"))[:2000]) | |
| for step in body.get("steps", []): | |
| if step["status"] == "failed": | |
| print(f"\nfailed at {step['step_key']}:") | |
| print(str(step.get("error"))[:3000]) | |
| def main() -> int: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--pages", type=int, default=35) | |
| parser.add_argument("--timeout", type=int, default=5400) | |
| parser.add_argument("--run", help="watch an existing run instead of starting one") | |
| args = parser.parse_args() | |
| admin_id = existing_user_id(ADMIN_EMAIL) | |
| if admin_id is None: | |
| print("no bootstrap admin in the database; sign in once first") | |
| return 2 | |
| user = client(token_for(ADMIN_EMAIL, admin_id)) | |
| seen: dict[str, str] = {} | |
| if args.run: | |
| run_id = args.run | |
| project_id = user.get(f"/v1/runs/{run_id}").json()["project_id"] | |
| print(f"watching run {run_id}") | |
| else: | |
| project = user.post( | |
| "/v1/projects", | |
| json={"kind": "book", "title": f"{TOPIC} ({args.pages} ุตูุญุฉ)"}, | |
| ) | |
| if project.status_code != 201: | |
| print("could not create the project:", project.status_code, project.text[:300]) | |
| return 1 | |
| project_id = project.json()["id"] | |
| started = user.post( | |
| f"/v1/projects/{project_id}/runs", | |
| json={"params": { | |
| "variant": "from_scratch", | |
| "brief": { | |
| "topic": TOPIC, | |
| "units_number": 2, | |
| "course_audience": AUDIENCE, | |
| "course_description": DESCRIPTION, | |
| "notes": NOTES, | |
| "total_pages": args.pages, | |
| }, | |
| }}, | |
| ) | |
| if started.status_code != 202: | |
| print("run refused:", started.status_code, started.text[:400]) | |
| return 1 | |
| run_id = started.json()["id"] | |
| print(f"project {project_id}\nrun {run_id}\n") | |
| print("== to the gate ==") | |
| body = watch(user, run_id, lambda s: s not in ("queued", "running"), 1800, seen) | |
| if body.get("status") == "awaiting_review": | |
| review = user.get(f"/v1/runs/{run_id}/review").json() | |
| chapters = review.get("blueprint", {}).get("chapters", []) | |
| print(f"\n blueprint: {len(chapters)} chapters") | |
| for chapter in chapters: | |
| sections = chapter.get("sections") or [] | |
| print(f" - {chapter.get('chapter_title', '?')} ({len(sections)} ู ุญุงูุฑ)") | |
| for section in sections: | |
| subs = section.get("subsections") or [] | |
| print(f" ยท {section.get('title', '?')} ({len(subs)} ู ูุงุถูุน)") | |
| print("\n== approved ==") | |
| approved = user.post(f"/v1/runs/{run_id}/approve", json={}) | |
| if approved.status_code != 202: | |
| print("approve refused:", approved.status_code, approved.text[:300]) | |
| return 1 | |
| elif body.get("status") not in ("succeeded", "running", "queued"): | |
| print("did not reach the gate:", body.get("status")) | |
| show_failure(body) | |
| return 1 | |
| body = watch(user, run_id, lambda s: s in ("succeeded", "failed", "cancelled"), | |
| args.timeout, seen) | |
| status = body.get("status") | |
| print(f"\nfinal status: {status}") | |
| if status != "succeeded": | |
| show_failure(body) | |
| return 1 | |
| artifacts = user.get(f"/v1/projects/{project_id}/artifacts").json().get("items", []) | |
| print(f"\nartifacts: {len(artifacts)}") | |
| for artifact in artifacts: | |
| fetched = user.get(f"/v1/artifacts/{artifact['id']}") | |
| print(f" {artifact['format']:5} {artifact['title'][:46]:48} " | |
| f"{(artifact['size_bytes'] or 0):>9,}b download={fetched.status_code} " | |
| f"{len(fetched.content):>9,}b") | |
| spend = user.get(f"/v1/admin/users/{admin_id}/by-task-type?days=1").json() | |
| book = next((r for r in spend if r["task_type"] == "book"), None) | |
| if book: | |
| print(f"\ncost today on books: ${book['cost_usd']} " | |
| f"({book['total_tokens']:,} tokens)") | |
| else: | |
| print("\n!! no cost recorded for this run - the meter is not seeing the calls") | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |