/
app.py
54 lines (45 loc) · 1.41 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
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 7 10:32:16 2020
@author: Ali
"""
from flask import Flask,render_template,url_for,request
import pickle
import string , regex
import nltk
nltk.download('wordnet')
import gzip
from nltk.stem import WordNetLemmatizer as wnl
# load the model from disk
with gzip.open('model_2.pkl', 'rb') as ifp:
m = pickle.load(ifp)
cv=pickle.load(open('vector_f.pkl','rb'))
def clean_text(text):
data = [char for char in text if char not in string.punctuation]
data = ''.join(data)
data = str(data)
words = regex.sub(r"(@[A-Za-z0-9]+)|([^A-Za-z0-9 \t])|(\w+:\/\/\S+)|^rt|http.+?", " ", data )
words = words.lower()
final_words = [wnl().lemmatize(word , pos = "v") for word in words.split()]
final_words = ' '.join(final_words)
return(final_words)
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/predict',methods=['POST'])
def predict():
if request.method == 'POST':
message = str(request.form['message'])
data = [message]
data_clean = clean_text(data)
data_clean = [data_clean]
vect = cv.transform(data_clean).toarray()
my_prediction = m.predict(vect)
if my_prediction == int(1):
output = 1
else:
output = 0
return render_template('result.html',prediction = output)
if __name__ == '__main__':
app.run(debug=True)