spam-detector / app.py
Meryb's picture
Update app.py
c3db3b9 verified
raw
history blame
2.01 kB
import pandas as pd
import gradio as gr
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
# Load and clean the dataset
data = pd.read_csv("spam.csv")
data.drop_duplicates(inplace=True)
data['Category'] = data['Category'].replace(['ham', 'spam'], ['Not spam', 'Spam'])
# Prepare data
X = data['Message']
y = data['Category']
# Split into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Convert text data to numerical features
vectorizer = CountVectorizer(stop_words='english')
X_train_features = vectorizer.fit_transform(X_train)
X_test_features = vectorizer.transform(X_test)
# Train the model
model = MultinomialNB()
model.fit(X_train_features, y_train)
# Define prediction function
def predict_spam(message):
message_features = vectorizer.transform([message])
prediction = model.predict(message_features)[0]
return prediction
# Build better UI
with gr.Blocks(theme=gr.themes.Default()) as demo:
gr.Markdown("## πŸ“© Spam Detector\nEnter any message below to check if it's spam or not.")
with gr.Row():
with gr.Column(scale=3):
message_input = gr.Textbox(
label="Your Message",
placeholder="e.g. Congratulations! You've won a prize...",
lines=4
)
submit_btn = gr.Button("Detect Spam")
with gr.Column(scale=2):
result_output = gr.Label(label="Prediction")
examples = [
["Congratulations! You have been selected for a free cruise!"],
["Hey, what time is class tomorrow?"],
["Win cash now!!! Click here"],
["Lunch at 1 PM?"],
]
gr.Examples(
examples=examples,
inputs=message_input
)
submit_btn.click(fn=predict_spam, inputs=message_input, outputs=result_output)
# Launch app
if __name__ == "__main__":
demo.launch()