Spaces:
Paused
Paused
File size: 5,501 Bytes
4eefabb | 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | # Deploying MicroClimate-X to Hugging Face Spaces
> ไธๆฌก้จ็ฝฒ๏ผๆฐธไน
ๅ
ฌ็ฝ URL๏ผๅฏผๅธ้ๆถๅฏ็๏ผไธ็จๆ็ฌ่ฎฐๆฌใ
> One-time deploy โ permanent public URL your supervisor can open any time, no laptop required.
---
## Why Hugging Face Spaces?
* **Free** persistent hosting for ML demos (CPU tier is enough for this project).
* **Docker SDK** โ reuses the existing `Dockerfile` 1:1, no platform-specific build hacks.
* **Server-side Git LFS** โ the 217 MB `rf_model.pkl` uploads through `huggingface-cli`
without needing local `git-lfs` installed.
* The HF Space URL (`https://huggingface.co/spaces/<user>/microclimate-x`) is *the*
canonical demo URL for ML thesis projects in 2026.
---
## Architecture on HF Spaces
```
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ https://huggingface.co/spaces/<user>/microclimate-x โ
โ โ
โ Docker image (this repo's Dockerfile) โ
โ โโ FastAPI @ :8000 (declared via README.md `app_port`) โ
โ โโ /app/frontend/ โโ mounted at /app/ โ
โ โโ /app/models/ โโ rf_model.pkl baked in โ
โ โโ /tmp/cache.sqlite3 (ephemeral โ fine for demo) โ
โ โ
โ โ outbound HTTP โ
โ โโ api.open-meteo.com (weather, ERA5) โ
โ โโ api.opentopodata.org/srtm30m (DEM) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
```
The frontend talks to its own origin via relative `/api/โฆ` URLs, so no CORS
fiddling and no front-end edits are needed.
---
## 3-step deploy
### Step 1 โ Create the Space (web UI, one minute)
1. Go to <https://huggingface.co/new-space>
2. Fill in:
* **Owner**: your HF account
* **Space name**: `microclimate-x`
* **License**: MIT
* **Space SDK**: **Docker** โ "Blank" template
* **Hardware**: CPU basic (free)
* **Visibility**: Public
3. Click **Create Space**. You'll land on an empty repo page.
### Step 2 โ Authenticate the CLI (one minute)
```bash
# Install once
pip install -U "huggingface_hub[cli]"
# Get a token at https://huggingface.co/settings/tokens
# - Type: Write
# - Scope: read + write to the new Space
huggingface-cli login
# (paste the token when prompted)
```
### Step 3 โ Push (one command)
From the repo root:
```bash
./scripts/deploy_hf.sh <hf-username>/microclimate-x
# e.g.
./scripts/deploy_hf.sh KyoukoLi/microclimate-x
```
The script uploads:
* `backend/`, `frontend/`, `scripts/`, `models/`, `docs/`
* `Dockerfile`, `requirements.txt`, `README.md`, `LICENSE`, `pyproject.toml`
and skips local-only junk (`data/`, `figures/`, `tests/`, `.venv/`, SQLite caches, โฆ).
HF then runs the same `Dockerfile` you use locally. Build takes โ 3โ5 min the first time
(the 217 MB model upload dominates) and < 1 min for subsequent deploys.
When the Space's status badge turns green ("Running"), the URL
`https://huggingface.co/spaces/<user>/microclimate-x` is live. Send that to your supervisor.
---
## Updating the demo after code changes
```bash
git commit -am "fix: โฆ"
./scripts/deploy_hf.sh KyoukoLi/microclimate-x
```
The script diffs against the remote, so only changed files are re-uploaded.
A code-only change (no model retrain) is a < 10 s push.
---
## Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Build log: `unable to open database file` | `MICROCLIMATEX_DB` points to a read-only path on HF | Dockerfile already sets it to `/tmp/cache.sqlite3`. Make sure your Space doesn't override it under **Settings โ Variables and secrets**. |
| Build log: `port 7860 expected, got 8000` | HF didn't parse the `app_port` frontmatter | Confirm `README.md` starts with the YAML block including `app_port: 8000`. |
| App loads but every request โ 502 | Outbound calls to Open-Meteo / Open Topo Data hit HF's egress timeout | Increase `httpx.AsyncClient(timeout=โฆ)` in `backend/main.py` (currently 15 s). |
| ML predictor banner says "heuristic" | `models/rf_model.pkl` wasn't uploaded | Re-run `./scripts/deploy_hf.sh`; the script prompts before continuing without the model. |
| LFS quota exceeded | Free HF accounts get 5 GB LFS โ we use ~220 MB | Delete old commits' LFS objects under **Files & versions โ Settings**. |
---
## Optional: GitHub Actions auto-sync
If you'd rather have `git push origin main` auto-deploy to HF, add this workflow:
```yaml
# .github/workflows/sync-to-hf.yml
name: Sync to Hugging Face Space
on:
push:
branches: [main]
jobs:
sync:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 0 }
- uses: actions/setup-python@v5
with: { python-version: "3.12" }
- run: pip install -U "huggingface_hub[cli]"
- run: |
echo "${{ secrets.HF_TOKEN }}" | huggingface-cli login --token "$(cat -)"
./scripts/deploy_hf.sh KyoukoLi/microclimate-x
```
Add `HF_TOKEN` to the GitHub repo's secrets. Now every push to `main`
re-deploys the Space.
|