-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspam_comments_detection_gui.py
75 lines (57 loc) · 2.22 KB
/
spam_comments_detection_gui.py
1
2
3
4
5
6
7
8
9
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
"""spam comments detection gui.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1jdHczRZhsQdGhJfxLhQJ9wOrOB-8FBMC
"""
import tkinter as tk
from tkinter import ttk
import pickle
class TextPredictionGUI:
def __init__(self, root):
self.root = root
self.root.title("Text Prediction Interface")
# Load the saved model and vectorizer
with open("vectorizer.pkl", "rb") as vec_file:
self.cv = pickle.load(vec_file)
with open("model.pkl", "rb") as model_file:
self.model = pickle.load(model_file)
# Set up the GUI
self.setup_ui()
def setup_ui(self):
# Input Label
input_label = ttk.Label(self.root, text="Enter your text:")
input_label.pack(pady=5)
# Text Entry Field
self.text_entry = ttk.Entry(self.root, width=50)
self.text_entry.pack(pady=5)
# Predict Button
predict_button = ttk.Button(self.root, text="Predict", command=self.predict_text)
predict_button.pack(pady=10)
# Clear Button
clear_button = ttk.Button(self.root, text="Clear", command=self.clear_text)
clear_button.pack(pady=5)
# Result Label
self.result_label = ttk.Label(self.root, text="Prediction will appear here.", font=("Arial", 12),
foreground="blue")
self.result_label.pack(pady=10)
def predict_text(self):
# Get the text from the input field
sample = self.text_entry.get()
if not sample.strip():
self.result_label.config(text="Please enter valid text.")
return
# Transform input text and predict
data = self.cv.transform([sample]).toarray()
prediction = self.model.predict(data)[0]
# Display the result
self.result_label.config(text=f"Prediction: {prediction}")
def clear_text(self):
# Clear the text entry and result label
self.text_entry.delete(0, tk.END)
self.result_label.config(text="Prediction will appear here.")
# Run the application
if __name__ == "__main__":
root = tk.Tk()
app = TextPredictionGUI(root)
root.mainloop()