Download apps/api/scripts/smoke_full_book.py from aliroya/contiai: direct link, hf CLI and curl.
- Browser
- Download file 6.25 kB
-
https://huggingface.co/spaces/aliroya/contiai/resolve/main/apps/api/scripts/smoke_full_book.py
- Command line
-
hf download hf://spaces/aliroya/contiai/apps/api/scripts/smoke_full_book.py
-
curl -L -o smoke_full_book.py https://huggingface.co/spaces/aliroya/contiai/resolve/main/apps/api/scripts/smoke_full_book.py
6.25 kB
| # ruff: noqa: T201 | |
| """One whole book, start to finished file. | |
| The route tests stop at the gate because that is where the cheap half ends. | |
| This one approves the blueprint and lets the other nine steps run - search, | |
| ingest, RAG, planning, writing, cohesion, enrichment, export - which is the | |
| only way to find what breaks in the half nobody exercises. | |
| It is expensive and slow by nature. Run it deliberately. | |
| Usage: .venv/Scripts/python.exe scripts/smoke_full_book.py [--pages 25] | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import sys | |
| import time | |
| sys.path.insert(0, ".") | |
| from scripts.smoke_local import ( # noqa: E402 | |
| ADMIN_EMAIL, | |
| client, | |
| existing_user_id, | |
| token_for, | |
| ) | |
| OUTLINE = """كتاب أصول المراسلات الرسمية | |
| كتاب عملي موجّه لموظفي العلاقات العامة في الجهات الحكومية، يشرح كتابة | |
| الخطابات الرسمية والرد عليها وأرشفتها. | |
| الوحدة الأولى: مبادئ المراسلة الرسمية | |
| - التحية الافتتاحية وصيغها | |
| - بنية الخطاب الرسمي | |
| - لغة المخاطبة ومستوياتها | |
| الوحدة الثانية: الرد والمتابعة | |
| - مهلة الرد ومعاييرها | |
| - صيغ الاعتذار المهني | |
| - متابعة الخطابات غير المجاب عنها | |
| الوحدة الثالثة: الأرشفة والتوثيق | |
| - ترقيم الصادر والوارد | |
| - حفظ النسخ الورقية والرقمية | |
| """ | |
| def main() -> int: | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--pages", type=int, default=25) | |
| parser.add_argument("--timeout", type=int, default=5400, | |
| help="seconds to wait for the whole run") | |
| 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)) | |
| project = user.post( | |
| "/v1/projects", | |
| json={"kind": "book", "title": f"كتاب المراسلات الرسمية ({args.pages} صفحة)"}, | |
| ) | |
| if project.status_code != 201: | |
| print("could not create the project:", project.status_code, project.text[:200]) | |
| return 1 | |
| project_id = project.json()["id"] | |
| print(f"project {project_id}") | |
| started = user.post( | |
| f"/v1/projects/{project_id}/runs", | |
| json={"params": { | |
| "variant": "from_text", | |
| "brief": { | |
| "blueprint_text": OUTLINE, | |
| "course_name": "أصول المراسلات الرسمية", | |
| "total_pages": args.pages, | |
| }, | |
| }}, | |
| ) | |
| if started.status_code != 202: | |
| print("run refused:", started.status_code, started.text[:300]) | |
| return 1 | |
| run_id = started.json()["id"] | |
| print(f"run {run_id}") | |
| print() | |
| def poll(label: str, until, limit: int) -> dict: | |
| """Wait, printing each step as it changes state.""" | |
| seen: dict[str, str] = {} | |
| 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": | |
| mark = {"running": "..", "succeeded": "ok", "failed": "!!", | |
| "awaiting_review": "??", "skipped": "--"}.get(state, state) | |
| print(f" {mark} {key}") | |
| seen[key] = state | |
| if until(body["status"]): | |
| return body | |
| time.sleep(6) | |
| print(f" ! timed out waiting for {label} after {limit}s") | |
| return body | |
| print("== to the gate ==") | |
| body = poll("the gate", lambda s: s not in ("queued", "running"), 900) | |
| if body.get("status") != "awaiting_review": | |
| print("did not reach the gate:", body.get("status")) | |
| print(str(body.get("error"))[:600]) | |
| return 1 | |
| review = user.get(f"/v1/runs/{run_id}/review") | |
| chapters = review.json().get("blueprint", {}).get("chapters", []) | |
| print(f"\n blueprint: {len(chapters)} chapters") | |
| for chapter in chapters: | |
| title = chapter.get("chapter_title", "?") | |
| sections = chapter.get("sections") or [] | |
| print(f" - {title} ({len(sections)} sections)") | |
| print("\n== approved; the rest runs in one pass ==") | |
| 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 | |
| body = poll("the run", lambda s: s in ("succeeded", "failed", "cancelled"), | |
| args.timeout) | |
| status = body.get("status") | |
| print(f"\nfinal status: {status}") | |
| if status != "succeeded": | |
| print("\nerror:") | |
| print(str(body.get("error"))[:2000]) | |
| failed = [s for s in body.get("steps", []) if s["status"] == "failed"] | |
| for step in failed: | |
| print(f"\nfailed at {step['step_key']}:") | |
| print(str(step.get("error"))[:2000]) | |
| return 1 | |
| artifacts = user.get(f"/v1/projects/{project_id}/artifacts").json().get("items", []) | |
| print(f"\nartifacts: {len(artifacts)}") | |
| for artifact in artifacts: | |
| print(f" {artifact['title']} {artifact['format']} " | |
| f"{(artifact['size_bytes'] or 0):,} bytes") | |
| fetched = user.get(f"/v1/artifacts/{artifact['id']}") | |
| print(f" download {fetched.status_code} {len(fetched.content):,} bytes") | |
| 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)") | |
| return 0 | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |