小学期大作业是做一个电商网站,所以利用了chatterbot作为人工智能客服。
在Linux上使用了使用flask + nginx + Gunicorn进行部署
chatterbot需要python 3.7以上的版本,阿里云的服务器自带该版本。
可以通过输入python3查看版本。
[root@iZ2zeg1vqrxi25jo2mb4oeZ home]# python3
Python 3.7.7 (default, Oct 23 2020, 19:20:54)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
随后输入git clone命令,获取flask-chatterbot的代码。
git clone https://github.com/chamkank/flask-chatterbot.git test_flask-chatterbot
进入test_flask-chatterbot,运行pip命令,安装依赖的插件
pip install -r requirements.txt
最后在test_flask-chatterbot中运行app.py
[root@iZ2zeg1vqrxi25jo2mb4oeZ home]# python3 app.py
ai.yml Training: [####################] 100%
botprofile.yml Training: [####################] 100%
computers.yml Training: [####################] 100%
conversations.yml Training: [####################] 100%
emotion.yml Training: [####################] 100%
food.yml Training: [####################] 100%
gossip.yml Training: [####################] 100%
greetings.yml Training: [####################] 100%
history.yml Training: [####################] 100%
humor.yml Training: [####################] 100%
literature.yml Training: [####################] 100%
money.yml Training: [####################] 100%
movies.yml Training: [####################] 100%
politics.yml Training: [####################] 100%
psychology.yml Training: [####################] 100%
science.yml Training: [####################] 100%
sports.yml Training: [####################] 100%
trivia.yml Training: [####################] 100%
Serving Flask app “app” (lazy loading)
Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
Debug mode: off
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
这样就说明flask项目运行成功了。
首先安装Gunicorn
pip3 install gunicorn
之后在test_flask-chatterbot目录运行Gunicorn
gunicorn -w 1 -b 127.0.0.1:8081 -t 1200 app:app
注意一定要加参数-t,且该值一定要设大,因为Gunicorn默认的值是30s,服务器30s没完成部署就会杀死进程,而30s并不足以完成训练。
运行成功的结果如下
[2020-10-27 13:36:41 +0800] [21551] [INFO] Starting gunicorn 20.0.4
[2020-10-27 13:36:41 +0800] [21551] [INFO] Listening at: http://127.0.0.1:8081 (21551)
[2020-10-27 13:36:41 +0800] [21551] [INFO] Using worker: sync
[2020-10-27 13:36:41 +0800] [21554] [INFO] Booting worker with pid: 21554
Training ai.yml: [####################] 100%
Training botprofile.yml: [####################] 100%
Training computers.yml: [####################] 100%
Training conversations.yml: [####################] 100%
Training emotion.yml: [####################] 100%
Training food.yml: [####################] 100%
Training gossip.yml: [####################] 100%
Training greetings.yml: [####################] 100%
Training health.yml: [####################] 100%
Training history.yml: [####################] 100%
Training humor.yml: [####################] 100%
Training literature.yml: [####################] 100%
Training money.yml: [####################] 100%
Training movies.yml: [####################] 100%
Training politics.yml: [####################] 100%
Training psychology.yml: [####################] 100%
Training science.yml: [####################] 100%
Training sports.yml: [####################] 100%
Training trivia.yml: [####################] 100%
首先下载并解压nginx包
然后再nginx目录进行安装
./configure
make
make install
可以在/usr/local找到安装的nginx
cd /usr/local/nginx
打开/usr/local/nginx/conf/nginx.conf修改配置
vim /usr/local/nginx/conf/nginx.conf
server {
listen 8089;
server_name 39.106.***.**; # 这是HOST机器的外部地址
location / {
proxy_pass http://127.0.0.1:8081; # 这里是指向 gunicorn host 的服务地址
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
然后启动nginx即可
/usr/local/nginx/sbin/nginx
现在就可以使用外部机器访问该应用了
参考:
1.https://www.cnblogs.com/Ray-liang/p/4837850.html.
2.从零开始搭建ChatterBot环境