Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,39 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
# Load
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
outputs=gr.Textbox(label="Prediction"),
|
37 |
-
title="AI-Powered Spam Detector",
|
38 |
-
description="Enter a message to check if it's spam or not, using a fine-tuned BERT model.",
|
39 |
)
|
40 |
|
41 |
-
# Run the app
|
42 |
if __name__ == "__main__":
|
43 |
-
|
44 |
-
print(df.head())
|
45 |
-
app.launch
|
|
|
1 |
import gradio as gr
|
2 |
+
from datasets import load_dataset
|
3 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
4 |
+
from sklearn.naive_bayes import MultinomialNB
|
5 |
+
from sklearn.pipeline import make_pipeline
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
+
from sklearn.metrics import accuracy_score
|
8 |
+
|
9 |
+
# 1. Load dataset
|
10 |
+
dataset = load_dataset("ucirvine/sms_spam", split="train")
|
11 |
+
texts = dataset["sms"]
|
12 |
+
labels = [1 if label == "spam" else 0 for label in dataset["label"]] # spam=1, ham=0
|
13 |
+
|
14 |
+
# 2. Train/test split
|
15 |
+
X_train, X_test, y_train, y_test = train_test_split(texts, labels, test_size=0.2, random_state=42)
|
16 |
+
|
17 |
+
# 3. Create model pipeline (TF-IDF + Naive Bayes)
|
18 |
+
model = make_pipeline(TfidfVectorizer(), MultinomialNB())
|
19 |
+
model.fit(X_train, y_train)
|
20 |
+
|
21 |
+
# 4. Accuracy for reference
|
22 |
+
y_pred = model.predict(X_test)
|
23 |
+
print("Validation Accuracy:", accuracy_score(y_test, y_pred))
|
24 |
+
|
25 |
+
# 5. Gradio interface
|
26 |
+
def predict_spam(message):
|
27 |
+
pred = model.predict([message])[0]
|
28 |
+
return "📩 Not Spam (Ham)" if pred == 0 else "🚫 Spam"
|
29 |
+
|
30 |
+
iface = gr.Interface(
|
31 |
+
fn=predict_spam,
|
32 |
+
inputs=gr.Textbox(lines=4, label="Enter your SMS message"),
|
33 |
+
outputs=gr.Text(label="Prediction"),
|
34 |
+
title="📬 SMS Spam Detector",
|
35 |
+
description="Classifies whether an SMS message is spam or not using a Naive Bayes model."
|
|
|
|
|
|
|
36 |
)
|
37 |
|
|
|
38 |
if __name__ == "__main__":
|
39 |
+
iface.launch(share=False)
|
|
|
|