-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
152 lines (132 loc) · 4.56 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from flask import Flask,render_template,url_for,request
import pandas as pd
import pickle
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.externals import joblib
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/youtube',methods=['POST'])
def youtube():
return render_template('youtube.html')
@app.route('/text',methods=['POST'])
def text():
return render_template('text.html')
@app.route('/email',methods=['POST'])
def email():
return render_template('email.html')
@app.route('/sms',methods=['POST'])
def sms():
return render_template('sms.html')
@app.route('/predictyoutube',methods=['POST'])
def predictyoutube():
df= pd.read_csv("YoutubeSpamMergedData.csv")
df_data = df[["CONTENT","CLASS"]]
# Features and Labels
df_x = df_data['CONTENT']
df_y = df_data.CLASS
# Extract Feature With CountVectorizer
corpus = df_x
cv = CountVectorizer()
X = cv.fit_transform(corpus) # Fit the Data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, df_y, test_size=0.33, random_state=42)
#Naive Bayes Classifier
from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB()
clf.fit(X_train,y_train)
clf.score(X_test,y_test)
#Alternative Usage of Saved Model
# ytb_model = open("naivebayes_spam_model.pkl","rb")
# clf = joblib.load(ytb_model)
if request.method == 'POST':
comment = request.form['comment']
data = [comment]
vect = cv.transform(data).toarray()
my_prediction = clf.predict(vect)
return render_template('resultyoutube.html',prediction = my_prediction)
@app.route('/predictemail',methods=['POST'])
def predictemail():
df= pd.read_csv("spam_ham_dataset.csv")
df_data = df[["text","label_num"]]
# Features and Labels
df_x = df_data['text']
df_y = df_data.label_num
# Extract Feature With CountVectorizer
corpus = df_x
cv = CountVectorizer()
X = cv.fit_transform(corpus) # Fit the Data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, df_y, test_size=0.33, random_state=42)
#Naive Bayes Classifier
from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB()
clf.fit(X_train,y_train)
clf.score(X_test,y_test)
#Alternative Usage of Saved Model
# ytb_model = open("naivebayes_spam_model.pkl","rb")
# clf = joblib.load(ytb_model)
if request.method == 'POST':
comment = request.form['comment']
data = [comment]
vect = cv.transform(data).toarray()
my_prediction = clf.predict(vect)
return render_template('resultemail.html',prediction = my_prediction)
@app.route('/predictsms',methods=['POST'])
def predictsms():
df= pd.read_csv("SMSSPAMDATA.csv")
df_data = df[["SMS","CLASS"]]
# Features and Labels
df_x = df_data['SMS']
df_y = df_data.CLASS
# Extract Feature With CountVectorizer
corpus = df_x
cv = CountVectorizer()
X = cv.fit_transform(corpus) # Fit the Data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, df_y, test_size=0.33, random_state=42)
#Naive Bayes Classifier
from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB()
clf.fit(X_train,y_train)
clf.score(X_test,y_test)
#Alternative Usage of Saved Model
# ytb_model = open("naivebayes_spam_model.pkl","rb")
# clf = joblib.load(ytb_model)
if request.method == 'POST':
comment = request.form['comment']
data = [comment]
vect = cv.transform(data).toarray()
my_prediction = clf.predict(vect)
return render_template('resultsms.html',prediction = my_prediction)
@app.route('/predicttext',methods=['POST'])
def predicttext():
df= pd.read_csv("textspam.csv")
df_data = df[["text","CLASS"]]
# Features and Labels
df_x = df_data['text']
df_y = df_data.CLASS
# Extract Feature With CountVectorizer
corpus = df_x
cv = CountVectorizer()
X = cv.fit_transform(corpus) # Fit the Data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, df_y, test_size=0.33, random_state=42)
#Naive Bayes Classifier
from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB()
clf.fit(X_train,y_train)
clf.score(X_test,y_test)
#Alternative Usage of Saved Model
# ytb_model = open("naivebayes_spam_model.pkl","rb")
# clf = joblib.load(ytb_model)
if request.method == 'POST':
comment = request.form['comment']
data = [comment]
vect = cv.transform(data).toarray()
my_prediction = clf.predict(vect)
return render_template('resulttext.html',prediction = my_prediction)
if __name__ == '__main__':
app.run(debug=True)