Pipeline API for predicting on text data giving - 'AttributeError:
Pipeline API for predicting on text data giving - 'AttributeError:
我的文本数据预测管道模型是
pipe1=Pipeline([
('tfidf',TfidfVectorizer(analyzer=split_into_lemmas,min_df=20,max_df=3000)),
('classfier',MultinomialNB())
])
API 应用是
from flask import Flask, jsonify,request
from sklearn.externals import joblib
import pandas as pd
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
stop = set(stopwords.words('english'))
from nltk.stem.wordnet import WordNetLemmatizer
lemma = WordNetLemmatizer()
def split_into_lemmas(message):
message=message.lower()
words = word_tokenize(message)
words_sans_stop=[]
for word in words :
if word in stop:continue
words_sans_stop.append(word)
return [lemma.lemmatize(word) for word in words_sans_stop]
app = Flask(__name__)
@app.route('/',methods=['POST'])
def home():
return 'Hello Jaison'
@app.route('/predict', methods=['POST'])
def predict():
json_ = request.get_json(silent=True)
message=json_.get('message')
mydf = pd.DataFrame({'message':message})
print(mydf)
prediction = clf.predict_proba(mydf['message'])
return jsonify({'prediction': prediction.tolist()})
if __name__ == '__main__':
clf = joblib.load('/Users/JaisoN/my_model_pipeline.pkl')
app.run(port=5000)
当我点击 url
得到正确的响应,但是当我点击 url 进行预测时,文本数据为 JSON
curl -H "Content-Type: application/json" -X POST -d '{"message":["I‘m going to try for 2 months ha ha only joking"]}' http://127.0.0.1:5000/predict
低于错误
File "C:/Users/JaisoN/.spyder-py3/temp.py", line 30, in predict
message=json_.get('message')
AttributeError: 'NoneType' object has no attribute 'get'
Curl 请求不正确。
改为:
curl localhost:5000/predict -d '{"foo": "bar"}' -H 'Content-Type: application/json'
我的文本数据预测管道模型是
pipe1=Pipeline([
('tfidf',TfidfVectorizer(analyzer=split_into_lemmas,min_df=20,max_df=3000)),
('classfier',MultinomialNB())
])
API 应用是
from flask import Flask, jsonify,request
from sklearn.externals import joblib
import pandas as pd
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
stop = set(stopwords.words('english'))
from nltk.stem.wordnet import WordNetLemmatizer
lemma = WordNetLemmatizer()
def split_into_lemmas(message):
message=message.lower()
words = word_tokenize(message)
words_sans_stop=[]
for word in words :
if word in stop:continue
words_sans_stop.append(word)
return [lemma.lemmatize(word) for word in words_sans_stop]
app = Flask(__name__)
@app.route('/',methods=['POST'])
def home():
return 'Hello Jaison'
@app.route('/predict', methods=['POST'])
def predict():
json_ = request.get_json(silent=True)
message=json_.get('message')
mydf = pd.DataFrame({'message':message})
print(mydf)
prediction = clf.predict_proba(mydf['message'])
return jsonify({'prediction': prediction.tolist()})
if __name__ == '__main__':
clf = joblib.load('/Users/JaisoN/my_model_pipeline.pkl')
app.run(port=5000)
当我点击 url
得到正确的响应,但是当我点击 url 进行预测时,文本数据为 JSON
curl -H "Content-Type: application/json" -X POST -d '{"message":["I‘m going to try for 2 months ha ha only joking"]}' http://127.0.0.1:5000/predict
低于错误
File "C:/Users/JaisoN/.spyder-py3/temp.py", line 30, in predict
message=json_.get('message')
AttributeError: 'NoneType' object has no attribute 'get'
Curl 请求不正确。
改为:
curl localhost:5000/predict -d '{"foo": "bar"}' -H 'Content-Type: application/json'