当前位置:网站首页>模型部署篇

模型部署篇

2022-06-10 08:05:00 算法之名

服务器部署

Flask

Flask是一个使用Python编写的轻量级Web应用框架。

安装Flask

pip install Flask

现在我们开始一个Hello World。

from flask import Flask, requestapp = Flask(__name__)@app.route("/hello")def helloword():    return "<h1>Hello World</h1>"if __name__ == '__main__':    app.run(host='192.168.0.138', port=8090, debug=True)

运行后显示

 * Serving Flask app "flask_web" (lazy loading) * Environment: production   WARNING: This is a development server. Do not use it in a production deployment.   Use a production WSGI server instead. * Debug mode: on * Running on http://192.168.0.138:8090/ (Press CTRL+C to quit) * Restarting with fsevents reloader * Debugger is active! * Debugger PIN: 235-830-661

我们在浏览器中输入http://192.168.0.138:8090/hello,得到

现在我们再增加一个非常重要的方法,图片上传,提供给模型进行前向推理。

from flask import Flask, requestimport osapp = Flask(__name__)@app.route("/hello")def helloword():    return "<h1>Hello World</h1>"@app.route("/upload", methods=['POST', 'GET'])def upload():    f = request.files.get('file')    print(f)    upload_path = os.path.join("/Users/admin/Documents/tmp/tmp." + f.filename.split('.')[-1])    print(upload_path)    f.save(upload_path)    return upload_pathif __name__ == '__main__':    app.run(host='192.168.0.138', port=8090, debug=True)

重新启动后,我们使用postman来上传文件

并在我们编辑的路径中找到该文件

运行结果

<FileStorage: '1.jpeg' ('image/jpeg')>/Users/admin/Documents/tmp/tmp.jpeg192.168.0.138 - - [02/Jun/2022 09:50:55] "POST /upload HTTP/1.1" 200 -

动态启动服务

安装gunicorn

pip install gunicorn

进入flask python文件的目录,我这里是

cd Downloads/PycharmProjects/untitled1/flask-web/

运行命令

gunicorn -b 192.168.0.138:8000 -w 2 flask_web:app

这里使用的端口号不需要跟代码中的相同,可以任意定义

运行日志

[2022-06-02 10:31:12 +0800] [1980] [INFO] Starting gunicorn 20.1.0[2022-06-02 10:31:12 +0800] [1980] [INFO] Listening at: http://192.168.0.138:8090 (1980)[2022-06-02 10:31:12 +0800] [1980] [INFO] Using worker: sync[2022-06-02 10:31:12 +0800] [1983] [INFO] Booting worker with pid: 1983[2022-06-02 10:33:40 +0800] [1980] [CRITICAL] WORKER TIMEOUT (pid:1983)[2022-06-02 10:33:40 +0800] [1983] [INFO] Worker exiting (pid: 1983)[2022-06-02 10:33:40 +0800] [1989] [INFO] Booting worker with pid: 1989

 

原网站

版权声明
本文为[算法之名]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/3768341/blog/5534345