-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·65 lines (43 loc) · 1.39 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from flask import Flask, abort, jsonify, request
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.ensemble import RandomForestClassifier
nltk.download('stopwords')
from sklearn.eternals import joblib
from sklearn.naive_bayes import MultinomialNB
cv = CountVectorizer()
cv = joblib.load('cv.joblib.pkl')
classifier = MultinomialNB()
classifier = joblib.load('classifier.joblib.pkl')
app = Flask(__name__)
@app.route('/')
def index():
return 'Text Classifier for Surfcourse'
@app.route('/predict', methods=['POST'])
def predict():
# recieved data['query']
data = request.get_json(force=True)
ps = PorterStemmer()
query = data['query']
query = query.lower()
query = query.split()
query = [ps.stem(word) for word in query if not word in set(stopwords.words('english'))]
mod_query = ' '.join(query)
query = cv.transform([' '.join(query)])
prediction = classifier.predict_proba(query.toarray()).T
classes = classifier.classes_.tolist()
subjects = []
while(True):
val = prediction.argmax(axis=0)
if(prediction[val][0] == 0):
break
subjects.append(val)
prediction[val]=[0]
subjects = [classes[i[0]] for i in subjects]
if(len(subjects)>5):
subjects = subjects[:5]
return jsonify(sub=subjects , query = mod_query)
if __name__ == '__main__':
app.run(host='0.0.0.0')