-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatBot.py
More file actions
136 lines (113 loc) · 4.81 KB
/
chatBot.py
File metadata and controls
136 lines (113 loc) · 4.81 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import google.generativeai as genai
import tkinter
from tkinter import *
from tkinter import scrolledtext
import threading
import queue
#Initialising a queue to maintain the responses
response_queue = queue.Queue()
# Configure Google Generative AI
API_KEY = "Your API Key"
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel("gemini-2.0-flash")
chat = model.start_chat()
def send_message_async(text):
"""Send message to Gemini API in a separate thread."""
try:
response = chat.send_message(text)
response_lines = response.text.splitlines()[:5] # Limit to 5 lines
limited_response = "\n".join(response_lines)
response_queue.put(("success", limited_response))
except Exception as e:
response_queue.put(("error", str(e)))
def OnClick(event=None):
"""Handle Send button click or Ctrl+Enter."""
text = inputText.get("1.0", END).strip()
inputText.delete("1.0", END)
if not text:
return "break" # Ignore empty input
# Disable UI elements
send.configure(state=DISABLED)
inputText.configure(state=DISABLED)
displayText.configure(state=NORMAL)
displayText.insert(END, "YOU: " + text + "\n", "user")
displayText.insert(END, "BOT: Typing...\n", "bot")
displayText.see(END)
displayText.configure(state=DISABLED)
# Start API call in a separate thread
threading.Thread(target=send_message_async, args=(text,), daemon=True).start()
# Check queue for response
root.after(100, check_queue)
return "break" # Prevent default Enter behavior
def check_queue():
"""Check queue for API response and update UI."""
try:
status, result = response_queue.get_nowait()
displayText.configure(state=NORMAL)
# Remove "Typing..." message
displayText.delete("end-2l", "end-1l")
if status == "success":
displayText.insert(END, "BOT: " + result + "\n\n", "bot")
else:
displayText.insert(END, "BOT: Error: " + result + "\n\n", "bot")
displayText.see(END)
displayText.configure(state=DISABLED)
# Re-enable UI elements
send.configure(state=NORMAL)
inputText.configure(state=NORMAL)
inputText.focus_set()
except queue.Empty:
# No response yet, check again after 100ms
root.after(100, check_queue)
#Funtion to reset the chat on hitting back button
def NewChat():
displayText.configure(state=NORMAL)
displayText.delete("1.0", END)
inputText.delete("1.0", END)
page1.tkraise()
#Setting the root window
root = tkinter.Tk()
root.geometry("1200x800")
root.title("Chat Bot")
root.resizable(False, False)
#Adding multiple frames
page1 = Frame(root)
page2 = Frame(root)
page1.configure(bg="FloralWhite")
page2.configure(bg="FloralWhite")
#Showing one page at a time
for page in (page1, page2):
page.place(relx=0, rely=0, relwidth=1, relheight=1)
#Setting up first page
welcome_container = Frame(page1, bg="FloralWhite")
welcome_container.pack(expand=True)
#Packing label and button in single frame for better visuals
front = Label(welcome_container, text="Welcome User!", font="Gabriela 40 bold", bg="FloralWhite", fg="DarkMagenta")
front.pack(anchor="center", pady=10)
start = Button(welcome_container, text="Start Chat", command=lambda: page2.tkraise(), font="Gabriela 15 bold", bg="DarkMagenta", fg="white")
start.pack(anchor="center", pady=10, ipadx=20, ipady=5)
#Setting up second page
header = Frame(page2, bg="FloralWhite")
header.pack(expand=True)
#Packing label and button in single frame for better visuals
back = Button(header, text="←", command=NewChat, bg="DarkMagenta", font="Gabriela 17 bold", fg="white")
back.pack(side=LEFT,anchor="w", pady=5, expand=True)
name = Label(header, text="Your Chat Bot", font="Gabriela 35 bold", bg="FloralWhite", fg="DarkMagenta")
name.pack(side=RIGHT,anchor="center", pady=5, expand=True, ipadx=405)
#Setting two scrolled text area
displayText = scrolledtext.ScrolledText(page2, wrap=tkinter.WORD, height=15, state=tkinter.DISABLED, bg="Lavender", font="Gabriela")
displayText.pack(fill=tkinter.BOTH, expand=True, pady=10, padx=10)
displayText.tag_config("user", foreground='Indigo')
displayText.tag_config("bot", foreground='DarkRed')
input = Label(page2, text="Type here...", font="Gabriela 15 bold", bg="FloralWhite", fg="DarkMagenta")
input.pack(anchor="w", padx=10, expand=True)
inputText = scrolledtext.ScrolledText(page2, wrap=tkinter.WORD, height=2, bg="LightGoldenRodYellow", font="Gabriela")
inputText.pack(fill=tkinter.BOTH, expand=True, padx=10)
inputText.focus_set()
# Bind Ctrl+Enter to OnClick
inputText.bind("<Control-Return>", OnClick)
send = Button(page2, text="Send", command=OnClick, bg="DarkMagenta", font="Gabriela 12 bold", fg="white")
send.pack(anchor=CENTER, pady=20, ipadx=15, ipady=3, padx=20)
#Ensuring the first page opens always
page1.tkraise()
root.mainloop()