-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
63 lines (45 loc) · 1.69 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
from flask import Flask, render_template, request
import pandas as pd
# import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
import pickle
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
load_data = pd.read_csv('final_dataframe.csv')
data_df = load_data[["CONTENT", "CLASS"]]
# Our features and label
data_feature = data_df['CONTENT']
data_label = data_df['CLASS']
# Extracting features from text
corpus = data_feature
vectorizer = CountVectorizer()
# Fit the data (feature) which is in corpus
X = vectorizer.fit_transform(corpus)
# Splitting the data into training and testing
X_train, X_test, y_train, y_test = train_test_split(
X, data_label, test_size=0.33, random_state=42
)
# Building the model
clf = MultinomialNB()
clf.fit(X_train, y_train)
clf.score(X_test, y_test)
print("Model Score is: ", clf.score(X_test, y_test))
if request.method == "POST":
predict_comment = request.form['predict_comment']
data = [predict_comment]
print("The received comment is: ", data)
vectorizer_predict = vectorizer.transform(data).toarray()
model_prediction = clf.predict(vectorizer_predict)
if model_prediction == [1]:
model_prediction = "Spam"
elif model_prediction == [0]:
model_prediction = "Not Spam/ Ham"
return render_template('show.html', prediction=model_prediction)
if __name__ == '__main__':
app.run(debug=True)