Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import joblib
|
| 3 |
+
from gensim.models import Word2Vec
|
| 4 |
+
from gensim.utils import simple_preprocess
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pickle
|
| 7 |
+
|
| 8 |
+
import pickle
|
| 9 |
+
|
| 10 |
+
# Open the file in binary read mode
|
| 11 |
+
with open("Emailclassifier_model.pkl", "rb") as file:
|
| 12 |
+
classifier_model = pickle.load(file)
|
| 13 |
+
|
| 14 |
+
wordvect_model = Word2Vec.load("word2vec_model.model")
|
| 15 |
+
|
| 16 |
+
def preprocess_sentence(sentence):
|
| 17 |
+
return simple_preprocess(sentence)
|
| 18 |
+
|
| 19 |
+
def vectorize_sentence(tokens, wordvect_model):
|
| 20 |
+
tokens = [token for token in tokens if token in wordvect_model.wv]
|
| 21 |
+
print('Tokens are ----------------',tokens)
|
| 22 |
+
if not tokens:
|
| 23 |
+
return np.zeros(wordvect_model.vector_size)
|
| 24 |
+
print('avaergae', np.mean([wordvect_model.wv[token] for token in tokens], axis=0))
|
| 25 |
+
return np.mean([wordvect_model.wv[token] for token in tokens], axis=0)
|
| 26 |
+
|
| 27 |
+
def predict_sentence(sentence, wordvect_model, classifier):
|
| 28 |
+
tokens = preprocess_sentence(sentence)
|
| 29 |
+
vector = vectorize_sentence(tokens, wordvect_model)
|
| 30 |
+
prediction = classifier.predict([vector])
|
| 31 |
+
return "spam" if prediction == 1 else "ham"
|
| 32 |
+
|
| 33 |
+
st.title("Email Spam Classifier")
|
| 34 |
+
st.write("Enter the email content below:")
|
| 35 |
+
|
| 36 |
+
user_input = st.text_area("Email Content")
|
| 37 |
+
|
| 38 |
+
if st.button("Classify"):
|
| 39 |
+
result = predict_sentence(user_input, wordvect_model, classifier_model)
|
| 40 |
+
st.write(f"The email is classified as: {result}")
|
| 41 |
+
|