-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
149 lines (121 loc) · 4.16 KB
/
gui.py
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
141
142
143
144
145
146
147
148
149
import tkinter as tk
from tkinter import Scrollbar, Text, Button, ttk, PhotoImage
import argparse
import atexit
import yaml
from rich.console import Console
from language_model import LLM
from utils import suppress_stdout_stderr, verify_download_model
THEME_COLOR = "#02537f"
def chat(input_text):
# Replace this with your chatbot logic
gen = llm.reply(input_text)
return gen
# Function to update the chat history with the response token by token
def update_chat_history(input_text):
chat_history_text.config(state=tk.NORMAL) # Allow editing the text widget
chat_history_len = len(chat_history_text.get("1.0", tk.END))
if chat_history_len > 1:
chat_history_text.insert(tk.END, "\nYou: " + input_text.strip())
else:
chat_history_text.insert(tk.END, "You: " + input_text.strip())
chat_history_text.insert(tk.END, "\nChatbot: ")
chat_history_text.config(state=tk.DISABLED) # Disable text editing
root.update()
for token in chat(input_text):
chat_history_text.config(state=tk.NORMAL) # Allow editing the text
chat_history_text.insert(tk.END, token)
chat_history_text.see(tk.END) # Scroll to the end of the text widget
chat_history_text.config(state=tk.DISABLED) # Disable text editing
root.update()
chat_history_text.config(state=tk.DISABLED)
def handle_keypress(event):
# Detect Shift + Enter to add a new line
if (
event.keysym == "Return" and event.state == 1
): # Shift key has state 1 when pressed
user_input_text.insert(tk.END, "")
# Function to handle sending a message
def send_message(*args):
user_input = user_input_text.get(
"1.0", "end-1c"
) # Get user input from the Text widget
if user_input.strip():
user_input_text.delete("0.0", tk.END) # Clear the user input field
update_chat_history(user_input)
def close_window():
root.quit()
# Create the main application window
root = tk.Tk()
root.title("Chatbot")
img = PhotoImage(file="icon.ppm")
root.iconphoto(False, img)
# Create a style for themed widgets (using the 'clam' theme)
style = ttk.Style(root)
style.theme_use("clam")
# Set the background color of the root window to purple
root.configure(bg=THEME_COLOR)
# Create a Frame for chat history
frame = ttk.Frame(root)
frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Create a PanedWindow for chat history and user input
paned_window = ttk.PanedWindow(frame, orient=tk.VERTICAL)
paned_window.pack(fill=tk.BOTH, expand=True)
# Create a Text widget to display the chat history
chat_history_text = Text(
paned_window,
wrap=tk.WORD,
state=tk.DISABLED,
font=("Helvetica", 12),
height=5,
)
scrollbar = Scrollbar(chat_history_text, command=chat_history_text.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
chat_history_text.config(yscrollcommand=scrollbar.set)
# Create a Text widget for user input
user_input_text = Text(
paned_window,
wrap=tk.WORD,
font=("Helvetica", 12),
height=1,
)
scrollbar = Scrollbar(
user_input_text,
command=user_input_text.yview,
)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
user_input_text.config(yscrollcommand=scrollbar.set)
user_input_text.bind("<Return>", send_message)
user_input_text.bind("<Shift-Return>", handle_keypress) # Bind Shift + Enter
# Add the widgets to the PanedWindow
paned_window.add(chat_history_text, weight=10)
paned_window.add(user_input_text, weight=1)
# user_input_text.place(x=0, y=0, relwidth=1.0, relheight=0.1)
# Create a Send button to send user input
send_button = Button(
frame,
text="Send",
command=send_message,
bg=THEME_COLOR,
fg="#ffffff",
font=("Helvetica", 12),
)
send_button.pack(pady=10)
root.geometry("600x400")
parser = argparse.ArgumentParser()
parser.add_argument(
"--config",
default="./config/config.yaml",
help="Config file path",
)
console = Console()
args = parser.parse_args()
with open(args.config) as f:
conf = yaml.safe_load(f)
verify_download_model(conf["model_path"], conf["model_url"])
with suppress_stdout_stderr():
llm = LLM(conf, console)
root.protocol("WM_DELETE_WINDOW", close_window)
atexit.register(close_window)
# Start the GUI application
root.mainloop()