通过flask中的请求参数访问url参数
我一直试图通过flask部署一个机器学习模型。我需要通过url传递一些文本,以便可以对其进行检索以进行分类。我正在使用request.args.get函数,但它抛出了错误:通过flask中的请求参数访问url参数,flask,Flask,我一直试图通过flask部署一个机器学习模型。我需要通过url传递一些文本,以便可以对其进行检索以进行分类。我正在使用request.args.get函数,但它抛出了错误: TypeError: index() got an unexpected keyword argument 'text' 网址是: http://127.0.0.1:8000/index/text=Windows%20is%20a%20operating%20system 它也显示了错误: Internal Server
TypeError: index() got an unexpected keyword argument 'text'
网址是:
http://127.0.0.1:8000/index/text=Windows%20is%20a%20operating%20system
它也显示了错误:
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
service.py:
from flask import Flask, request, jsonify
import pickle
app = Flask(__name__)
file = open("models/GridSearchCV_nb.pickle","rb")
nb_clf = pickle.load(file)
file = open("models/GridSearchCV_svm.pickle","rb")
svc_clf = pickle.load(file)
file = open("models/ctargets.pickle","rb")
targets = pickle.load(file)
def nbclassifyit(text):
idx_nb = nb_clf.predict([text])[0]
return targets[idx_nb]
def svclassifyit(text):
idx_svc = svc_clf.predict([text])[0]
return targets[idx_svc]
@app.route('/index/<text>')
def index():
text = request.args.get('text')
result = {"prediction" : nbclassifyit(text)}
return jsonify(result)
从烧瓶导入烧瓶,请求,jsonify
进口泡菜
app=烧瓶(名称)
文件=打开(“模型/GridSearchCV_nb.pickle”、“rb”)
nb_clf=pickle.load(文件)
文件=打开(“models/GridSearchCV_svm.pickle”、“rb”)
svc_clf=pickle.load(文件)
文件=打开(“models/ctargets.pickle”、“rb”)
targets=pickle.load(文件)
def nbclassifyit(文本):
idx_nb=nb_clf.predict([text])[0]
返回目标[idx_nb]
def svclassifyit(文本):
idx_svc=svc_clf.predict([text])[0]
返回目标[idx_svc]
@app.route(“/index/”)
def index():
text=request.args.get('text')
结果={“预测”:nbclassifyit(文本)}
返回jsonify(结果)
我做错了什么?路径末尾的
请求.args.get
函数在?
之后查找参数。您不需要在路线中使用
因此,您可以尝试更改索引路由和函数,如下所示:
@app.route('/index/')
def index():
text = request.args.get('text')
result = {"prediction" : nbclassifyit(text)}
return jsonify(result)
http://127.0.0.1:8000/index/?text=Windows%20is%20a%20operating%20system
并在url中指定如下文本参数:
@app.route('/index/')
def index():
text = request.args.get('text')
result = {"prediction" : nbclassifyit(text)}
return jsonify(result)
http://127.0.0.1:8000/index/?text=Windows%20is%20a%20operating%20system
request.args.get
函数在路由末尾的?
之后查找参数。您不需要在路线中使用
因此,您可以尝试更改索引路由和函数,如下所示:
@app.route('/index/')
def index():
text = request.args.get('text')
result = {"prediction" : nbclassifyit(text)}
return jsonify(result)
http://127.0.0.1:8000/index/?text=Windows%20is%20a%20operating%20system
并在url中指定如下文本参数:
@app.route('/index/')
def index():
text = request.args.get('text')
result = {"prediction" : nbclassifyit(text)}
return jsonify(result)
http://127.0.0.1:8000/index/?text=Windows%20is%20a%20operating%20system
您可以从路径中删除最后一个
/
。url将是:http://127.0.0.1:8000/index?text=Windows%20is%20a%20operating%20system
是的,您可以,我认为/
是可选的,路由将与两种模式一起工作,对吗?您可以从路径中删除最后一个/
。url将是:http://127.0.0.1:8000/index?text=Windows%20is%20a%20operating%20system
是的,你可以,我认为/
是可选的,路线可以同时使用两种模式,对吗?