Spaces:
Running
Running
File size: 2,183 Bytes
0a82323 12ea8e3 80ac3a1 0a82323 f2c3987 12ea8e3 f2c3987 a727a3e b56ec7a a727a3e b56ec7a f2c3987 b56ec7a f2c3987 80ac3a1 a727a3e f2c3987 a5edae5 f2c3987 b56ec7a a5edae5 f2c3987 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | ---
title: PyBot
emoji: 🐍
colorFrom: gray
colorTo: gray
sdk: docker
app_port: 7860
pinned: false
---
# PyBot: Can Python be a chatbot?
A chat demo that runs [PyBot](https://github.com/huggingface/pybot), a chatbot written in plain Python, entirely in your
browser: [Pyodide](https://pyodide.org) (CPython compiled to WebAssembly) runs the bot in a Web Worker, and this Space
only serves files. Each reply shows which of PyBot's modules and files produced it; click a file to read it on GitHub.
## Files
- `index.html`: the page (chat, the reply's trace, and a treemap of PyBot's files; Plotly from jsDelivr).
- `worker.js`: loads Pyodide 314.0.7 from jsDelivr and the bot bundle, then answers turns. Like transformers.js does
for model files, it keeps the bundle in Cache Storage, so a second visit doesn't download it again.
- `pybot_web.py`: the glue: fixed random seeds as in the benchmark's eval server (so replies match the leaderboard run),
the trace via `sys.monitoring`, and the file that holds a reply's text.
- `pybot-<sha12>.zip`: `bot/**.pyc` compiled for Pyodide's CPython 3.14 (loading skips compiling 33 MB of source),
plus `norm.json` (each file's text, letters and digits only) for finding the file that holds a reply.
It is built from the same code as `bot/` in the GitHub repo.
- `tree.json`: PyBot's files and line counts for the treemap, and the repo its file links open.
- `Dockerfile`: only serves the files above (`python -m http.server`). A static Space would redirect the large files to a
CDN without CORS headers, which blocks the worker's fetch of the bundle and the fonts.
## Rebuild from a newer bot
```sh
git clone https://github.com/huggingface/pybot && (cd pybot && zip -qr ../bot-src.zip bot -i '*.py')
npm install pyodide@314.0.7 # the same version as worker.js: bytecode only loads in the Python that compiled it
node build/build.mjs bot-src.zip build/build_bundle.py pybot-<sha12>.zip
```
Then regenerate `tree.json` (`[path, line count]` for every `bot/**.py`) and update `BUNDLE` in `worker.js` (a new
name makes browsers download the new bundle). To test locally, serve this folder (`python -m http.server`) and open it.
|