Skip to content

部署上线

部署选项选择

根据你的需求选择合适的部署方式:

选项优点缺点价格适合场景
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

  1. 访问 share.streamlit.io
  2. 用 GitHub 账号登录
  3. 点击 "New app"
  4. 选择你的仓库
  5. 填写配置:
    • Repository: your-username/qa-bot
    • Branch: main
    • Main file path: app.py
  6. 点击 "Deploy"

等待几分钟,应用就会上线了。

步骤 4:配置环境变量

在 Streamlit Cloud 的设置页面:

  1. 点击你的应用

  2. 进入 "Settings" → "Secrets"

  3. 添加环境变量:

    • DASHSCOPE_API_KEY: 你的 API Key
    • LLM_PROVIDER: dashscope
    • LLM_MODEL: qwen-turbo
  4. 保存并重新部署

获取地址

部署成功后,你会得到一个类似这样的地址:

https://your-app-name.streamlit.app

方案二:Hugging Face Spaces

步骤 1:创建 Space

  1. 访问 huggingface.co/spaces
  2. 登录后点击 "Create new Space"
  3. 填写信息:
    • 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:部署

  1. 访问 railway.app
  2. 连接 GitHub
  3. 点击 "New Project" → "Deploy from GitHub repo"
  4. 选择你的仓库
  5. 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/月
  • 需要自己维护

下一步

应用上线了,收集反馈并持续改进。

继续:迭代改进 →


← 返回测试优化 | 返回项目一

最近更新

基于 Apache 2.0 许可发布