Spaces:
Runtime error
Runtime error
marco
commited on
Commit
·
fddc9f9
1
Parent(s):
b676419
Removing useless functions from file.py (from_bytes) + UI update + is now possible to download the processed files and to provide any kind of file
Browse files- codeinterpreterapi/config.py +1 -1
- codeinterpreterapi/schema/file.py +0 -4
- frontend/{demo.py → app.py} +8 -6
- frontend/utils.py +32 -2
codeinterpreterapi/config.py
CHANGED
|
@@ -14,7 +14,7 @@ class CodeInterpreterAPISettings(BaseSettings):
|
|
| 14 |
VERBOSE: bool = False
|
| 15 |
|
| 16 |
CODEBOX_API_KEY: Optional[str] = None
|
| 17 |
-
OPENAI_API_KEY: Optional[str] =
|
| 18 |
|
| 19 |
|
| 20 |
settings = CodeInterpreterAPISettings()
|
|
|
|
| 14 |
VERBOSE: bool = False
|
| 15 |
|
| 16 |
CODEBOX_API_KEY: Optional[str] = None
|
| 17 |
+
OPENAI_API_KEY: Optional[str] = "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"
|
| 18 |
|
| 19 |
|
| 20 |
settings = CodeInterpreterAPISettings()
|
codeinterpreterapi/schema/file.py
CHANGED
|
@@ -6,10 +6,6 @@ class File(BaseModel):
|
|
| 6 |
name: str
|
| 7 |
content: bytes
|
| 8 |
|
| 9 |
-
@classmethod
|
| 10 |
-
def from_bytes(cls, name: str, content: bytes):
|
| 11 |
-
return cls(name=name, content=content)
|
| 12 |
-
|
| 13 |
@classmethod
|
| 14 |
def from_path(cls, path: str):
|
| 15 |
with open(path, "rb") as f:
|
|
|
|
| 6 |
name: str
|
| 7 |
content: bytes
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
@classmethod
|
| 10 |
def from_path(cls, path: str):
|
| 11 |
with open(path, "rb") as f:
|
frontend/{demo.py → app.py}
RENAMED
|
@@ -5,6 +5,9 @@ import streamlit as st
|
|
| 5 |
from codeinterpreterapi import File
|
| 6 |
from frontend.utils import get_images
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
st.title('Code Interpreter API 🚀')
|
| 9 |
|
| 10 |
# This will create a sidebar
|
|
@@ -14,18 +17,17 @@ st.sidebar.markdown("[Github Repo](https://github.com/shroominic/codeinterpreter
|
|
| 14 |
|
| 15 |
# This will create a textbox where you can input text
|
| 16 |
input_text = st.text_area("Write your prompt")
|
| 17 |
-
uploaded_files = st.file_uploader("
|
| 18 |
-
type=".csv")
|
| 19 |
|
| 20 |
uploaded_files_list = []
|
| 21 |
for uploaded_file in uploaded_files:
|
| 22 |
bytes_data = uploaded_file.read()
|
| 23 |
-
uploaded_files_list.append(File
|
| 24 |
-
|
| 25 |
|
| 26 |
# This will create a button
|
| 27 |
button_pressed = st.button('Run code interpreter', use_container_width=True)
|
| 28 |
|
| 29 |
-
# This will display the
|
| 30 |
if button_pressed and input_text != "":
|
| 31 |
-
asyncio.run(get_images(input_text, files=uploaded_files_list))
|
|
|
|
| 5 |
from codeinterpreterapi import File
|
| 6 |
from frontend.utils import get_images
|
| 7 |
|
| 8 |
+
# Page configuration
|
| 9 |
+
st.set_page_config(layout="wide")
|
| 10 |
+
|
| 11 |
st.title('Code Interpreter API 🚀')
|
| 12 |
|
| 13 |
# This will create a sidebar
|
|
|
|
| 17 |
|
| 18 |
# This will create a textbox where you can input text
|
| 19 |
input_text = st.text_area("Write your prompt")
|
| 20 |
+
uploaded_files = st.file_uploader("Upload your files", accept_multiple_files=True)
|
|
|
|
| 21 |
|
| 22 |
uploaded_files_list = []
|
| 23 |
for uploaded_file in uploaded_files:
|
| 24 |
bytes_data = uploaded_file.read()
|
| 25 |
+
uploaded_files_list.append(File(name=uploaded_file.name,
|
| 26 |
+
content=bytes_data))
|
| 27 |
|
| 28 |
# This will create a button
|
| 29 |
button_pressed = st.button('Run code interpreter', use_container_width=True)
|
| 30 |
|
| 31 |
+
# This will display the images only when the button is pressed
|
| 32 |
if button_pressed and input_text != "":
|
| 33 |
+
asyncio.run(get_images(input_text, files=uploaded_files_list))
|
frontend/utils.py
CHANGED
|
@@ -1,5 +1,16 @@
|
|
| 1 |
from codeinterpreterapi import CodeInterpreterSession
|
| 2 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
async def get_images(prompt: str, files: list = None):
|
|
@@ -17,5 +28,24 @@ async def get_images(prompt: str, files: list = None):
|
|
| 17 |
with st.chat_message("assistant"):
|
| 18 |
st.write(response.content)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from codeinterpreterapi import CodeInterpreterSession
|
| 2 |
import streamlit as st
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
import shutil
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def create_temp_folder() -> str:
|
| 9 |
+
"""
|
| 10 |
+
Creates a temp folder
|
| 11 |
+
"""
|
| 12 |
+
temp_folder = tempfile.mkdtemp()
|
| 13 |
+
return temp_folder
|
| 14 |
|
| 15 |
|
| 16 |
async def get_images(prompt: str, files: list = None):
|
|
|
|
| 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('Download Results', response.files[0].content,
|
| 38 |
+
file_name=response.files[0].name,
|
| 39 |
+
use_container_width=True)
|
| 40 |
+
else:
|
| 41 |
+
target_path = tempfile.mkdtemp()
|
| 42 |
+
for _file in response.files:
|
| 43 |
+
_file.save(os.path.join(target_path, _file.name))
|
| 44 |
+
|
| 45 |
+
zip_path = os.path.join(os.path.dirname(target_path), "archive")
|
| 46 |
+
shutil.make_archive(zip_path, 'zip', target_path)
|
| 47 |
+
|
| 48 |
+
with open(zip_path + ".zip", 'rb') as f:
|
| 49 |
+
st.download_button('Download Results', f,
|
| 50 |
+
file_name="archive.zip",
|
| 51 |
+
use_container_width=True)
|