Ablepodz commited on
Commit
7d8a7af
·
verified ·
1 Parent(s): 641deba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -11
app.py CHANGED
@@ -1,9 +1,11 @@
 
 
1
  import random
2
 
 
3
  class ChatBot:
4
- def __init__(self, name="PyBot"):
5
  self.name = name
6
- self.memory = {}
7
  self.responses = {
8
  "greeting": [
9
  "Hey there! 👋",
@@ -36,17 +38,54 @@ class ChatBot:
36
  intent = self.understand(user_input)
37
  return random.choice(self.responses[intent])
38
 
39
- def chat(self):
40
- print(f"{self.name}: Hi! I'm {self.name}. Type 'quit' to end.")
41
- while True:
42
- user_input = input("You: ")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  if user_input.lower() in ["quit", "exit"]:
44
- print(f"{self.name}: It was nice chatting with you. Bye!")
45
- break
46
- reply = self.respond(user_input)
47
- print(f"{self.name}: {reply}")
 
 
 
 
 
48
 
49
 
50
  if __name__ == "__main__":
51
  bot = ChatBot("Sophia")
52
- bot.chat()
 
 
1
+ import tkinter as tk
2
+ from tkinter import scrolledtext
3
  import random
4
 
5
+
6
  class ChatBot:
7
+ def __init__(self, name="Sophia"):
8
  self.name = name
 
9
  self.responses = {
10
  "greeting": [
11
  "Hey there! 👋",
 
38
  intent = self.understand(user_input)
39
  return random.choice(self.responses[intent])
40
 
41
+
42
+ class ChatInterface:
43
+ def __init__(self, bot):
44
+ self.bot = bot
45
+
46
+ self.window = tk.Tk()
47
+ self.window.title(f"{self.bot.name} ChatBot")
48
+ self.window.geometry("500x600")
49
+
50
+ # Chat history display
51
+ self.chat_area = scrolledtext.ScrolledText(self.window, wrap=tk.WORD, state="disabled")
52
+ self.chat_area.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
53
+
54
+ # User input field
55
+ self.entry = tk.Entry(self.window, font=("Arial", 14))
56
+ self.entry.pack(padx=10, pady=10, fill=tk.X)
57
+ self.entry.bind("<Return>", self.send_message)
58
+
59
+ # Send button
60
+ self.send_button = tk.Button(self.window, text="Send", command=self.send_message)
61
+ self.send_button.pack(pady=5)
62
+
63
+ # Welcome message
64
+ self.display_message(f"{self.bot.name}: Hi! I'm {self.bot.name}. Type 'quit' to end.")
65
+
66
+ def display_message(self, message):
67
+ self.chat_area.config(state="normal")
68
+ self.chat_area.insert(tk.END, message + "\n")
69
+ self.chat_area.config(state="disabled")
70
+ self.chat_area.yview(tk.END)
71
+
72
+ def send_message(self, event=None):
73
+ user_input = self.entry.get().strip()
74
+ if user_input:
75
+ self.display_message(f"You: {user_input}")
76
  if user_input.lower() in ["quit", "exit"]:
77
+ self.display_message(f"{self.bot.name}: It was nice chatting with you. Bye!")
78
+ self.window.after(2000, self.window.destroy)
79
+ return
80
+ response = self.bot.respond(user_input)
81
+ self.display_message(f"{self.bot.name}: {response}")
82
+ self.entry.delete(0, tk.END)
83
+
84
+ def run(self):
85
+ self.window.mainloop()
86
 
87
 
88
  if __name__ == "__main__":
89
  bot = ChatBot("Sophia")
90
+ gui = ChatInterface(bot)
91
+ gui.run()