Spaces:
Build error
Build error
Upload 5 files
Browse files- app.py +64 -0
- model.pkl +3 -0
- nltk.txt +2 -0
- requirements.txt +3 -3
- vectorizer.pkl +3 -0
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pickle
|
| 3 |
+
import string
|
| 4 |
+
import nltk
|
| 5 |
+
from nltk.corpus import stopwords
|
| 6 |
+
from nltk.stem.porter import PorterStemmer
|
| 7 |
+
|
| 8 |
+
# π§ Download required NLTK resources only once
|
| 9 |
+
try:
|
| 10 |
+
nltk.data.find('tokenizers/punkt')
|
| 11 |
+
except LookupError:
|
| 12 |
+
nltk.download('punkt')
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
nltk.data.find('corpora/stopwords')
|
| 16 |
+
except LookupError:
|
| 17 |
+
nltk.download('stopwords')
|
| 18 |
+
|
| 19 |
+
# π€ Initialize stemmer
|
| 20 |
+
ps = PorterStemmer()
|
| 21 |
+
|
| 22 |
+
# π Preprocessing function
|
| 23 |
+
def transform_text(text):
|
| 24 |
+
text = text.lower()
|
| 25 |
+
text = nltk.word_tokenize(text)
|
| 26 |
+
|
| 27 |
+
y = []
|
| 28 |
+
for word in text:
|
| 29 |
+
if word.isalnum():
|
| 30 |
+
y.append(word)
|
| 31 |
+
|
| 32 |
+
text = y[:]
|
| 33 |
+
y.clear()
|
| 34 |
+
|
| 35 |
+
for word in text:
|
| 36 |
+
if word not in stopwords.words('english') and word not in string.punctuation:
|
| 37 |
+
y.append(ps.stem(word))
|
| 38 |
+
|
| 39 |
+
return " ".join(y)
|
| 40 |
+
|
| 41 |
+
# π¦ Load model and vectorizer
|
| 42 |
+
tfidf = pickle.load(open('vectorizer.pkl', 'rb'))
|
| 43 |
+
model = pickle.load(open('model.pkl', 'rb'))
|
| 44 |
+
|
| 45 |
+
# π¬ Streamlit UI
|
| 46 |
+
st.title("π© SMS Spam Classifier")
|
| 47 |
+
|
| 48 |
+
input_sms = st.text_area("Enter the message")
|
| 49 |
+
|
| 50 |
+
if st.button('Predict'):
|
| 51 |
+
# 1. Preprocess
|
| 52 |
+
transformed_sms = transform_text(input_sms)
|
| 53 |
+
|
| 54 |
+
# 2. Vectorize
|
| 55 |
+
vector_input = tfidf.transform([transformed_sms])
|
| 56 |
+
|
| 57 |
+
# 3. Predict
|
| 58 |
+
result = model.predict(vector_input)[0]
|
| 59 |
+
|
| 60 |
+
# 4. Show result
|
| 61 |
+
if result == 1:
|
| 62 |
+
st.error("π« Spam")
|
| 63 |
+
else:
|
| 64 |
+
st.success("β
Not Spam")
|
model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1b9e10101ebb499ed8198528e137234642c377827d8ded234c7292dac890cf28
|
| 3 |
+
size 147
|
nltk.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
punkt
|
requirements.txt
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
nltk
|
| 3 |
+
sklearn
|
vectorizer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:05d5bc6b5ccde6839aefc33be9b9974d59020956f930de583e236559d82f2322
|
| 3 |
+
size 170452
|