Spaces:
Running
Running
Download app.py from HuggingFaceSwarms/PyBot: direct link, hf CLI and curl.
- Browser
- Download file 4.51 kB
-
https://huggingface.co/spaces/HuggingFaceSwarms/PyBot/resolve/f2bbd92ec1870ba43f17e005bf02ff20e4a62969/app.py
- Command line
-
hf download hf://spaces/HuggingFaceSwarms/PyBot@f2bbd92ec1870ba43f17e005bf02ff20e4a62969/app.py
-
curl -L -o app.py https://huggingface.co/spaces/HuggingFaceSwarms/PyBot/resolve/f2bbd92ec1870ba43f17e005bf02ff20e4a62969/app.py
4.51 kB
| """PyBot release Space: the story page (site/) and the PyBot demo's chat endpoint. | |
| The bot is one pinned commit's bot/**.py, shipped in bot/bot-<sha12>.zip, run by bot_runner (from SwarmChat-Demo): | |
| one loader process, and a fresh forked worker per request that replays the conversation's user lines, so visitors | |
| never share bot memory. Run only trusted bot code here and add no secrets: the worker is not a security sandbox. | |
| """ | |
| import re | |
| import sys | |
| import tempfile | |
| import threading | |
| import zipfile | |
| from pathlib import Path | |
| from typing import Literal | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.staticfiles import StaticFiles | |
| from pydantic import BaseModel, Field | |
| from bot_runner import Bot, user_lines | |
| HERE = Path(__file__).parent | |
| TREE = tempfile.TemporaryDirectory(prefix="swarmchat-source-") | |
| zipfile.ZipFile(next(HERE.glob("bot/bot-*.zip"))).extractall(TREE.name) | |
| app = FastAPI(title="PyBot") | |
| _bot, _bot_lock = None, threading.Lock() | |
| _slots = threading.BoundedSemaphore(4) # conversations answered at once; cpu-basic has 2 vCPUs | |
| _norm = lambda s: re.sub(r"[^a-z0-9]", "", s.lower()) | |
| # Every bot file, letters and digits only, to find which one holds a reply's text verbatim. | |
| SOURCES = {p.relative_to(TREE.name).as_posix(): _norm(p.read_text(errors="replace")) for p in Path(TREE.name, "bot").rglob("*.py")} | |
| def bot(): | |
| global _bot | |
| with _bot_lock: | |
| if _bot is None or not _bot.alive(): | |
| _bot = Bot(Path(TREE.name), seed=0, timeout=30, workers=2, log=sys.stderr) | |
| return _bot | |
| threading.Thread(target=bot, daemon=True).start() # load at boot, so the first visitor does not wait | |
| class Message(BaseModel): | |
| role: Literal["user", "assistant"] | |
| content: str = Field(min_length=1, max_length=2000) | |
| class ChatRequest(BaseModel): | |
| messages: list[Message] = Field(min_length=1, max_length=60) | |
| def chat(req: ChatRequest): | |
| lines = user_lines([m.model_dump() for m in req.messages]) | |
| if not lines or req.messages[-1].role != "user": | |
| raise HTTPException(400, "A user message is required.") | |
| if sum(map(len, lines)) > 20000: | |
| raise HTTPException(400, "This conversation is too long. Reload the page to start again.") | |
| if not _slots.acquire(timeout=15): | |
| raise HTTPException(503, "The bot is busy. Please try again in a moment.") | |
| try: | |
| result = bot().ask(lines) | |
| finally: | |
| _slots.release() | |
| if result.get("status") != "ok" or len(result.get("replies", [])) != len(lines): | |
| raise HTTPException(502, "The bot failed on that line. Try another one.") | |
| return {"reply": result["replies"][-1]} | |
| def answer_source(reply: str, used: list[str]) -> str | None: | |
| """The one file that holds the reply verbatim (its first 40+ letters/digits), preferring files the winner called; | |
| None when the reply is composed, too short, or found in several files.""" | |
| text = _norm(reply) | |
| for n in (60, 40): | |
| key = text[:n] | |
| if len(key) < 20: # too short to point at one place | |
| continue | |
| hits = [f for f, body in SOURCES.items() if key in body] | |
| hits = hits if len(hits) == 1 else [f for f in hits if f in used] | |
| if len(hits) == 1: | |
| return hits[0] | |
| return None | |
| def trace(req: ChatRequest): | |
| """Like /api/chat, plus how the last reply was made: the module that won, the ones that also proposed a reply, | |
| the bot files the winner called, and the file holding the reply's text.""" | |
| lines = user_lines([m.model_dump() for m in req.messages]) | |
| if not lines or req.messages[-1].role != "user": | |
| raise HTTPException(400, "A user message is required.") | |
| if sum(map(len, lines)) > 20000: | |
| raise HTTPException(400, "This conversation is too long. Reload the page to start again.") | |
| if not _slots.acquire(timeout=15): | |
| raise HTTPException(503, "The bot is busy. Please try again in a moment.") | |
| try: | |
| result = bot().ask(lines, trace=True) | |
| finally: | |
| _slots.release() | |
| if result.get("status") != "ok" or len(result.get("replies", [])) != len(lines) or not result.get("trace"): | |
| raise HTTPException(502, "The bot failed on that line. Try another one.") | |
| reply, info = result["replies"][-1], result["trace"] | |
| return {"reply": reply, **info, "source": answer_source(reply, info["files"])} | |
| def health(): | |
| return {"ok": True} | |
| app.mount("/", StaticFiles(directory=HERE / "site", html=True), name="site") | |