Decoder2704/python-chatbot-jay
0
1# Hugging Face Space (Docker) — single service2 3This repo can run as **one Docker Space**: **FastAPI** serves JSON APIs and the **built React app** from the same process. Data and artifacts are read from `backend/data` and `backend/artifacts` inside the image (commit them to the Space repo, or populate via your own download step — not covered here).4 5Retrieval code, dataset schemas, and notebook-aligned paths are unchanged; only wiring (static files, Docker, frontend API base URL) is added.6 7---8 9## How the Space runs10 111. **Build stage:** Node builds `frontend/` → `dist/`.122. **Runtime image:** Python installs `backend/requirements.txt`, copies `backend/` (including `data/` and `artifacts/` if present in the build context), and copies the Vite output to **`backend/static/dist`**.133. **Start:** `uvicorn app.main:app --host 0.0.0.0 --port $PORT` (HF sets `PORT`, default **7860**).144. **Requests:** API routes are registered first; **`StaticFiles`** is mounted at `/` last so the SPA serves `index.html` and assets, while JSON APIs keep working alongside the UI.15 16The frontend production build uses **same-origin** API calls when `VITE_API_URL` is unset — no extra env vars required on the Space for a single deployment.17 18---19 20## Updated / relevant layout21 22```text23pybot_NLP/24├── Dockerfile # multi-stage: npm build → Python + static/dist25├── .dockerignore26├── backend/27│ ├── app/28│ │ ├── main.py # + optional StaticFiles mount for SPA29│ │ └── paths.py # + FRONTEND_DIST_DIR (static/dist)30│ ├── data/ # processed CSVs (in repo for HF runtime)31│ ├── artifacts/ # pickles (in repo for HF runtime)32│ └── static/33│ └── dist/ # populated by Docker build (not required locally)34├── frontend/35│ ├── src/services/api.js # prod: same-origin; dev: 127.0.0.1:800036│ └── ...37└── docs/38 └── HF_SPACE.md # this file39```40 41---42 43## Files added or modified (minimal)44 45| Action | Path |46|--------|------|47| **Add** | `Dockerfile` — Node build + Python runtime, copies `dist` → `backend/static/dist` |48| **Add** | `.dockerignore` — trims context (node_modules, venv, notebooks, etc.) |49| **Add** | `docs/HF_SPACE.md` — this guide |50| **Modify** | `backend/app/paths.py` — `FRONTEND_DIST_DIR` |51| **Modify** | `backend/app/main.py` — mount SPA if `FRONTEND_DIST_DIR` exists; log if missing |52| **Modify** | `frontend/src/services/api.js` — production default: same-origin; dev: `:8000` |53 54No changes to `retriever.py`, dataset builders, or column definitions.55 56---57 58## Space setup (dashboard)59 601. New **Space** → **Docker** → connect this repository.612. **SDK** is Docker; root `Dockerfile` is used automatically.623. Ensure **`backend/data`** and **`backend/artifacts`** are in the branch the Space builds (or add a custom build step to fetch them — your choice).634. **Hardware:** CPU is enough if inference matches your local stack; increase if load is high.64 65### Optional `README` card (YAML)66 67If you use a dedicated Space repo, put this at the **top** of `README.md`:68 69```yaml70---71title: PyBot NLP Chat72emoji: 🤖73colorFrom: blue74colorTo: indigo75sdk: docker76pinned: false77---78```79 80---81 82## Local checks83 84- **API only (no UI files):** `cd backend && uvicorn app.main:app --reload` — works; log may warn that `static/dist` is missing.85- **Full stack locally:** Build `frontend/dist`, copy to `backend/static/dist`, run uvicorn — open the app root for the SPA; use the chat UI to verify responses.86- **Docker:** From repo root: `docker build -t pybot-hf .` then run with `-p 7860:7860` and open `http://localhost:7860`.87 88---89 90## Local Docker (Mac/Linux)91 92Exact **build / run / browser** steps: **`docs/DOCKER_LOCAL.md`**. Optional one-command run: `docker compose up --build` from the repo root (`docker-compose.yml`).93 94---95 96## Common issues97 98| Issue | Notes |99|-------|-------|100| **404 on `/`** | `backend/static/dist` empty — run Docker build or copy Vite `dist` there. |101| **Chat fails in UI** | Same-origin requires API on same host/port as the page; do not set `VITE_API_URL` to localhost in the production build. |102| **Server errors when chatting** | Missing or corrupt `data/` or `artifacts/` in the image — check Space build logs and repo contents. |103| **Huge git push** | Large CSVs/pickles may exceed HF limits — consider Git LFS or external artifact hosting (custom download in Dockerfile). |104 