github star gitee star atomgit star PyPI Downloads AI编程 AI交流群

大家好,这里是程序员晚枫,正在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
# app.py
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

1
2
streamlit
openai

第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部署

  1. 访问 https://streamlit.io/cloud
  2. 用GitHub登录
  3. 点击"New App"
  4. 选择你的GitHub仓库
  5. 点击"Deploy!"

你的应用就上线了!🎉

2、部署到服务器

第1步:购买云服务器

推荐阿里云/腾讯云,新用户首月几块钱:

  • 配置:1核2G
  • 系统:Ubuntu 22.04

第2步:安装环境

1
2
3
4
5
6
7
8
9
10
11
12
# 连接服务器
ssh root@你的服务器IP

# 安装Python和Nginx
apt update
apt install python3 python3-pip nginx

# 安装项目依赖
pip3 install -r requirements.txt

# 用Nginx做反向代理
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、配置域名(可选)

  1. 买一个域名(阿里云/腾讯云,几十块/年)
  2. 解析到服务器IP
  3. 申请SSL证书(Let's Encrypt免费)
  4. 配置HTTPS

4、实战:一键部署脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
# deploy.sh

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