Search code examples
pythonweb-servicesmachine-learningscikit-learnpipeline

Pipeline API for predicting on text data giving - 'AttributeError:


My Pipeline model for prediction in text data is

pipe1=Pipeline([
    ('tfidf',TfidfVectorizer(analyzer=split_into_lemmas,min_df=20,max_df=3000)),
    ('classfier',MultinomialNB())
]) 

the API app is

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)

When I am hitting the url

http://127.0.0.1:5000/

getting the proper response, but when I am hitting the url for prediction with the text data as 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 

am getting below error

File "C:/Users/JaisoN/.spyder-py3/temp.py", line 30, in predict
    message=json_.get('message')
AttributeError: 'NoneType' object has no attribute 'get'

Solution

  • Curl request is not correct.

    change it to:

    curl localhost:5000/predict -d '{"foo": "bar"}' -H 'Content-Type: application/json'