-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
51 lines (30 loc) · 1.1 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
from flask import Flask, render_template, request, jsonify
import pickle
from sklearn.feature_extraction.text import CountVectorizer
app = Flask(__name__)
#Here we load the model and the vectorizer
model = pickle.load(open('models/model.pkl', 'rb'))
vectorizer = pickle.load(open('vectorizers/vectorizer.pkl', 'rb'))
#Defining the routes
@app.route('/')
def index():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
try:
extracted_text = request.form['extracted_text']
#Transforming text into numerical data through vectorizer
features = vectorizer.transform([extracted_text])
#Prediction
prediction = model.predict(features)
output='prediction_value'
#Ham-> 0 , Spam -> 1
if prediction[0]==1:
output = 'Spam'
else:
output='Not Spam'
return render_template('index.html', prediction_text = f'Prediction: {output}')
except Exception as e:
return jsonify({'error': str(e)})
if __name__ =='__main__':
app.run(debug=True)