AI Demo 快速部署

AI Demo 快速部署

假设你已经准备好所有资源:

  • 域名:demo.example.ai
  • 服务器 IP:12.23.34.45
  • 服务器 OS:ubuntu server 22.04

这里以一个简单的 Gradio 的 Chatbot 为例,介绍如何快速在服务器上部署一个 AI Demo,并确保服务在后台持续运行。

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
# 项目目录:/root/project/ai-demo
# 项目代码:/root/project/ai-demo/app.py
# 项目依赖:pip install gradio openai -U

import gradio as gr
import openai

openai.api_base = "https://api.openai.com/v1" # 国内需要替换代理 OpenAI API 地址
openai.api_key = "sk-proj-1234567890" # 替换成你的 OpenAI API 密钥

with gr.Blocks() as demo:
chatbot = gr.Chatbot(type="messages")
msg = gr.Textbox()
send_btn = gr.Button("发送")
clear_btn = gr.ClearButton([msg, chatbot])

def respond(message, chat_history):
print(message, chat_history)
# 将用户消息添加到历史记录中
chat_history.append({"role": "user", "content": message})

# 使用 OpenAI 的新 API 格式生成响应
messages = [{"role": "system", "content": "You are a helpful assistant."}] # 可选添加系统消息
messages.extend([{"role": entry["role"], "content": entry["content"]} for entry in chat_history])
print(messages)
response = openai.chat.completions.create(
model="gpt-4o-mini", # 如果有权限的话也可以使用 "gpt-4"
messages=messages
)

bot_message = response.choices[0].message.content.strip()

# 将助手的回复添加到历史记录中
chat_history.append({"role": "assistant", "content": bot_message})
return "", chat_history

msg.submit(respond, [msg, chatbot], [msg, chatbot])
send_btn.click(respond, [msg, chatbot], [msg, chatbot])

if __name__ == "__main__":
demo.launch()

1 在服务器上运行服务

为了确保你的 AI Demo 在服务器上持续运行,可以使用 tmux 来启动一个持续运行的服务。

步骤如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 1. 安装 tmux
apt install tmux -y

# 2. 创建一个新的 tmux 会话:
tmux new -s demo

# 3. 进入项目目录:
cd /root/project/ai-demo

# 4. 运行服务,默认端口为 7860
python app.py
# app.py 就是上文中提到的代码

# 按下 Ctrl+B 然后松开,再按 D 键来离开 tmux 会话,这样服务会继续在后台运行。

# 如果你需要重新进入 tmux 会话,可以使用:
tmux attach -t demo

2 域名解析到服务器

假设你已经购买了一个域名,并将其指向你服务器的 IP 地址。

在 DNS 解析中配置一条 A 记录即可,如:

  • Type:A record
  • Host: demo.example.ai
  • Value: 12.23.34.45

保存并等待一分钟。

完成后,你可以通过 ping 命令或者浏览器检查域名是否正确解析到服务器 IP。

1
2
3
4
5
ping demo.example.ai
PING demo.example.ai (12.23.34.45): 56 data bytes
64 bytes from 12.23.34.45: icmp_seq=0 ttl=48 time=42.597 ms
64 bytes from 12.23.34.45: icmp_seq=1 ttl=48 time=37.991 ms
...

3 配置 HTTPS 证书

为确保你的服务安全,我们将使用 Certbot 自动获取并配置免费的 SSL 证书。

步骤如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. 在服务器上安装 Certbot 和 Nginx
apt install certbot nginx -y

# 2. 使用 Certbot 获取证书:

# 如果不是第一次部署,需要先关闭 nginx
service nginx stop

# 获取证书
certbot certonly --standalone -d "demo.example.ai"
# 证书默认存放在:/etc/letsencrypt/live/demo.example.ai,后面配置 nginx 使用

# 重新开启 nginx
service nginx start

4 配置 Nginx 反向代理到服务

在这一步,我们将配置 Nginx 将外部的 HTTP/HTTPS 请求转发到你正在运行的服务。

步骤如下:

🔔 需要把 demo.example.ai 替换为你的域名

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
# 1. 打开并编辑 Nginx 配置文件:
# 以域名作为文件名称
vim /etc/nginx/sites-available/demo.example.ai

# 增加配置
server {
listen 80;
listen [::]:80;
server_name demo.example.ai;
rewrite ^(.*)$ https://demo.example.ai:443$1 permanent;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /etc/letsencrypt/live/demo.example.ai/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/demo.example.ai/privkey.pem;
keepalive_timeout 70;
server_name demo.example.ai;
server_tokens off;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;

location / {
proxy_pass http://127.0.0.1:7860;

proxy_buffering off;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

# 使 nginx 配置生效

# 安装 nginx_ensite
cd /root/software/
git clone https://github.com/perusio/nginx_ensite.git
cd nginx_ensite
sudo make install

# 安装 nginx 配置
nginx_ensite demo.example.ai

# 重新加载 nginx
service nginx reload

这样,Nginx 就会把 HTTP 请求转发到你服务的端口,确保外部访问是通过域名而不是直接通过 IP。

完成!现在访问 demo.example.ai 即可看到你的 AI Demo。

作者

Ailln

发布于

2025-02-15

更新于

2025-02-16

许可协议

评论