Spaces:
Runtime error
Runtime error
Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import shutil
|
| 3 |
+
import tempfile
|
| 4 |
+
from typing import Optional
|
| 5 |
+
|
| 6 |
+
import streamlit as st # type: ignore
|
| 7 |
+
|
| 8 |
+
from codeinterpreterapi import CodeInterpreterSession
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def create_temp_folder() -> str:
|
| 12 |
+
"""
|
| 13 |
+
Creates a temp folder
|
| 14 |
+
"""
|
| 15 |
+
temp_folder = tempfile.mkdtemp()
|
| 16 |
+
return temp_folder
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
async def get_images(prompt: str, files: Optional[list] = None):
|
| 20 |
+
if files is None:
|
| 21 |
+
files = []
|
| 22 |
+
with st.chat_message("user"):
|
| 23 |
+
st.write(prompt)
|
| 24 |
+
with st.spinner():
|
| 25 |
+
async with CodeInterpreterSession(model="gpt-3.5-turbo") as session:
|
| 26 |
+
response = await session.generate_response(prompt, files=files)
|
| 27 |
+
|
| 28 |
+
with st.chat_message("assistant"):
|
| 29 |
+
st.write(response.content)
|
| 30 |
+
|
| 31 |
+
# Showing Results
|
| 32 |
+
for _file in response.files:
|
| 33 |
+
st.image(_file.get_image(), caption=prompt, use_column_width=True)
|
| 34 |
+
|
| 35 |
+
# Allowing the download of the results
|
| 36 |
+
if len(response.files) == 1:
|
| 37 |
+
st.download_button(
|
| 38 |
+
"Download Results",
|
| 39 |
+
response.files[0].content,
|
| 40 |
+
file_name=response.files[0].name,
|
| 41 |
+
use_container_width=True,
|
| 42 |
+
)
|
| 43 |
+
else:
|
| 44 |
+
target_path = tempfile.mkdtemp()
|
| 45 |
+
for _file in response.files:
|
| 46 |
+
_file.save(os.path.join(target_path, _file.name))
|
| 47 |
+
|
| 48 |
+
zip_path = os.path.join(os.path.dirname(target_path), "archive")
|
| 49 |
+
shutil.make_archive(zip_path, "zip", target_path)
|
| 50 |
+
|
| 51 |
+
with open(zip_path + ".zip", "rb") as f:
|
| 52 |
+
st.download_button(
|
| 53 |
+
"Download Results",
|
| 54 |
+
f,
|
| 55 |
+
file_name="archive.zip",
|
| 56 |
+
use_container_width=True,
|
| 57 |
+
)
|