hoollyzhang commited on
Commit
7fdd0ed
·
1 Parent(s): 2b9927f

feat:Add application file

Browse files
Files changed (1) hide show
  1. frontend/utils.py +50 -37
frontend/utils.py CHANGED
@@ -1,38 +1,51 @@
1
- import asyncio
2
-
3
  import streamlit as st
4
-
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
14
- st.sidebar.title("Code Interpreter API 🚀")
15
-
16
- st.sidebar.markdown("### 给勇哥哥打call 🚀")
17
-
18
- st.sidebar.markdown(
19
- "![Code Interpreter](https://vercel.brzhang.club/_next/image?url=%2Fcoffer.jpg&w=640&q=75)")
20
-
21
-
22
- # This will create a textbox where you can input text
23
- input_text = st.text_area("Write your prompt")
24
- uploaded_files = st.file_uploader(
25
- "Upload your files", accept_multiple_files=True)
26
-
27
- uploaded_files_list = []
28
- for uploaded_file in uploaded_files:
29
- bytes_data = uploaded_file.read()
30
- uploaded_files_list.append(File(name=uploaded_file.name,
31
- content=bytes_data))
32
-
33
- # This will create a button
34
- button_pressed = st.button('Run code interpreter', use_container_width=True)
35
-
36
- # This will display the images only when the button is pressed
37
- if button_pressed and input_text != "":
38
- asyncio.run(get_images(input_text, files=uploaded_files_list))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
17
+ if files is None:
18
+ files = []
19
+ with st.chat_message("user"):
20
+ st.write(prompt)
21
+ with st.spinner():
22
+ async with CodeInterpreterSession(model='gpt-3.5-turbo') as session:
23
+ response = await session.generate_response(
24
+ prompt,
25
+ files=files
26
+ )
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('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)