lixiaowww Cursor commited on
Commit
429404e
·
1 Parent(s): 22761ba

Deploy Streamlit on HF Spaces via Docker (Streamlit SDK deprecated).

Browse files

Add Dockerfile, .dockerignore, and updated setup guide for app_port 8501.

Co-authored-by: Cursor <cursoragent@cursor.com>

.dockerignore ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ venv/
2
+ .venv/
3
+ .git/
4
+ __pycache__/
5
+ *.py[cod]
6
+ .pytest_cache/
7
+ .mypy_cache/
8
+ .env
9
+ .env.*
10
+ !.env.example
11
+ data/forecaster.db
12
+ data/bls_cache.json
13
+ data/kb_calibration.json
14
+ data/kb_calibration_log.jsonl
15
+ pending/
16
+ =0.9
17
+ nodeids
18
+ CACHEDIR.TAG
Dockerfile ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Hugging Face Spaces — Streamlit via Docker (native Streamlit SDK deprecated 2025-04)
2
+ # https://huggingface.co/docs/hub/spaces-changelog
3
+ FROM python:3.12-slim
4
+
5
+ RUN apt-get update && apt-get install -y --no-install-recommends curl \
6
+ && rm -rf /var/lib/apt/lists/* \
7
+ && useradd -m -u 1000 user
8
+
9
+ USER user
10
+ WORKDIR /home/user/app
11
+
12
+ ENV STREAMLIT_HOME=/tmp/.streamlit
13
+ ENV STREAMLIT_BROWSER_GATHER_USAGE_STATS=false
14
+ ENV PYTHONUNBUFFERED=1
15
+ ENV PATH="/home/user/.local/bin:${PATH}"
16
+
17
+ COPY --chown=user:user requirements.txt requirements-dashboard.txt ./
18
+ RUN pip install --user --no-cache-dir -r requirements.txt
19
+
20
+ COPY --chown=user:user . .
21
+
22
+ EXPOSE 8501
23
+
24
+ HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health || exit 1
25
+
26
+ ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
README.md CHANGED
@@ -153,14 +153,14 @@ See **[docs/DEPLOY.md](docs/DEPLOY.md)** for setup (Pages, `GROQ_API_KEY` secret
153
 
154
  ### Interactive dashboard (Hugging Face Spaces)
155
 
156
- Deploy the Streamlit UI (Job Radar, accuracy tabs) on HF free tier:
157
 
158
  ```bash
159
- streamlit run dashboard.py # local
160
- # or: streamlit run app.py # same UI (HF Spaces entry)
161
  ```
162
 
163
- See **[docs/HUGGINGFACE.md](docs/HUGGINGFACE.md)** — connect repo `app.py` to a new Streamlit Space.
164
 
165
  ---
166
 
 
153
 
154
  ### Interactive dashboard (Hugging Face Spaces)
155
 
156
+ > HF 已弃用原生 Streamlit SDK(2025-04)→ 创建 Space 时选 **Docker**,仓库内已含 `Dockerfile`。
157
 
158
  ```bash
159
+ streamlit run app.py # local
160
+ docker build -t jobforecaster . && docker run -p 8501:8501 jobforecaster # optional
161
  ```
162
 
163
+ See **[docs/HUGGINGFACE.md](docs/HUGGINGFACE.md)**.
164
 
165
  ---
166
 
deploy/huggingface/SPACE_README.md ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: JobForecaster Agent
3
+ emoji: 🔮
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: docker
7
+ app_port: 8501
8
+ license: other
9
+ pinned: false
10
+ short_description: Streamlit Job Radar & AI economy forecast dashboard
11
+ tags:
12
+ - streamlit
13
+ - docker
14
+ - economics
15
+ - job-market
16
+ ---
17
+
18
+ # JobForecaster Agent — Interactive Dashboard
19
+
20
+ Copy this file to the **root `README.md`** of your Hugging Face Space repo, or ensure your linked GitHub Space uses **SDK: Docker** with `app_port: 8501`.
21
+
22
+ Static reports: [GitHub Pages](https://lixiaowww.github.io/JobForecaster-Agent/)
docs/HUGGINGFACE.md CHANGED
@@ -1,136 +1,136 @@
1
  # Hugging Face Spaces 部署指南
2
 
3
- 把 **Streamlit 交互 Dashboard**(Job Radar、准确度、Benchmarks)部署到 [Hugging Face Spaces](https://huggingface.co/spaces) 免费档。
4
 
5
- > **GitHub Pages**([lixiaowww.github.io/JobForecaster-Agent](https://lixiaowww.github.io/JobForecaster-Agent/))只托管**静态预测报告**
6
- > **本指南**部署的是完整 **Streamlit 前端**,两者互补
7
 
8
- ---
9
-
10
- ## 方式 A:从 GitHub 仓库连接(推荐)
 
11
 
12
- 仓库已包含 `app.py`、`.streamlit/config.toml` 和合并后的 `requirements.txt`。
13
 
14
- ### 1. 创建 Space
15
 
16
  1. 打开 [huggingface.co/new-space](https://huggingface.co/new-space)
17
  2. 填写:
18
- | 字段 | 值 |
19
- |------|-----|
20
- | Owner | 你的 HF 账号(如 `lixiaowww`) |
21
- | Space name | `JobForecaster-Agent`(或任意名) |
22
- | License | 与仓库一致 |
23
- | Space SDK | **Streamlit** |
24
- | Space hardware | **CPU basic**(免费) |
25
- 3. **不要**选 Docker;选 **Link to a Git repository**(若界面有)
26
- 4. 创建
27
-
28
- ### 2. 连接 GitHub 仓库
29
-
30
- 1. Space 页面 → **Settings** → **Repository**
31
- 2. **Repository URL**:`https://github.com/lixiaowww/JobForecaster-Agent`
32
- 3. **Branch**:`main`
33
- 4. **App file**:`app.py`
34
- 5. Save → 等待自动构建(约 3–8 分钟)
35
-
36
- 若创建时未出现 Git 连接选项:
37
-
38
- 1. 先创建空白 Streamlit Space
39
- 2. Settings → Repository → 填上述 URL
40
- 3. App file 设为 `app.py`
41
 
42
- ### 3. 添加 Secrets(可
 
 
 
 
 
43
 
44
- Space **Settings** **Repository secrets**(或 Variables and secrets)
45
 
46
- | Name | Value | 说明 |
47
- |------|-------|------|
48
- | `GROQ_API_KEY` | 你的 Groq key | [console.groq.com](https://console.groq.com) 免费档 |
49
 
50
- - **有 key**搜索未知岗位时可 LLM 扩 KB
51
- - **无 key**:Radar / BLS / 情景滑块仍可用,仅 AI 生成岗位不可用
52
 
53
- Secrets Streamlit 中通过 `os.environ` 读取与本地 `.env` 相同
54
 
55
- ### 4. 访问
 
 
 
56
 
57
- 构建成功后
58
 
59
- `https://huggingface.co/spaces/<你的用户名>/JobForecaster-Agent`
 
 
 
 
60
 
61
- (公开 Space 可直接打开;私有 Space 需登录。
62
 
63
  ---
64
 
65
- ## 方式 B:独立 Space 仓库
66
 
67
- 若不想把整个 GitHub 主仓库连到 HF:
 
 
 
 
 
68
 
69
- 1. HF 新建 Streamlit Space(空白
70
- 2. `deploy/huggingface/README.md` 的内容复制为 Space 根目录 `README.md`(含顶部 YAML)
71
- 3. 用 `git clone` Space 仓库,把本项目代码 push 上去(或 GitHub Action 同步)
72
 
73
  ---
74
 
75
- ## 文件说明
76
 
77
- | 文件 | 作用 |
 
 
78
  |------|------|
79
- | `app.py` | HF 入口`import dashboard` |
80
- | `dashboard.py` | Streamlit 主界面 |
81
- | `requirements.txt` | `-r requirements-dashboard.txt` |
82
- | `.streamlit/config.toml` | 云端 headless + HF 兼容设置 |
83
- | `data/jobs_kb.json` | 岗位知识库(已入库) |
84
- | `data/bls_market_seed.json` | 离线 BLS 实证层 |
85
 
86
  ---
87
 
88
- ## 与 GitHub Pages 分工
89
 
90
- | 服务 | 内容 | URL ��例 |
91
- |------|------|----------|
92
- | **GitHub Pages** | 每日静态预测报告 | `lixiaowww.github.io/JobForecaster-Agent/` |
93
- | **HF Spaces** | 交互 Dashboard | `huggingface.co/spaces/lixiaowww/JobForecaster-Agent` |
 
94
 
95
  ---
96
 
97
  ## 常见问题
98
 
99
- ### 失败 `ModuleNotFoundError`
 
 
100
 
101
- 确认 Space 使用仓库根目录的 `requirements.txt`,且已 push 含 `-r requirements-dashboard.txt` 的版本。
102
 
103
- ### 页面空白或 502
104
 
105
- - 查看 Space **Logs** 标签
106
- - 首次冷启动可能 1–2 分钟
107
- - 免费档闲置后会休眠,再次打开需等待唤醒
108
 
109
- ### Accuracy 页数据为空
110
 
111
- 正常:Space 上默 `data/forecaster.db`(未提交 git)。
112
- 预测数据在 **GitHub Pages 报告** 或本地 `run.py once` 产生;HF 主要展示 **Job Radar**。
113
 
114
- ### 限制免费档
115
 
116
- - CPU / 内存有限,GMM `n_bootstrap` 在侧边栏改小可加速
117
- - 不适合高并发;个人 demo / donation 项目足够
118
 
119
  ---
120
 
121
- ## 推送更新
122
 
123
- 主仓库更新后,若 Space 已连 GitHub,**push 到 `main` 会自动重建**:
 
 
 
 
 
 
 
 
 
 
 
124
 
125
  ```bash
126
- git add app.py docs/HUGGINGFACE.md .streamlit/config.toml requirements.txt
127
- git commit -m "Add Hugging Face Spaces Streamlit deploy"
128
- git push origin main
129
  ```
130
 
131
  ---
132
 
133
- ## 相关文档
134
 
135
- - [docs/DEPLOY.md](DEPLOY.md) — GitHub Pages 零成本部署
136
- - [README.md](../README.md) — 本地 `streamlit run dashboard.py`
 
1
  # Hugging Face Spaces 部署指南
2
 
3
+ 把 **Streamlit 交互 Dashboard** 部署到 [Hugging Face Spaces](https://huggingface.co/spaces) 免费档。
4
 
5
+ > **重要(2025 年起)**:Hugging Face **已移除原生 Streamlit SDK**([changelog 2025-04-30](https://huggingface.co/docs/hub/spaces-changelog))。
6
+ > 创建 Space 时请选择 **Docker**,不要找 Streamlit 选项
7
 
8
+ | 服务 | 内容 |
9
+ |------|------|
10
+ | [GitHub Pages](https://lixiaowww.github.io/JobForecaster-Agent/) | 静态每日预测报告 |
11
+ | **HF Spaces** | 交互 Streamlit(Job Radar 等) |
12
 
13
+ ---
14
 
15
+ ## 第一步:创建 Space(选 Docker)
16
 
17
  1. 打开 [huggingface.co/new-space](https://huggingface.co/new-space)
18
  2. 填写:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ | 字段 |择 |
21
+ |------|------|
22
+ | Space name | `JobForecaster-Agent` |
23
+ | **SDK** | **Docker** ← 不是 Streamlit |
24
+ | Hardware | **CPU basic**(免费) |
25
+ | License | 与仓库一致 |
26
 
27
+ 3. **Create Space**
28
 
29
+ ---
 
 
30
 
31
+ ## 第二步连接 GitHub 仓库
 
32
 
33
+ ### 方式 A:Space 设置里连 GitHub推荐
34
 
35
+ 1. Space → **Settings** → **Repository**
36
+ 2. **Repository URL**:`https://github.com/lixiaowww/JobForecaster-Agent`
37
+ 3. **Branch**:`main`
38
+ 4. Save → 自动用仓库根目录的 **`Dockerfile`** 构建
39
 
40
+ ### 方式 B推送到 HF Git 仓库
41
 
42
+ ```bash
43
+ # 在 HF Space 页面复制 git clone 地址后:
44
+ git remote add hf https://huggingface.co/spaces/<你的用户名>/JobForecaster-Agent
45
+ git push hf main
46
+ ```
47
 
48
+ 首次 push 若冲突,按 HF 提示 `git push --force`(仅首次
49
 
50
  ---
51
 
52
+ ## 仓库里已准备好的文件
53
 
54
+ | 文件 | 作用 |
55
+ |------|------|
56
+ | `Dockerfile` | Streamlit + 依赖,`EXPOSE 8501` |
57
+ | `app.py` | 入口(`import dashboard`) |
58
+ | `.dockerignore` | 排除 `venv/`、`.env` 等 |
59
+ | `requirements.txt` | 含 Streamlit 依赖 |
60
 
61
+ `README.md` 顶部 **无需** YAML(选 Docker SDK 创建即可
62
+ HF 要求 metadata,把 `deploy/huggingface/SPACE_README.md` 的 YAML 块合并进 Space README
 
63
 
64
  ---
65
 
66
+ ## 第三步:Secrets(可选)
67
 
68
+ Space **Settings** **Repository secrets**
69
+
70
+ | Name | 说明 |
71
  |------|------|
72
+ | `GROQ_API_KEY` | [Groq 免费 key](https://console.groq.com)用于 LLM 扩岗位 KB |
73
+
74
+ key Radar / BLS / 情景滑块仍可用。
 
 
 
75
 
76
  ---
77
 
78
+ ## 构建成功标志
79
 
80
+ - Logs 出现 `You can now view your Streamlit app`
81
+ - 健康检查:`/_stcore/health` 通过
82
+ - 访问:`https://huggingface.co/spaces/<用户名>/JobForecaster-Agent`
83
+
84
+ 首次或休眠唤醒后可能等 1–3 分钟。
85
 
86
  ---
87
 
88
  ## 常见问题
89
 
90
+ ### 页没有 Streamlit?
91
+
92
+ 正常。请选 **Docker**,本项目已提供 `Dockerfile`([官方说明](https://huggingface.co/docs/hub/spaces-sdks-docker))。
93
 
94
+ ### 503 / Preparing Space 一直转
95
 
96
+ 检查三处端口一致为 **8501**:
97
 
98
+ - `Dockerfile` `EXPOSE 8501` 与 `ENTRYPOINT ... --server.port=8501`
99
+ - Space README YAML(若有)→ `app_port: 8501`
100
+ - 不要用 `app_port: 7860`(那是 Gradio 默认)
101
 
102
+ ### 构建失败 ModuleNotFoundError
103
 
104
+ 认 `main` 分支已 push 最新 `requirements.txt`( `-r requirements-dashboard.txt`)。
 
105
 
106
+ ### Accuracy 页为空
107
 
108
+ Space `forecaster.db`(未入库)。预测历史见 GitHub Pages;HF 主要用于 **Job Radar**。
 
109
 
110
  ---
111
 
112
+ ## 本地对比
113
 
114
+ ```bash
115
+ # 本地 Streamlit(与 HF 相同 UI)
116
+ streamlit run app.py
117
+
118
+ # 本地模拟 Docker 构建(可选)
119
+ docker build -t jobforecaster .
120
+ docker run -p 8501:8501 jobforecaster
121
+ ```
122
+
123
+ ---
124
+
125
+ ## 推送更新
126
 
127
  ```bash
128
+ git push origin main # Space 已连 GitHub,会自动重建
 
 
129
  ```
130
 
131
  ---
132
 
133
+ ## 相关
134
 
135
+ - [docs/DEPLOY.md](DEPLOY.md) — GitHub Pages
136
+ - [Docker Spaces 文档](https://huggingface.co/docs/hub/spaces-sdks-docker)