-
-
Notifications
You must be signed in to change notification settings - Fork 407
/
utils.py
55 lines (45 loc) · 1.84 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
import os
import shutil
import tempfile
from typing import Optional
import streamlit as st
from codeinterpreterapi import CodeInterpreterSession
def create_temp_folder() -> str:
"""
Creates a temp folder
"""
temp_folder = tempfile.mkdtemp()
return temp_folder
async def get_images(prompt: str, files: Optional[list] = None) -> list:
if files is None:
files = []
with st.chat_message("user"): # type: ignore
st.write(prompt)
with st.spinner():
async with CodeInterpreterSession(model="gpt-3.5-turbo") as session:
response = await session.agenerate_response(prompt, files=files)
with st.chat_message("assistant"): # type: ignore
st.write(response.content)
# Showing Results
for _file in response.files:
st.image(_file.get_image(), caption=prompt, use_column_width=True)
# Allowing the download of the results
if len(response.files) == 1:
st.download_button(
"Download Results",
response.files[0].content,
file_name=response.files[0].name,
)
else:
target_path = tempfile.mkdtemp()
for _file in response.files:
_file.save(os.path.join(target_path, _file.name))
zip_path = os.path.join(os.path.dirname(target_path), "archive")
shutil.make_archive(zip_path, "zip", target_path)
with open(zip_path + ".zip", "rb") as f:
st.download_button(
"Download Results",
f,
file_name="archive.zip",
)
return response.files