Error: While calling Flask API (Json Decodeerror)
Error: While calling Flask API (Json Decodeerror)
您好,我是将 Ml 模型作为烧瓶公开的新手 API。下面是我的代码:
import numpy as np
from nltk.corpus import wordnet
from nltk.stem.wordnet import WordNetLemmatizer
import re
from sklearn.externals import joblib
import warnings
warnings.filterwarnings('ignore')
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route("/glcoding", methods=['POST'])
def mylemmatize(token):
lmtzr = WordNetLemmatizer()
lemmas = {}
lemma = None
if not token in lemmas:
lemma = wordnet.morphy(token)
if not lemma:
lemma = token
if lemma == token:
lemma = lmtzr.lemmatize(token)
lemmas[token] = lemma
return lemmas[token]
def cleanmytext(text):
words = map(mylemmatize,text.lower().split())
return ' '.join(words)
def glcoding():
if request.method == 'POST':
json_data = request.get_json()
data = pd.read_json(json_data, orient='index')
data['Invoice line item description'] = data['Invoice line item description'].apply(cleanmytext)
return jsonify(data)
if __name__ == '__main__':
app.run()
我用下面的代码调用 API:
from flask import Flask, jsonify, request
import requests, json
BASE_URL = "http://127.0.0.1:5000"
data = '{"0":{"Vendor Number": "166587","Invoice line item description":"Petrol charges with electricity"}}'
response = requests.post("{}/glcoding".format(BASE_URL), json = data)
response.json()
我收到如下所述的错误:
Traceback (most recent call last):
TypeError: mylemmatize() takes exactly 1 argument (0 given)
127.0.0.1 - - [16/Mar/2018 14:31:51] "POST /glcoding HTTP/1.1" 500 -
当我不将其公开为 API 时,上面的代码工作正常。但它仅在从 API 调用时才会抛出错误。请帮助
您定义了您的请求处理程序 mylemmatize(token)
以获取名为 token
的变量,但您的路由并不知道这一点,因此不会将您的数据传递给请求处理程序。
从 :
更改路线
@app.route("/glcoding", methods=['POST'])
改为:
@app.route("/glcoding/<token>", methods=['POST'])
有关详细信息,请参阅 doc on variable rule。
此外,如果您不需要将令牌作为变量传递,那么您需要
将其从您的 mylemmatize
函数定义中删除。
你用 app.route()
装饰器装饰了错误的方法。只需将装饰器移动到 glcoding()
方法上方,一切都应该正常工作。
您好,我是将 Ml 模型作为烧瓶公开的新手 API。下面是我的代码:
import numpy as np
from nltk.corpus import wordnet
from nltk.stem.wordnet import WordNetLemmatizer
import re
from sklearn.externals import joblib
import warnings
warnings.filterwarnings('ignore')
from flask import Flask, jsonify, request
app = Flask(__name__)
@app.route("/glcoding", methods=['POST'])
def mylemmatize(token):
lmtzr = WordNetLemmatizer()
lemmas = {}
lemma = None
if not token in lemmas:
lemma = wordnet.morphy(token)
if not lemma:
lemma = token
if lemma == token:
lemma = lmtzr.lemmatize(token)
lemmas[token] = lemma
return lemmas[token]
def cleanmytext(text):
words = map(mylemmatize,text.lower().split())
return ' '.join(words)
def glcoding():
if request.method == 'POST':
json_data = request.get_json()
data = pd.read_json(json_data, orient='index')
data['Invoice line item description'] = data['Invoice line item description'].apply(cleanmytext)
return jsonify(data)
if __name__ == '__main__':
app.run()
我用下面的代码调用 API:
from flask import Flask, jsonify, request
import requests, json
BASE_URL = "http://127.0.0.1:5000"
data = '{"0":{"Vendor Number": "166587","Invoice line item description":"Petrol charges with electricity"}}'
response = requests.post("{}/glcoding".format(BASE_URL), json = data)
response.json()
我收到如下所述的错误:
Traceback (most recent call last):
TypeError: mylemmatize() takes exactly 1 argument (0 given)
127.0.0.1 - - [16/Mar/2018 14:31:51] "POST /glcoding HTTP/1.1" 500 -
当我不将其公开为 API 时,上面的代码工作正常。但它仅在从 API 调用时才会抛出错误。请帮助
您定义了您的请求处理程序 mylemmatize(token)
以获取名为 token
的变量,但您的路由并不知道这一点,因此不会将您的数据传递给请求处理程序。
从 :
@app.route("/glcoding", methods=['POST'])
改为:
@app.route("/glcoding/<token>", methods=['POST'])
有关详细信息,请参阅 doc on variable rule。
此外,如果您不需要将令牌作为变量传递,那么您需要
将其从您的 mylemmatize
函数定义中删除。
你用 app.route()
装饰器装饰了错误的方法。只需将装饰器移动到 glcoding()
方法上方,一切都应该正常工作。