-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
194 lines (167 loc) · 5.48 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import os
import base64
import requests
from dotenv import load_dotenv
from taipy.gui import Gui, notify
import taipy.gui.builder as tgb
from PIL import Image
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
def on_init(state):
state.conv.update_content(state, "")
state.messages_dict = {}
state.messages = [
{
"role": "assistant",
"style": "assistant_message",
"content": "Hi, I'm GPT-4o! How can I help you today?",
},
]
state.gpt_messages = []
new_conv = create_conv(state)
state.conv.update_content(state, new_conv)
def create_conv(state):
messages_dict = {}
with tgb.Page() as conversation:
for i, message in enumerate(state.messages):
text = message["content"].replace("<br>", "\n").replace('"', "'")
messages_dict[f"message_{i}"] = text
tgb.text(
"{messages_dict['" + f"message_{i}" + "'] if messages_dict else ''}",
class_name=f"message_base {message['style']}",
mode="md",
)
state.messages_dict = messages_dict
return conversation
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
def query_gpt4o(state):
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_API_KEY}",
}
if state.query_image_path != "":
base64_image = encode_image(state.query_image_path)
message = {
"role": "user",
"content": [
{"type": "text", "text": f"{state.query_message}"},
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
},
],
}
else:
message = {
"role": "user",
"content": [{"type": "text", "text": f"{state.query_message}"}],
}
state.gpt_messages.append(message)
payload = {
"model": "gpt-4o",
"messages": state.gpt_messages,
"max_tokens": 300,
}
response = requests.post(
"https://api.openai.com/v1/chat/completions", headers=headers, json=payload
)
return (
response.json()
.get("choices")[0]
.get("message")
.get("content")
.replace("\n\n", "\n")
)
def send_message(state):
if state.query_image_path == "":
state.messages.append(
{
"role": "user",
"style": "user_message",
"content": state.query_message,
}
)
else:
state.messages.append(
{
"role": "user",
"style": "user_message",
"content": f"{state.query_message}\n![user_image]({state.query_image_path})",
}
)
state.conv.update_content(state, create_conv(state))
notify(state, "info", "Sending message...")
state.messages.append(
{
"role": "assistant",
"style": "assistant_message",
"content": query_gpt4o(state),
}
)
state.conv.update_content(state, create_conv(state))
state.query_message = ""
state.query_image_path = ""
def upload_image(state):
try:
global index
image = Image.open(state.query_image_path)
image.thumbnail((300, 300))
image.save(f"images/example_{index}.png")
state.query_image_path = f"images/example_{index}.png"
index = index + 1
except:
notify(
state,
"error",
f"Please make sure your image is under 1MB",
)
def reset_chat(state):
state.messages = []
state.gpt_messages = []
state.query_message = ""
state.query_image_path = ""
state.conv.update_content(state, create_conv(state))
on_init(state)
if __name__ == "__main__":
index = 0
query_image_path = ""
query_message = ""
messages = []
gpt_messages = []
messages_dict = {}
with tgb.Page() as page:
with tgb.layout(columns="300px 1"):
with tgb.part(class_name="sidebar"):
tgb.text("## Taipy x GPT-4o", mode="md")
tgb.button(
"New Conversation",
class_name="fullwidth plain",
id="reset_app_button",
on_action=reset_chat,
)
tgb.html("br")
tgb.image(
content="{query_image_path}", width="250px", class_name="image_preview"
)
with tgb.part(class_name="p1"):
tgb.part(partial="{conv}", height="600px", class_name="card card_chat")
with tgb.part("card mt1"):
tgb.input(
"{query_message}",
on_action=send_message,
change_delay=-1,
label="Write your message:",
class_name="fullwidth",
)
tgb.file_selector(
content="{query_image_path}",
on_action=upload_image,
extensions=".jpg,.jpeg,.png",
label="Upload an image",
)
tgb.text("Max file size: 1MB")
gui = Gui(page)
conv = gui.add_partial("")
gui.run(title="🤖Taipy x GPT-4o", dark_mode=False, margin="0px", debug=True)