| """trace_publish.py - upload /data/traces to the Hub as a dataset, using the |
| HF_TOKEN secret. No command line; the custom gradio.Server app calls it directly. |
| """ |
| from __future__ import annotations |
|
|
| import os |
| import tempfile |
|
|
| import paths |
|
|
|
|
| def publish_traces(repo_id: str) -> str: |
| repo_id = (repo_id or "").strip() |
| if "/" not in repo_id: |
| return "⚠️ Enter a repo id like `username/dataset-name`." |
| token = (os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN") |
| or "").strip() |
| if not token: |
| return ("⚠️ No `HF_TOKEN` secret found. Add it in Space → Settings → " |
| "Variables and secrets (a **write** token), then try again.") |
| try: |
| traces = [f for f in os.listdir(paths.TRACES_DIR) if f.endswith(".json")] |
| except OSError: |
| traces = [] |
| if not traces: |
| return "⚠️ No traces yet — run a few Auto Research reports first." |
| try: |
| from huggingface_hub import HfApi |
| api = HfApi(token=token) |
| api.create_repo(repo_id, repo_type="dataset", exist_ok=True) |
| card = ( |
| "---\nlicense: mit\ntags: [agent-trace, finance, chan-theory, llama-cpp]\n---\n\n" |
| "# Chan Compass — agent traces\n\n" |
| "JSON traces from the Chan Compass multi-agent research desk. Each file " |
| "is one ticker's run: the plan, every evidence-tool call and its result, " |
| "and each local sub-agent's request and response. Shared for the " |
| "Build Small hackathon (*Sharing is Caring*).\n\n" |
| "*Educational data — not investment advice.*\n") |
| rp = os.path.join(tempfile.gettempdir(), "README.md") |
| with open(rp, "w", encoding="utf-8") as f: |
| f.write(card) |
| api.upload_file(path_or_fileobj=rp, path_in_repo="README.md", |
| repo_id=repo_id, repo_type="dataset") |
| api.upload_folder(folder_path=paths.TRACES_DIR, path_in_repo="traces", |
| repo_id=repo_id, repo_type="dataset") |
| url = f"https://huggingface.co/datasets/{repo_id}" |
| return (f"✅ Published {len(traces)} trace(s) to {repo_id} ({url}). " |
| f"Put that link in your submission for the Sharing is Caring badge.") |
| except Exception as e: |
| return f"❌ Upload failed: {e}" |
|
|