通过 Flask 制作一个简单的微服务预测接口,实现模型的预测和浏览器交互功能:
import joblib
from flask import Flask, request
import numpy as np
'''
# 不使用浏览器,常规的控制台交互
model_path = ".//model.pkl"
model = joblib.load(model_path)
sen = [[['坚决', 'a', 'ad', '1_v'],
['惩治', 'v', 'v', '0_Root'],
['贪污', 'v', 'v', '1_v'],
['贿赂', 'n', 'n', '-1_v'],
['等', 'u', 'udeng', '-1_v'],
['经济', 'n', 'n', '1_v'],
['犯罪', 'v', 'vn', '-2_v']]]
print(model.predict(sen))
'''
# Flask 实现模型和浏览器进行交互
app = Flask(__name__)
@app.route('/')
def hello_world():
return '你好'
@app.route('/predict/<sen>')
def predict(sen):
print("参数:", type(sen), sen) # 得到的为字符串而不是数组,故结果不对,解决办法??? 没想到。。
result = model.predict(sen)
return str(result)
if __name__ == '__main__':
app.config['JSON_AS_ASCII'] = False # 中文乱码
model_path = ".//model.pkl"
model = joblib.load(model_path)
app.run()
原文:
https://soyoger.blog.csdn.net/article/details/108729394