Vivekkrishu commited on
Commit
08313ff
·
1 Parent(s): b84ccee
Files changed (1) hide show
  1. src/gui.py +40 -79
src/gui.py CHANGED
@@ -1,82 +1,43 @@
1
- import tkinter as tk
2
- from tkinter import Canvas, Frame, Scrollbar, Label, Entry, BOTH, RIGHT, LEFT, Y, NW
3
- from predict import chatbot_response
4
-
5
- class ChatbotGUI:
6
- def __init__(self, root):
7
- self.root = root
8
- self.root.title("LMS Chatbot")
9
- self.root.geometry("500x600")
10
- self.root.minsize(400, 400)
11
- self.root.configure(bg="#f0f0f0")
12
-
13
- # Chat frame with canvas for scrolling
14
- self.frame = Frame(root, bg="#f0f0f0")
15
- self.frame.pack(padx=10, pady=10, fill=BOTH, expand=True)
16
-
17
- self.canvas = Canvas(self.frame, bg="#f0f0f0", highlightthickness=0)
18
- self.scrollbar = Scrollbar(self.frame, orient="vertical", command=self.canvas.yview)
19
- self.scrollable_frame = Frame(self.canvas, bg="#f0f0f0")
20
-
21
- self.scrollable_frame.bind(
22
- "<Configure>",
23
- lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all"))
24
- )
25
-
26
- self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor=NW)
27
- self.canvas.configure(yscrollcommand=self.scrollbar.set)
28
-
29
- self.canvas.pack(side=LEFT, fill=BOTH, expand=True)
30
- self.scrollbar.pack(side=RIGHT, fill=Y)
31
-
32
- # Entry box
33
- self.entry = Entry(root, font=("Helvetica", 14), bd=2, relief="groove")
34
- self.entry.pack(padx=10, pady=10, fill="x")
35
- self.entry.bind("<Return>", self.send_message)
36
-
37
- # Typing indicator
38
- self.typing_label = Label(root, text="", bg="#f0f0f0", fg="gray", font=("Helvetica", 10))
39
- self.typing_label.pack(pady=(0,5))
40
-
41
- def send_message(self, event):
42
- user_input = self.entry.get().strip()
43
- if not user_input:
44
- return
45
- self.entry.delete(0, tk.END)
46
- self.add_message(user_input, sender="user")
47
- self.typing_label.config(text="Bot is typing...")
48
- self.root.after(500, lambda: self.bot_reply(user_input))
49
-
50
- def bot_reply(self, user_input):
51
- response = chatbot_response(user_input)
52
- self.typing_label.config(text="")
53
- self.animate_bot_response(response)
54
-
55
- def add_message(self, message, sender="bot"):
56
- bubble = Label(self.scrollable_frame, text=message, wraplength=300, justify="left",
57
- bg="#DCF8C6" if sender=="user" else "#FFFFFF",
58
- fg="#000000", font=("Helvetica", 12), padx=10, pady=5, bd=1, relief="solid")
59
- bubble.pack(anchor="e" if sender=="user" else "w", pady=5, padx=5)
60
- self.canvas.update_idletasks()
61
- self.canvas.yview_moveto(1.0)
62
-
63
- def animate_bot_response(self, text, idx=0, message=""):
64
- if idx < len(text):
65
- message += text[idx]
66
- if hasattr(self, "bot_bubble"):
67
- self.bot_bubble.config(text=message)
68
- else:
69
- self.bot_bubble = Label(self.scrollable_frame, text=message, wraplength=300, justify="left",
70
- bg="#FFFFFF", fg="#000000", font=("Helvetica", 12), padx=10, pady=5, bd=1, relief="solid")
71
- self.bot_bubble.pack(anchor="w", pady=5, padx=5)
72
- idx += 1
73
- self.root.after(30, self.animate_bot_response, text, idx, message)
74
  else:
75
- del self.bot_bubble
76
- self.canvas.update_idletasks()
77
- self.canvas.yview_moveto(1.0)
 
 
 
 
 
 
 
 
78
 
79
  if __name__ == "__main__":
80
- root = tk.Tk()
81
- app = ChatbotGUI(root)
82
- root.mainloop()
 
1
+ # app.py
2
+ import gradio as gr
3
+ import joblib
4
+ from src.preprocess import clean_text
5
+ import time
6
+
7
+ # Load model and responses
8
+ model = joblib.load("models/lms_chatbot.joblib")
9
+ responses = joblib.load("models/responses.joblib")
10
+
11
+ # Keep chat history
12
+ history = []
13
+
14
+ def chatbot_response(user_input):
15
+ """Simulate Tkinter-style chat UI with bubbles."""
16
+ clean_input = clean_text(user_input)
17
+ tag = model.predict([clean_input])[0]
18
+ bot_reply = responses.get(tag, ["Sorry, I don't understand."])[0]
19
+
20
+ # Add user and bot messages to history
21
+ history.append(("You", user_input))
22
+ history.append(("Bot", bot_reply))
23
+
24
+ # Format messages like chat bubbles
25
+ formatted_history = ""
26
+ for sender, msg in history:
27
+ if sender == "You":
28
+ formatted_history += f"<div style='background-color:#DCF8C6; padding:8px; border-radius:10px; margin:4px 0; width:fit-content; float:right; clear:both'>{msg}</div>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  else:
30
+ formatted_history += f"<div style='background-color:#FFFFFF; padding:8px; border-radius:10px; margin:4px 0; width:fit-content; float:left; clear:both'>{msg}</div>"
31
+ return formatted_history
32
+
33
+ # Gradio interface
34
+ demo = gr.Interface(
35
+ fn=chatbot_response,
36
+ inputs=gr.Textbox(lines=2, placeholder="Type your message here..."),
37
+ outputs=gr.HTML(),
38
+ title="LMS Chatbot",
39
+ description="Ask anything about LMS and get automated responses."
40
+ )
41
 
42
  if __name__ == "__main__":
43
+ demo.launch()