aditya-s-yadav commited on
Commit
1d6c859
Β·
verified Β·
1 Parent(s): 3f247de

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pickle
3
+ import string
4
+ from nltk.corpus import stopwords
5
+ import nltk
6
+ from nltk.stem.porter import PorterStemmer
7
+ ps = PorterStemmer()
8
+
9
+
10
+ # Function to preprocess the input text
11
+ def transform_text(text):
12
+ text = text.lower() # Convert to lowercase
13
+ text = nltk.word_tokenize(text) # Tokenize the text
14
+ y = []
15
+
16
+ # Removing alphanumeric tokens
17
+ for i in text:
18
+ if i.isalnum():
19
+ y.append(i)
20
+
21
+ text = y[:]
22
+ y.clear()
23
+
24
+ # Removing stopwords and punctuation
25
+ for i in text:
26
+ if i not in stopwords.words('english') and i not in string.punctuation:
27
+ y.append(i)
28
+
29
+ text = y[:]
30
+ y.clear()
31
+
32
+ # Performing stemming
33
+ for i in text:
34
+ y.append(ps.stem(i))
35
+
36
+ return " ".join(y) # Join the list into a single string with spaces
37
+
38
+
39
+ # Load the saved models (TF-IDF Vectorizer and the classification model)
40
+ tfidf = pickle.load(open('vectorizer.pkl', 'rb'))
41
+ model = pickle.load(open('model.pkl', 'rb'))
42
+
43
+ # Setting up the main title and description
44
+ st.title("πŸ“§ Email/SMS Spam Classifier")
45
+ st.write("""
46
+ ### Enter a message to determine whether it's Spam or Not Spam.
47
+ This classifier uses **natural language processing (NLP)** techniques to preprocess and predict based on your input.
48
+ """)
49
+
50
+ # Input text field for user to enter the message
51
+ st.write("#### Message Input:")
52
+ input_sms = st.text_area("Type or paste your message here", height=150)
53
+
54
+ # Add a button to trigger the classification
55
+ if st.button("πŸ” Classify Message"):
56
+ if input_sms.strip(): # Ensure there's text in the input
57
+ ## 1. Preprocess the input text
58
+ with st.spinner('Processing...'):
59
+ transformed_sms = transform_text(input_sms)
60
+
61
+ ## 2. Vectorize the transformed text
62
+ vector_input = tfidf.transform([transformed_sms])
63
+
64
+ ## 3. Predict the label (Spam or Not Spam)
65
+ result = model.predict(vector_input)[0]
66
+
67
+ ## 4. Display the result with appropriate color and message
68
+ if result == 1:
69
+ st.success("πŸ”΄ This message is classified as **Spam**.")
70
+ else:
71
+ st.success("🟒 This message is classified as **Not Spam**.")
72
+ else:
73
+ st.warning("Please enter a valid message to classify.")
74
+
75
+ # Adding a footer with a reference to your classifier and author
76
+ st.markdown("""
77
+ ---
78
+ Developed using **Streamlit** and **NLP techniques**.<br>
79
+ **Author**: **Aditya Yadav**
80
+ """, unsafe_allow_html=True)