-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
44 lines (37 loc) · 1.08 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
from flask import Flask,render_template,url_for,request
from sklearn.feature_extraction.text import CountVectorizer
import joblib
import re
import string
app=Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/predict',methods=['POST'])
def predict():
# loading the model from the disk
filename='model.pkl'
cvfile='cv.pkl'
clf=joblib.load(filename)
cv=joblib.load(cvfile)
if request.method=='POST':
txt=request.form['message']
clean=re.compile('<.*?>')
txt=re.sub(clean,'',txt)
txt=txt.lower()
txt=re.sub('\[.*?\]','',txt)
txt=re.sub('[%s]'%re.escape(string.punctuation),'',txt)
txt=re.sub('\w*\d\w*','',txt)
txt=re.sub('[''"",,,]','',txt)
txt=re.sub('\n','',txt)
txt=[txt]
txt=cv.transform(txt).toarray()
mypred=clf.predict(txt)
res=None
if mypred[0]=='ham':
res=0
else:
res=1
return render_template('result.html',prediction=res)
if __name__=='__main__':
app.run(debug=True)