Repository navigation
Expand file tree
/
Copy pathsimplebot.py
More file actions
140 lines (108 loc) · 3.81 KB
/
Copy pathsimplebot.py
File metadata and controls
140 lines (108 loc) · 3.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
137
138
139
140
#CODIO SOLUTION BEGIN
import tkinter.scrolledtext as tks
from datetime import datetime
from tkinter import Tk, Frame, Label, Button, Text, END, WORD, NORMAL, DISABLED
import threading
from openai import OpenAI
client = OpenAI()
# Generating response (rule-based)
def get_bot_response(user_input):
response = client.chat.completions.create(
model="gpt-5-nano",
messages=[
{"role": "system", "content": "You are a helpful assistant chatbot."},
{"role": "user", "content": user_input}
]
)
return response.choices[0].message.content.strip()
def create_and_insert_user_frame(user_input):
userFrame = Frame(chatWindow, bg="#d0ffff")
Label(
userFrame,
text=user_input,
font=("Arial", 11),
bg="#d0ffff"
).grid(row=0, column=0, sticky="w", padx=5, pady=5)
Label(
userFrame,
text=datetime.now().strftime("%H:%M"),
font=("Arial", 7),
bg="#d0ffff"
).grid(row=1, column=0, sticky="w")
chatWindow.insert("end", "\n ", "tag-right")
chatWindow.window_create("end", window=userFrame)
def create_and_insert_bot_frame(bot_response):
botFrame = Frame(chatWindow, bg="#ffffd0")
Label(
botFrame,
text=bot_response,
font=("Arial", 11),
bg="#ffffd0",
wraplength=400,
justify="left"
).grid(row=0, column=0, sticky="w", padx=5, pady=5)
Label(
botFrame,
text=datetime.now().strftime("%H:%M"),
font=("Arial", 7),
bg="#ffffd0"
).grid(row=1, column=0, sticky="w")
chatWindow.insert("end", "\n ", "tag-left")
chatWindow.window_create("end", window=botFrame)
chatWindow.insert(END, "\n\n")
def send(event=None):
chatWindow.config(state=NORMAL)
user_input = userEntryBox.get("1.0", "end-2c").strip()
if not user_input:
chatWindow.config(state=DISABLED)
return
# Show the user's message immediately
create_and_insert_user_frame(user_input)
# Clear the input box right away
userEntryBox.delete("1.0", "end")
chatWindow.see("end")
# Insert a temporary typing message and remember where it starts/ends
typing_start = chatWindow.index("end-1c")
create_and_insert_bot_frame("Bot is typing...")
typing_end = chatWindow.index("end-1c")
chatWindow.config(state=DISABLED)
# Run the API call in the background so the GUI doesn't freeze
def worker():
try:
bot_response = get_bot_response(user_input)
except Exception:
bot_response = "Something went wrong while generating a response."
# Update the GUI safely from the main Tkinter thread
def update_ui():
chatWindow.config(state=NORMAL)
# Remove the temporary typing message
chatWindow.delete(typing_start, typing_end)
# Insert the real response
create_and_insert_bot_frame(bot_response)
chatWindow.config(state=DISABLED)
chatWindow.see("end")
baseWindow.after(0, update_ui)
threading.Thread(target=worker, daemon=True).start()
baseWindow = Tk()
baseWindow.title("The Simple Bot")
baseWindow.geometry("500x250")
chatWindow = tks.ScrolledText(baseWindow, font="Arial", wrap=WORD)
chatWindow.tag_configure("tag-left", justify="left")
chatWindow.tag_configure("tag-right", justify="right")
chatWindow.config(state=DISABLED)
sendButton = Button(
baseWindow,
font=("Verdana", 12, "bold"),
text="Send",
bg="#fd94b4",
activebackground="#ff467e",
fg="#ffffff",
command=send
)
baseWindow.bind("<Return>", send)
userEntryBox = Text(baseWindow, bd=1, bg="white", width=38, font="Arial")
chatWindow.place(x=1, y=1, height=200, width=500)
userEntryBox.place(x=3, y=202, height=27)
sendButton.place(x=430, y=200)
baseWindow.mainloop()
# CODIO SOLUTION END