Meryb commited on
Commit
c3db3b9
·
verified ·
1 Parent(s): b48b3fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -12
app.py CHANGED
@@ -5,9 +5,9 @@ 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']
@@ -29,13 +29,37 @@ model.fit(X_train_features, y_train)
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  from sklearn.naive_bayes import MultinomialNB
6
 
7
  # Load and clean the dataset
8
+ data = pd.read_csv("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']
 
29
  def predict_spam(message):
30
  message_features = vectorizer.transform([message])
31
  prediction = model.predict(message_features)[0]
32
+ return prediction
33
+
34
+ # Build better UI
35
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
36
+ gr.Markdown("## 📩 Spam Detector\nEnter any message below to check if it's spam or not.")
37
+
38
+ with gr.Row():
39
+ with gr.Column(scale=3):
40
+ message_input = gr.Textbox(
41
+ label="Your Message",
42
+ placeholder="e.g. Congratulations! You've won a prize...",
43
+ lines=4
44
+ )
45
+ submit_btn = gr.Button("Detect Spam")
46
+ with gr.Column(scale=2):
47
+ result_output = gr.Label(label="Prediction")
48
+
49
+ examples = [
50
+ ["Congratulations! You have been selected for a free cruise!"],
51
+ ["Hey, what time is class tomorrow?"],
52
+ ["Win cash now!!! Click here"],
53
+ ["Lunch at 1 PM?"],
54
+ ]
55
+
56
+ gr.Examples(
57
+ examples=examples,
58
+ inputs=message_input
59
+ )
60
+
61
+ submit_btn.click(fn=predict_spam, inputs=message_input, outputs=result_output)
62
+
63
+ # Launch app
64
+ if __name__ == "__main__":
65
+ demo.launch()