-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path57 - Build your own Chatbot with Python.py
More file actions
106 lines (93 loc) · 2.9 KB
/
57 - Build your own Chatbot with Python.py
File metadata and controls
106 lines (93 loc) · 2.9 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import tkinter as tk
from tkinter import scrolledtext
from nltk.chat.util import Chat, reflections
pairs = [
[
r"(.*)my name is (.*)",
["Hello %2! How are you today?"]
],
[
r"(.*)(help|support)(.*)",
["I can help you 🙂 Tell me your problem."]
],
[
r"(.*)(your name|who are you)(.*)",
["I'm a Python GUI chatbot 🤖"]
],
[
r"(how are you|how r you)(.*)",
["I'm doing great!", "All good here 😄"]
],
[
r"sorry(.*)",
["No worries 👍", "It's okay 😊"]
],
[
r"(i am|i'm)(.*)(good|fine|okay|well)",
["Nice to hear that 😄", "That's great!"]
],
[
r"(hi|hello|hey|hola)(.*)",
["Hello 👋", "Hey there 🙂"]
],
[
r"(.*)(sports|game)(.*)",
["I like Cricket 🏏"]
],
[
r"(quit|exit|bye)",
["Bye! See you soon 👋"]
],
[
r"(.*)",
["Interesting 🤔 Tell me more.", "I see 👀"]
],
]
chatbot = Chat(pairs, reflections)
class ChatbotGUI:
def __init__(self, root):
self.root = root
self.root.title("ChatBot")
self.root.geometry("450x500")
self.root.configure(bg= "#0f172a")
self.chat_area = scrolledtext.ScrolledText(
root,
wrap = tk.WORD,
bg = "#020617",
fg = "white",
font =("Consoals",11)
)
self.chat_area.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
self.chat_area.insert(tk.END, "🤖 Bot: Hello! Type 'quit' to exit.\n")
self.chat_area.config(state=tk.DISABLED)
self.entry = tk.Entry(
root,
font=("Consolas", 12)
)
self.entry.pack(fill=tk.X, padx=10, pady=5)
self.entry.bind("<Return>", self.send_message)
self.send_btn = tk.Button(
root,
text="Send",
command= self.send_message,
bg = "#38bdf8",
fg = "black"
)
self.send_btn.pack(pady=5)
def send_message(self, event = None):
user_input = self.entry.get().strip()
if not user_input:
return
self.chat_area.config(state=tk.NORMAL)
self.chat_area.insert(tk.END, f"🧑 You: {user_input}\n")
response = chatbot.respond(user_input.lower())
self.chat_area.insert(tk.END, f"🤖 Bot: {response}\n\n")
self.chat_area.config(state=tk.DISABLED)
self.chat_area.see(tk.END)
self.entry.delete(0, tk.END)
if user_input.lower() in ["quit", "bye", "exit"]:
self.root.after(100, self.root.destroy)
if __name__ =="__main__":
root = tk.Tk()
ChatbotGUI(root)
root.mainloop()