-
Notifications
You must be signed in to change notification settings - Fork 29
/
utils.py
444 lines (381 loc) · 16.5 KB
/
utils.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
"""
utils.py
"""
import os
import base64
import hmac
import re
from PIL import ImageFile
from typing import Tuple
from typing_extensions import override
import streamlit as st
from openai import (
OpenAI,
AssistantEventHandler
)
from openai.types.beta.threads import Text, TextDelta
from openai.types.beta.threads.runs import ToolCall, ToolCallDelta
# Get secrets
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY", st.secrets["OPENAI_API_KEY"])
# Config
LAST_UPDATE_DATE = "2024-04-08"
# Initialise the OpenAI client
client = OpenAI(api_key=OPENAI_API_KEY)
def render_custom_css() -> None:
"""
Applies custom CSS
"""
st.html("""
<style>
#MainMenu {visibility: hidden}
#header {visibility: hidden}
#footer {visibility: hidden}
.block-container {
padding-top: 3rem;
padding-bottom: 2rem;
padding-left: 3rem;
padding-right: 3rem;
}
</style>
""")
def initialise_session_state():
"""
Initialise session state variables
"""
if "file" not in st.session_state:
st.session_state.file = None
if "assistant_text" not in st.session_state:
st.session_state.assistant_text = [""]
for session_state_var in ["file_uploaded", "read_terms"]:
if session_state_var not in st.session_state:
st.session_state[session_state_var] = False
for session_state_var in ["code_input", "code_output"]:
if session_state_var not in st.session_state:
st.session_state[session_state_var] = []
def moderation_endpoint(text) -> bool:
"""
Checks if the text is triggers the moderation endpoint
Args:
- text (str): The text to check
Returns:
- bool: True if the text is flagged
"""
response = client.moderations.create(input=text)
return response.results[0].flagged
def is_nsfw(text) -> bool:
"""
Checks if the text is nsfw.
Args:
- text (str): The text to check
Returns:
- bool: True if the text is nsfw
"""
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "Is the given text NSFW? If yes, return `1``, else return `0`."},
{"role": "user", "content": text},
],
max_tokens=1,
logit_bias={"15": 100,
"16": 100},
)
output = response.choices[0].message.content
return bool(output)
def is_not_question(text) -> bool:
"""
Checks if the text is not a question
Args:
- text (str): The text to check
Returns:
- bool: True if the text is not a question
"""
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "Is the given text a question? If yes, return `1``, else return `0`."},
{"role": "user", "content": text},
],
max_tokens=1,
logit_bias={"15": 100,
"16": 100},
)
output = response.choices[0].message.content
return bool(output)
def delete_files(file_id_list: list[str]) -> None:
"""
Delete the file(s) uploaded
Args:
- file_id_list (list[str]): List of file ids to delete
"""
for file_id in file_id_list:
client.files.delete(file_id)
print(f"Deleted file: \t {file_id}")
def delete_thread(thread_id) -> None:
"""
Delete the thread
Args:
- thread_id (str): The id of the thread to delete
"""
client.beta.threads.delete(thread_id)
print(f"Deleted thread: \t {thread_id}")
def remove_links(text: str) -> str:
"""
Remove links from the text
Args:
- text (str): The text to remove markdown links from
Returns:
- str: The text with the links removed
"""
# Pattern to match Markdown links: [Link text](URL)
link_pattern = r'\[.*?\]\(.*?\)'
# Pattern to match lines starting with list item indicators (unordered or ordered)
list_item_pattern = r'^(\s*(\*|-|\d+\.)\s).*'
# Combine both patterns to identify list items containing links
combined_pattern = rf'({list_item_pattern}.*?{link_pattern}.*?$)|{link_pattern}'
# Replace the matching content with an empty string
# The MULTILINE flag is used to allow ^ to match the start of each line
cleaned_text = re.sub(combined_pattern, '', text, flags=re.MULTILINE)
return cleaned_text
def retrieve_messages_from_thread(thread_id: str) -> list[str]:
"""
Retrieve messages from the thread
Args:
- thread_id (str): The id of the thread
Returns:
- list[str]: List of assistant messages
"""
thread_messages = client.beta.threads.messages.list(thread_id)
assistant_messages = []
for message in thread_messages.data:
if message.role == "assistant":
assistant_messages.append(message.id)
return assistant_messages
def retrieve_assistant_created_files(message_list: list[str]) -> list[str]:
"""
Retrieve the assistant-created files
Args:
- message_list (list[str]): List of assistant messages
Returns:
- list[str]: List of assistant-created file ids
"""
assistant_created_file_ids = []
for message_id in message_list:
message = client.beta.threads.messages.retrieve(
message_id=message_id,
thread_id=st.session_state.thread_id,
)
# Retrieve the attachments from the message, and the file ids from the attachments
created_file_id = [file.file_id for file in message.attachments]
for file_id in created_file_id:
assistant_created_file_ids.append(file_id)
# message_files = client.beta.threads.messages.files.list(
# thread_id=st.session_state.thread_id,
# message_id=message_id)
# for file in message_files.data:
# assistant_created_file_ids.append(file.id)
return assistant_created_file_ids
@st.experimental_fragment
def render_download_files(file_id_list: list[str]) -> Tuple[list[bytes], list[str]]:
"""
Download the files, renders a download button for each file, and returns the downloaded files
Args:
- file_id_list (list[str]): List of file ids to download
Returns:
- downloaded_files (list[object]): List of downloaded files
- file_names (list[str]): List of file names
"""
downloaded_files = []
file_names = []
if len(file_id_list) > 0:
st.markdown("### 📂 **Downloadable Files**")
for file_id_num, file_id in enumerate(file_id_list):
try:
file_data = client.files.content(file_id)
file = file_data.read()
file_name = client.files.retrieve(file_id).filename
file_name = os.path.basename(file_name)
# # if file_name is `.csv`
# if file_name.endswith(".csv"):
# try:
# with open(f"static/{file_name}", "wb") as f:
# f.write(file)
# except:
# # Convert bytes to string
# csv_str = file.decode('utf-8')
# csv_file = io.StringIO(csv_str)
# df = pd.read_csv(csv_file)
# df.to_csv(f"static/{file_name}", index=False)
# elif file_name.endswith(".png"):
# with open(f"static/{file_name}", "wb") as f:
# f.write(file)
# st.write(f"- [{file_name}](static/{file_name})")
# Store the downloaded file and its name
downloaded_files.append(file)
file_names.append(file_name)
# Display the download button
st.download_button(label=f"{file_name}",
data=file,
file_name=file_name,
mime="text/csv")
except:
# Display the download button
file = st.session_state.download_files[file_id_num]
file_name = st.session_state.download_file_names[file_id_num]
st.download_button(label=f"{file_name}",
data=file,
file_name=file_name,
mime="text/csv")
return downloaded_files, file_names
class EventHandler(AssistantEventHandler):
"""
Event handler for the assistant stream
"""
@override
def on_text_created(self, text: Text) -> None:
"""
Handler for when a text is created
"""
# This try-except block will update the earlier expander for code to complete.
# Note the indexing. We are updating the x-1 textbox where x is the current textbox.
# Note how `on_tool_call_done` creates a new textbook (which is the x_th textbox, so we want to access the x-1_th)
# This is to address an edge case where code is executed, but there is no output textbox (e.g. a graph is created)
try:
st.session_state[f"code_expander_{len(st.session_state.text_boxes) - 1}"].update(state="complete", expanded=False)
except KeyError:
pass
# Create a new text box
st.session_state.text_boxes.append(st.empty())
# Insert the text into the last element in assistant text list
st.session_state.assistant_text[-1] += "**> 🕵️ DAVE:** \n\n "
# Remove links from the text
st.session_state.assistant_text[-1] = remove_links(st.session_state.assistant_text[-1])
# Display the text in the newly created text box
st.session_state.text_boxes[-1].info("".join(st.session_state["assistant_text"][-1]))
@override
def on_text_delta(self, delta: TextDelta, snapshot: Text):
"""
Handler for when a text delta is created
"""
# Clear the latest text box
st.session_state.text_boxes[-1].empty()
# If there is text written, add it to latest element in the assistant text list
if delta.value:
st.session_state.assistant_text[-1] += delta.value
# Remove links from the text
st.session_state.assistant_text[-1] = remove_links(st.session_state.assistant_text[-1])
# Re-display the full text in the latest text box
st.session_state.text_boxes[-1].info("".join(st.session_state["assistant_text"][-1]))
def on_text_done(self, text: Text):
"""
Handler for when text is done
"""
# Create new text box and element in the assistant text list
st.session_state.text_boxes.append(st.empty())
st.session_state.assistant_text.append("")
def on_tool_call_created(self, tool_call: ToolCall):
"""
Handler for when a tool call is created
"""
# Create new text box, which will contain code
st.session_state.text_boxes.append(st.empty())
# Create a new element in the code input list
st.session_state.code_input.append("")
def on_tool_call_delta(self, delta: ToolCallDelta, snapshot: ToolCallDelta):
"""
Handler for when a tool call delta is created
"""
if delta.type == "code_interpreter" and delta.code_interpreter:
# Code writen by the assistant to be executed
if delta.code_interpreter.input:
# Go to the last text box
with st.session_state.text_boxes[-1]:
# Check if a code box for this accompanying text box index exists
if f"code_box_{len(st.session_state.text_boxes)}" not in st.session_state:
# Nest the code in an expander
st.session_state[f"code_expander_{len(st.session_state.text_boxes)}"] = st.status("**💻 Code**", expanded=True)
# Create an empty container which is the placeholder for the code box
st.session_state[f"code_box_{len(st.session_state.text_boxes)}"] = st.session_state[f"code_expander_{len(st.session_state.text_boxes)}"].empty()
# Clear the code box
st.session_state[f"code_box_{len(st.session_state.text_boxes)}"].empty()
# If there is code written, add it to the code input
if delta.code_interpreter.input:
st.session_state.code_input[-1] += delta.code_interpreter.input
# Re-display the full code in the code box
st.session_state[f"code_box_{len(st.session_state.text_boxes)}"].code(st.session_state.code_input[-1])
# Output from the code executed by code interpreter
if delta.code_interpreter.outputs:
for output in delta.code_interpreter.outputs:
if output.type == "logs":
# This try-except block will update the earlier expander for code to complete.
# Note the indexing, as we have not yet created a new text box for the code output.
try:
st.session_state[f"code_expander_{len(st.session_state.text_boxes)}"].update(state="complete", expanded=False)
except KeyError:
pass
# Create a new element in the code input list, which is for the next code input
st.session_state.code_input.append("")
# Create a new text box, which is for the code output
st.session_state.text_boxes.append(st.empty())
# Nest the code output in an expander
st.session_state.text_boxes[-1] = st.expander(label="**🔎 Output**")
# Create a new element in the code output list
st.session_state.code_output.append("")
# Clear the latest text box which is for the code output
st.session_state.text_boxes[-1].empty()
# Add the logs to the code output
st.session_state.code_output[-1] += f"\n\n{output.logs}"
# Display the code output
st.session_state.text_boxes[-1].code(st.session_state.code_output[-1])
def on_tool_call_done(self, tool_call: ToolCall):
"""
Handler for when a tool call is done
"""
# Create a new element in the code input list
st.session_state.code_input.append("")
# Create a new element in the code output list
st.session_state.code_output.append("")
# Create a new element in the assistant text list
st.session_state.assistant_text.append("")
# Create a new text box for the next operation
st.session_state.text_boxes.append(st.empty())
def on_image_file_done(self, image_file: ImageFile):
"""
Handler for when an image file is done
"""
# Download file from OpenAI
image_data = client.files.content(image_file.file_id)
img_name = image_file.file_id
# Save file
image_data_bytes = image_data.read()
with open(f"images/{img_name}.png", "wb") as file:
file.write(image_data_bytes)
# Open file and encode as data
file_ = open(f"images/{img_name}.png", "rb")
contents = file_.read()
data_url = base64.b64encode(contents).decode("utf-8")
file_.close()
# Create new text box
st.session_state.text_boxes.append(st.empty())
st.session_state.assistant_text.append("")
# # Display image in textbox
image_html = f'<p align="center"><img src="data:image/png;base64,{data_url}" width=600></p>'
st.session_state.text_boxes[-1].html(image_html)
# st.session_state.text_boxes[-1].image(f"images/{img_name}.png", width=600)
# Create new text box
st.session_state.assistant_text.append("")
st.session_state.text_boxes.append(st.empty())
# Delete file from OpenAI
client.files.delete(image_file.file_id)
def on_timeout(self):
"""
Handler for when the api call times out
"""
st.error("The api call timed out.")
st.stop()
# def on_exception(self, exception: Exception):
# """
# Handler for when an exception occurs
# """
# st.error(f"An error occurred: {exception}")
# st.stop()