Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,41 @@
|
|
| 1 |
-
|
| 2 |
-
import streamlit as st
|
| 3 |
import joblib
|
| 4 |
|
| 5 |
# Load vectorizer and models
|
| 6 |
vectorizer = joblib.load("tfidf_vectorizer.pkl")
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
"SVM": joblib.load("svm_model.pkl")
|
| 11 |
-
}
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
else:
|
| 23 |
-
model = models[model_choice]
|
| 24 |
-
X_input = vectorizer.transform([message])
|
| 25 |
-
prediction = model.predict(X_input)[0]
|
| 26 |
-
label = "🟢 Ham" if prediction == 0 else "🔴 Spam"
|
| 27 |
-
st.success(f"Prediction ({model_choice}): {label}")
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
import joblib
|
| 3 |
|
| 4 |
# Load vectorizer and models
|
| 5 |
vectorizer = joblib.load("tfidf_vectorizer.pkl")
|
| 6 |
+
log_model = joblib.load("logistic_model.pkl")
|
| 7 |
+
nb_model = joblib.load("naive_bayes_model.pkl")
|
| 8 |
+
svm_model = joblib.load("svm_model.pkl")
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Prediction function
|
| 11 |
+
def predict_spam(message, model_name):
|
| 12 |
+
if not message.strip():
|
| 13 |
+
return "⚠️ Please enter a message."
|
| 14 |
+
|
| 15 |
+
X_input = vectorizer.transform([message])
|
| 16 |
+
|
| 17 |
+
model = {
|
| 18 |
+
"Logistic Regression": log_model,
|
| 19 |
+
"Naive Bayes": nb_model,
|
| 20 |
+
"SVM": svm_model
|
| 21 |
+
}[model_name]
|
| 22 |
+
|
| 23 |
+
prediction = model.predict(X_input)[0]
|
| 24 |
+
|
| 25 |
+
return "🟢 Ham" if prediction == 0 else "🔴 Spam"
|
| 26 |
|
| 27 |
+
# Create Gradio Interface
|
| 28 |
+
app = gr.Interface(
|
| 29 |
+
fn=predict_spam,
|
| 30 |
+
inputs=[
|
| 31 |
+
gr.Textbox(label="Enter your message"),
|
| 32 |
+
gr.Radio(["Logistic Regression", "Naive Bayes", "SVM"], label="Choose a Model")
|
| 33 |
+
],
|
| 34 |
+
outputs="text",
|
| 35 |
+
title="📧 Spam Message Detector",
|
| 36 |
+
description="Classify text messages as spam or ham using ML models"
|
| 37 |
+
)
|
| 38 |
|
| 39 |
+
# Run the app
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|