部署上线
部署选项选择
根据你的需求选择合适的部署方式:
| 选项 | 优点 | 缺点 | 价格 | 适合场景 |
|---|---|---|---|---|
| Streamlit Cloud | 最简单,免费 | 性能有限 | 免费 | 快速分享 |
| Hugging Face Spaces | 免费,社区支持 | 每次重启慢 | 免费 | 分享和演示 |
| Railway | 支持后端,易用 | 免费额度有限 | $5/月起 | 小型应用 |
| Render | 功能完整 | 冷启动慢 | $7/月起 | 生产环境 |
| 自建服务器 | 完全控制 | 需要运维 | 服务器费用 | 企业级 |
方案一:Streamlit Cloud(推荐新手)
步骤 1:准备代码
确保项目结构清晰:
qa-bot/
├── .gitignore # 忽略敏感文件
├── requirements.txt # 依赖列表
├── app.py # 应用入口
├── rag.py # RAG 逻辑
├── config.py # 配置
└── README.md # 说明文档步骤 2:推送到 GitHub
bash
# 初始化 Git
git init
# 添加文件
git add .
# 提交
git commit -m "Initial commit"
# 推送到 GitHub
git remote add origin https://github.com/your-username/qa-bot.git
git branch -M main
git push -u origin main步骤 3:部署到 Streamlit Cloud
- 访问 share.streamlit.io
- 用 GitHub 账号登录
- 点击 "New app"
- 选择你的仓库
- 填写配置:
- Repository: your-username/qa-bot
- Branch: main
- Main file path: app.py
- 点击 "Deploy"
等待几分钟,应用就会上线了。
步骤 4:配置环境变量
在 Streamlit Cloud 的设置页面:
点击你的应用
进入 "Settings" → "Secrets"
添加环境变量:
DASHSCOPE_API_KEY: 你的 API KeyLLM_PROVIDER: dashscopeLLM_MODEL: qwen-turbo
保存并重新部署
获取地址
部署成功后,你会得到一个类似这样的地址:
https://your-app-name.streamlit.app方案二:Hugging Face Spaces
步骤 1:创建 Space
- 访问 huggingface.co/spaces
- 登录后点击 "Create new Space"
- 填写信息:
- Owner: 你的用户名
- Space name: qa-bot
- License: MIT
- SDK: Streamlit
- Hardware: CPU basic (免费)
步骤 2:上传代码
两种方式:
方式 A:Git 上传
bash
git clone https://huggingface.co/spaces/your-username/qa-bot
cd qa-bot
# 复制你的代码文件
git add .
git commit -m "Add qa bot"
git push方式 B:网页上传
直接在 Space 页面上传文件。
步骤 3:配置 Secrets
在 Space 的 "Settings" → "Repository secrets" 中添加:
DASHSCOPE_API_KEY- 其他环境变量
获取地址
https://huggingface.co/spaces/your-username/qa-bot方案三:Railway(推荐进阶)
Railway 支持更复杂的应用,可以运行后端服务。
步骤 1:准备 Dockerfile
创建 Dockerfile:
dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8501
CMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]步骤 2:创建 railway.toml
toml
[build]
builder = "DOCKERFILE"
[deploy]
startCommand = "streamlit run app.py --server.port=8501 --server.address=0.0.0.0"
healthcheckPath = "/_stcore/health"
healthcheckTimeout = 300步骤 3:部署
- 访问 railway.app
- 连接 GitHub
- 点击 "New Project" → "Deploy from GitHub repo"
- 选择你的仓库
- Railway 会自动检测配置并部署
配置环境变量
在 Railway 的 "Variables" 标签页添加:
DASHSCOPE_API_KEY- 其他环境变量
方案四:传统 VPS
如果你有自己的服务器(阿里云、腾讯云等):
步骤 1:服务器准备
bash
# 更新系统
sudo apt update && sudo apt upgrade -y
# 安装 Python
sudo apt install python3 python3-pip -y
# 安装 Nginx
sudo apt install nginx -y步骤 2:部署代码
bash
# 克隆代码
cd /var/www
git clone https://github.com/your-username/qa-bot.git
cd qa-bot
# 安装依赖
pip3 install -r requirements.txt
# 测试运行
streamlit run app.py步骤 3:用 Supervisor 守护进程
创建 /etc/supervisor/conf.d/qa-bot.conf:
ini
[program:qa-bot]
directory=/var/www/qa-bot
command=/usr/bin/python3 -m streamlit run app.py
autostart=true
autorestart=true
stderr_logfile=/var/log/qa-bot.err.log
stdout_logfile=/var/log/qa-bot.out.log启动:
bash
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start qa-bot步骤 4:配置 Nginx 反向代理
创建 /etc/nginx/sites-available/qa-bot:
nginx
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://localhost:8501;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}启用:
bash
sudo ln -s /etc/nginx/sites-available/qa-bot /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx域名配置(可选)
添加 A 记录
在你的域名提供商处:
类型: A
主机记录: @
记录值: 你的服务器 IP
TTL: 600配置 HTTPS
bash
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your-domain.com安全配置
1. 保护 API Keys
不要把 .env 文件提交到 Git!
确保 .gitignore 包含:
.env
.env.local
__pycache__
*.pyc
venv/
chroma_db/2. 限制访问(可选)
在 app.py 中添加密码保护:
python
import streamlit as st
# 简单密码验证
password = st.text_input("请输入密码", type="password")
if password != "your-secret-password":
st.stop()3. 使用量限制
python
if "question_count" not in st.session_state:
st.session_state.question_count = 0
MAX_QUESTIONS = 50
if st.session_state.question_count >= MAX_QUESTIONS:
st.warning("今日提问次数已用完")
st.stop()
# 每次提问后计数
st.session_state.question_count += 1监控和日志
Streamlit Cloud
在应用后台可以看到:
- 访问量
- CPU 使用率
- 错误日志
Railway
在 "Metrics" 标签页查看:
- CPU/内存使用
- 网络流量
- 部署日志
自建服务器
bash
# 查看日志
sudo tail -f /var/log/qa-bot.out.log
# 监控资源
htop成本估算
Streamlit Cloud / Hugging Face
- 免费
- 限制:每月 750 小时运行时间
Railway
- 免费额度:$5/月
- 超出后按实际使用计费
- 小应用一般 $5-10/月
自建服务器
- 阿里云/腾讯云入门级:约 ¥50/月
- 需要自己维护
下一步
应用上线了,收集反馈并持续改进。