Trashika112 commited on
Commit
234529f
·
verified ·
1 Parent(s): 71e6a71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -20
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
- models = {
8
- "Logistic Regression": joblib.load("logistic_model.pkl"),
9
- "Naive Bayes": joblib.load("naive_bayes_model.pkl"),
10
- "SVM": joblib.load("svm_model.pkl")
11
- }
12
 
13
- st.title("📧 Spam Detection App")
14
- st.write("Choose a model and enter a message to check if it's spam or ham.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- model_choice = st.selectbox("Select Model", list(models.keys()))
17
- message = st.text_area("Enter your message", "")
 
 
 
 
 
 
 
 
 
18
 
19
- if st.button("Predict"):
20
- if message.strip() == "":
21
- st.warning("Please enter a message.")
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()