

大家好,这里是程序员晚枫,正在all in AI编程实战。
第26讲:AI应用上线部署——让世界看到你的作品
部署的几种方式
| 方式 | 难度 | 费用 | 适合场景 |
|---|
| 本地运行 | 零 | 零 | 开发测试 |
| Streamlit Cloud | 低 | 免费 | 数据分析工具 |
| 阿里云/腾讯云 | 中 | 付费 | 企业应用 |
| 服务器+VPS | 高 | 中等 | 全栈应用 |
1、部署Streamlit应用
最简单的方式,不需要买服务器!
第1步:准备代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import streamlit as st from openai import OpenAI
st.set_page_config(page_title="AI助手", page_icon="🤖")
client = OpenAI(api_key="你的Key", base_url="https://api.deepseek.com")
st.title("🤖 我的AI助手")
if prompt := st.text_input("问我任何问题:"): response = client.chat.completions.create( model="deepseek-chat", messages=[{"role": "user", "content": prompt}] ) st.write(response.choices[0].message.content)
|
第2步:创建requirements.txt
第3步:推送到GitHub
1 2 3 4 5
| git init git add . git commit -m "first commit" git remote add origin https://github.com/你的用户名/ai-app.git git push -u origin main
|
第4步:Streamlit Cloud部署
- 访问 https://streamlit.io/cloud
- 用GitHub登录
- 点击"New App"
- 选择你的GitHub仓库
- 点击"Deploy!"
你的应用就上线了!🎉
2、部署到服务器
第1步:购买云服务器
推荐阿里云/腾讯云,新用户首月几块钱:
第2步:安装环境
1 2 3 4 5 6 7 8 9 10 11 12
| ssh root@你的服务器IP
apt update apt install python3 python3-pip nginx
pip3 install -r requirements.txt
nginx
|
第3步:用PM2管理进程
1 2 3 4
| pip3 install pm2 pm2 start app.py --name "ai-app" pm2 save pm2 startup
|
第4步:配置Nginx
1 2 3 4 5 6 7 8 9
| server { listen 80; server_name 你的域名或IP;
location / { proxy_pass http://localhost:8501; proxy_set_header Host $host; } }
|
1 2
| nginx -t systemctl reload nginx
|
3、配置域名(可选)
- 买一个域名(阿里云/腾讯云,几十块/年)
- 解析到服务器IP
- 申请SSL证书(Let's Encrypt免费)
- 配置HTTPS
4、实战:一键部署脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #!/bin/bash
echo "正在部署AI应用..."
git pull origin main
pip3 install -r requirements.txt
pm2 restart ai-app
pm2 status
echo "部署完成!访问 http://你的服务器IP"
|
1 2
| chmod +x deploy.sh ./deploy.sh
|
下讲预告
学会了部署,下一讲我们学 AI应用变现——让你的AI技能变成真金白银。
敬请期待!
程序员晚枫专注AI编程培训,小白看完他和图灵社区合作的教程《30讲 · AI编程训练营》就能上手做AI项目。
前3讲可以试听,试听链接:https://www.bilibili.com/cheese/play/ss982042944