Spaces:
Runtime error
Runtime error
Commit ·
f3b4ef6
1
Parent(s): a277399
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pickle
|
| 3 |
+
from keras import Sequential
|
| 4 |
+
from keras.layers import Dense,Embedding
|
| 5 |
+
from keras.utils import pad_sequences
|
| 6 |
+
from keras.preprocessing.text import Tokenizer
|
| 7 |
+
st.title("Spam-NonSpam Detector")
|
| 8 |
+
Input=st.text_input("Input","Write here...")
|
| 9 |
+
if st.button("Check"):
|
| 10 |
+
st.text("Analyzing may take upto a minute. Please be patient. Thank you!")
|
| 11 |
+
df=pd.read_csv("mail_data.csv")
|
| 12 |
+
df.loc[mail_data['Category'] == 'spam', 'Category'] = 0
|
| 13 |
+
df.loc[mail_data['Category'] == 'ham', 'Category'] = 1
|
| 14 |
+
X = df['Message']
|
| 15 |
+
Y = df['Category']
|
| 16 |
+
from keras.utils import pad_sequences
|
| 17 |
+
tokenizer = Tokenizer()
|
| 18 |
+
docs=X.astype("string")
|
| 19 |
+
tokenizer.fit_on_texts(docs)
|
| 20 |
+
sequences = tokenizer.texts_to_sequences(docs)
|
| 21 |
+
sequences = pad_sequences(sequences,padding='post',maxlen=61)
|
| 22 |
+
voc_size=len(tokenizer.word_index)
|
| 23 |
+
model = Sequential()
|
| 24 |
+
model.add(Embedding(voc_size+1,2,input_length=61))
|
| 25 |
+
model.add(Dense(5,activation="relu"))
|
| 26 |
+
model.add(Dense(5,activation="relu"))
|
| 27 |
+
model.add(Dense(1, activation='sigmoid'))
|
| 28 |
+
X=sequences
|
| 29 |
+
Y=Y.to_numpy()
|
| 30 |
+
Y=Y.astype("int")
|
| 31 |
+
Y=Y.reshape(-1,1)
|
| 32 |
+
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])
|
| 33 |
+
model.fit(X,Y,epochs=21)
|
| 34 |
+
InputDataFeatures=cv.transform([Input])
|
| 35 |
+
prediction=model.predict(InputDataFeatures)
|
| 36 |
+
st.text("Input:")
|
| 37 |
+
st.markdown(Input)
|
| 38 |
+
st.text("Output:")
|
| 39 |
+
if prediction==0:
|
| 40 |
+
st.text("Spam")
|
| 41 |
+
else:
|
| 42 |
+
st.text("Not Spam")
|