Моя модель конвейера для прогнозирования в текстовых данных:
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
http://127.0.0.1: 5000 /
получаю правильный ответ, но когда я нажимаю 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'