-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
75 lines (37 loc) · 1.48 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from flask import Flask, render_template, jsonify, request
import pickle
import traceback
import pandas as pd
import numpy as np
from sklearn.linear_model import SGDClassifier
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
global label_dictionary,svm,vectorizer
#vectorizer=TfidfVectorizer(ngram_range=(1, 1), min_df=0.0, max_df=1.0)
label_dictionary = {0: 'negative review', 1: 'positive review'}
app = Flask(__name__,)
@app.route("/")
def index():
return render_template('form.html')
@app.route("/predict", methods=["POST"])
def predict():
try:
# json_ = request.json
query=[request.form.to_dict()['review']]
# query1=vectorizer.transform(query)
print(query)
prediction = (svm.predict(vectorizer.transform(query)))
print("#################################")
print(prediction)
return jsonify({'prediction': str(prediction),'label':label_dictionary[int(prediction)]})
except:
print(traceback.format_exc())
return jsonify({'trace': traceback.format_exc()})
if __name__ == '__main__':
port = 5000 # If you don't provide any port the port will be set to 12345
#load the model
svm_pkl=open('svm_classifier.pkl','rb')
svm=pickle.load(svm_pkl)
#load the vectorizer model
vectorizer_pkl=open('vectorizer.pkl','rb')
vectorizer=pickle.load(vectorizer_pkl)
app.run(host='0.0.0.0',port=port, debug=False)