-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (52 loc) · 2.25 KB
/
main.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
import openai
from api_key import my_key
import tkinter as tk
openai.api_key = my_key
messages = [ {"role": "system", "content": "You are a intelligent assistant."} ]
def create_multi_line_input_with_placeholder(master, placeholder):
def clear_placeholder(event):
if input_widget.get("1.0", "end-1c") == placeholder:
input_widget.delete("1.0", "end")
input_widget["fg"] = default_fg_color
def set_placeholder(event=None):
if not input_widget.get("1.0", "end-1c"):
input_widget.insert("1.0", placeholder)
input_widget["fg"] = placeholder_color
input_widget = tk.Text(master, height=3)
input_widget.placeholder = placeholder
input_widget.placeholder_color = "grey"
input_widget.default_fg_color = input_widget["fg"]
placeholder_color = "grey"
default_fg_color = input_widget["fg"]
input_widget.bind("<FocusIn>", clear_placeholder)
input_widget.bind("<FocusOut>", set_placeholder)
set_placeholder()
return input_widget
def create_app(master):
def show_answer():
message = message_input.get("1.0", "end-1c")
answer_label.insert("end", "\n\n" + "user: " + message)
clear_message()
answer_question(message)
message_label = tk.Label(master, text="Write your message:")
message_label.pack()
message_input = create_multi_line_input_with_placeholder(master, "Type your message here")
message_input.pack()
submit_button = tk.Button(master, text="Submit", command=show_answer)
submit_button.pack()
answer_label = tk.Text(root, wrap="word")
answer_label.pack(side="left", fill="both", expand=True)
scrollbar = tk.Scrollbar(root, command=answer_label.yview)
scrollbar.pack(side="right", fill="y")
answer_label.configure(yscrollcommand=scrollbar.set)
def answer_question(message):
messages.append({"role": "user", "content": message},)
chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
result = chat.choices[0].message.content
answer_label.insert("end", "\n" + "ChatGPT: " + result)
def clear_message():
last_index = message_input.index(tk.END)
message_input.delete("1.0", last_index)
root = tk.Tk()
create_app(root)
root.mainloop()