File size: 2,862 Bytes
59bd45e | 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 | # 部署指南
## 部署到 Hugging Face Spaces
### 前置准备
1. **构建前端**
```bash
cd frontend
npm install
npm run build
```
2. **验证构建产物**
- 确保 `frontend/dist/` 目录存在
- 包含 `index.html` 和 `assets/` 文件夹
### 自动部署(推荐)
**Windows:**
```bash
build_and_deploy.bat
```
**Linux/Mac:**
```bash
chmod +x build_and_deploy.sh
./build_and_deploy.sh
```
### 手动部署
1. **构建前端**
```bash
cd frontend
npm run build
cd ..
```
2. **提交更改**
```bash
git add .
git commit -m "Deploy: Update frontend build"
```
3. **推送到 Hugging Face**
```bash
git push hf main
```
### 配置 Hugging Face Secrets
在 Space 的 Settings → Repository secrets 中添加:
**必需:**
- `ZHIPU_API_KEY` - 智谱 AI API 密钥
- 获取:https://open.bigmodel.cn/
**可选:**
- `MINIMAX_API_KEY` - MiniMax API 密钥
- `MINIMAX_GROUP_ID` - MiniMax Group ID
- 获取:https://platform.minimaxi.com/
### 访问应用
部署成功后,访问:
- **前端应用**: `https://your-space.hf.space/app`
- **Gradio 界面**: `https://your-space.hf.space/gradio`
- **API 文档**: `https://your-space.hf.space/docs`
### 文件结构
```
.
├── app.py # Hugging Face 入口文件
├── app/ # FastAPI 后端
│ ├── main.py # 主应用
│ └── ...
├── frontend/
│ ├── dist/ # 构建产物(需要提交)
│ │ ├── index.html
│ │ └── assets/
│ └── ...
├── requirements_hf.txt # Python 依赖
└── README_HF.md # Hugging Face 说明
```
### 故障排查
**问题:前端 404**
- 检查 `frontend/dist/` 是否存在
- 确认已运行 `npm run build`
- 查看 Space 日志确认文件已上传
**问题:API 调用失败**
- 检查 Secrets 是否正确配置
- 查看 Space 日志中的错误信息
- 确认 API 密钥有效
**问题:静态资源加载失败**
- 检查 `frontend/dist/assets/` 是否存在
- 确认 CSS 和 JS 文件已生成
- 查看浏览器控制台的网络请求
### 本地测试
在部署前本地测试:
```bash
# 构建前端
cd frontend && npm run build && cd ..
# 运行应用
python app.py
```
访问 `http://localhost:7860/app` 测试前端应用。
### 更新部署
每次修改前端代码后:
1. 重新构建:`cd frontend && npm run build && cd ..`
2. 提交更改:`git add . && git commit -m "Update"`
3. 推送:`git push hf main`
### 注意事项
- ✅ `frontend/dist/` 必须提交到 Git(不要在 .gitignore 中忽略)
- ✅ 每次修改前端代码都需要重新构建
- ✅ Hugging Face Spaces 会自动重启应用
- ⚠️ 首次部署可能需要 5-10 分钟
- ⚠️ 免费 Space 可能会在不活跃时休眠
|