Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import gradio as gr
|
3 |
+
from sklearn.model_selection import train_test_split
|
4 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
5 |
+
from sklearn.naive_bayes import MultinomialNB
|
6 |
+
|
7 |
+
# Load and clean the dataset
|
8 |
+
data = pd.read_csv(r"spam.csv")
|
9 |
+
data.drop_duplicates(inplace=True)
|
10 |
+
data['Category'] = data['Category'].replace(['ham', 'spam'], ['Not spam', 'spam'])
|
11 |
+
|
12 |
+
# Prepare data
|
13 |
+
X = data['Message']
|
14 |
+
y = data['Category']
|
15 |
+
|
16 |
+
# Split into training and testing sets
|
17 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
|
18 |
+
|
19 |
+
# Convert text data to numerical features
|
20 |
+
vectorizer = CountVectorizer(stop_words='english')
|
21 |
+
X_train_features = vectorizer.fit_transform(X_train)
|
22 |
+
X_test_features = vectorizer.transform(X_test)
|
23 |
+
|
24 |
+
# Train the model
|
25 |
+
model = MultinomialNB()
|
26 |
+
model.fit(X_train_features, y_train)
|
27 |
+
|
28 |
+
# Define prediction function
|
29 |
+
def predict_spam(message):
|
30 |
+
message_features = vectorizer.transform([message])
|
31 |
+
prediction = model.predict(message_features)[0]
|
32 |
+
return f"Prediction: {prediction}"
|
33 |
+
|
34 |
+
# Launch Gradio interface
|
35 |
+
gr.Interface(
|
36 |
+
fn=predict_spam,
|
37 |
+
inputs="text",
|
38 |
+
outputs="text",
|
39 |
+
title="📩 Spam Detection with Gradio",
|
40 |
+
description="Enter a message and the model will predict whether it's spam or not."
|
41 |
+
).launch()
|