divyanshu1807gupta
commited on
Commit
•
79703cf
1
Parent(s):
90617f4
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import nltk
|
3 |
+
#nltk.download('all')
|
4 |
+
from nltk import WordPunctTokenizer
|
5 |
+
# from nltk.corpus import stopwords
|
6 |
+
import string
|
7 |
+
import pickle
|
8 |
+
from nltk.stem.porter import PorterStemmer
|
9 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
10 |
+
from sklearn.model_selection import train_test_split
|
11 |
+
from sklearn.naive_bayes import MultinomialNB
|
12 |
+
import pandas as pd
|
13 |
+
|
14 |
+
spam=pickle.load(open('spam.pkl','rb'))
|
15 |
+
predict=pd.DataFrame(spam)
|
16 |
+
|
17 |
+
tfid=TfidfVectorizer(max_features=3000)
|
18 |
+
|
19 |
+
X=tfid.fit_transform(predict['new_text']).toarray()
|
20 |
+
Y=predict['target'].values
|
21 |
+
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.2,random_state=2)
|
22 |
+
mnb=MultinomialNB()
|
23 |
+
mnb.fit(X_train,Y_train)
|
24 |
+
|
25 |
+
ps=PorterStemmer()
|
26 |
+
|
27 |
+
list=['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
|
28 |
+
|
29 |
+
def action(text):
|
30 |
+
text=text.lower()
|
31 |
+
text=text.split()
|
32 |
+
|
33 |
+
y=[]
|
34 |
+
for words in text:
|
35 |
+
if words.isalnum():
|
36 |
+
y.append(words)
|
37 |
+
|
38 |
+
text=y[:]
|
39 |
+
y.clear()
|
40 |
+
for words in text:
|
41 |
+
if words not in list and words not in string.punctuation:
|
42 |
+
y.append(words)
|
43 |
+
|
44 |
+
text=y[:]
|
45 |
+
y.clear()
|
46 |
+
|
47 |
+
for words in text:
|
48 |
+
y.append(ps.stem(words))
|
49 |
+
|
50 |
+
return " ".join(y)
|
51 |
+
|
52 |
+
st.title("Spam Predictor")
|
53 |
+
text=st.text_input("Enter your message",placeholder="Enter here")
|
54 |
+
input_text=action(text)
|
55 |
+
final_text=tfid.transform([input_text])
|
56 |
+
prediction=mnb.predict(final_text)[0]
|
57 |
+
|
58 |
+
if st.button("Predict",type='primary',use_container_width=True):
|
59 |
+
if(prediction==1):
|
60 |
+
st.header("Spam")
|
61 |
+
else:
|
62 |
+
st.header("Not Spam")
|
63 |
+
st.balloons()
|
64 |
+
|
65 |
+
#predict=predict.rename(columns={'new_text':'Messages'})
|
66 |
+
#st.dataframe(predict['Messages'],use_container_width=True)
|