Spaces:
Sleeping
Sleeping
App_Update
Browse files
app.py
CHANGED
@@ -1,31 +1,55 @@
|
|
1 |
-
pip install transformers
|
2 |
import streamlit as st
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
11 |
-
|
12 |
-
# Streamlit App Interface (similar to your previous code)
|
13 |
-
st.title("Email Spam Classifier")
|
14 |
input_sms = st.text_area("Enter message")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
transformed_sms = input_sms # Replace with your preprocessing logic if needed
|
19 |
-
|
20 |
-
# Tokenize the text
|
21 |
-
inputs = tokenizer(transformed_sms, return_tensors="pt")
|
22 |
|
23 |
-
# Make prediction
|
24 |
-
outputs = model(**inputs)
|
25 |
-
predictions = np.argmax(outputs.logits.detach().cpu().numpy(), axis=1)
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
st.header("Spam")
|
30 |
else:
|
31 |
-
st.header("Not Spam")
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import pickle
|
3 |
+
import nltk
|
4 |
+
from nltk.stem import PorterStemmer
|
5 |
+
from nltk.corpus import stopwords
|
6 |
+
import string
|
7 |
+
ps = PorterStemmer()
|
8 |
|
9 |
+
#lets load the saved vectorizer and native model
|
10 |
+
tfidf = pickle.load(open('vectorizer.pkl','rb'))
|
11 |
+
model = pickle.load(open('model.pkl','rb'))
|
12 |
|
13 |
+
#saving streamlit code
|
14 |
+
st.title("Email spam Classifier")
|
|
|
|
|
|
|
|
|
15 |
input_sms = st.text_area("Enter message")
|
16 |
+
def transform_text(text):
|
17 |
+
text = text.lower()
|
18 |
+
text = nltk.word_tokenize(text)
|
19 |
+
###############################################
|
20 |
+
# Check for alphabetics
|
21 |
+
y = []
|
22 |
+
for i in text:
|
23 |
+
if i.isalnum():
|
24 |
+
y.append(i)
|
25 |
+
text = y[:]
|
26 |
+
y.clear()
|
27 |
+
######################################################
|
28 |
+
# Remove punctuations
|
29 |
+
for i in text:
|
30 |
+
if i not in stopwords.words('english') and i not in string.punctuation:
|
31 |
+
y.append(i)
|
32 |
+
text = y[:] # copy content
|
33 |
+
y.clear() # Clear original list
|
34 |
+
#######################################################
|
35 |
+
# append steaming method(Root words)
|
36 |
+
for i in text:
|
37 |
+
y.append(ps.stem(i))
|
38 |
+
return " ".join(y)
|
39 |
|
40 |
+
# transform_text("I'm gonna be home soon and i don't want to talk about this stuff anymore tonight, k? I've cried enough today.")
|
41 |
+
return transform_text
|
|
|
|
|
|
|
|
|
42 |
|
|
|
|
|
|
|
43 |
|
44 |
+
if st.button('predict'):
|
45 |
+
#preprocess
|
46 |
+
transformed_sms = transform_text(input_sms)
|
47 |
+
#vectorize
|
48 |
+
vector_input = tfidf.transform([transformed_sms])
|
49 |
+
#predict
|
50 |
+
result = model.predict(vector_input)[0]
|
51 |
+
#display
|
52 |
+
if result ==1:
|
53 |
st.header("Spam")
|
54 |
else:
|
55 |
+
st.header("Not Spam")
|