hoollyzhang commited on
Commit
b4dde71
Β·
1 Parent(s): 49a48eb
Files changed (1) hide show
  1. app.py +31 -52
app.py CHANGED
@@ -1,67 +1,46 @@
1
- import streamlit as st
2
  import asyncio
3
- import tempfile
4
- import os
5
- import shutil
6
- from codeinterpreterapi import File
7
- from frontend.utils import get_images
8
 
9
- # Page configuration
10
- st.set_page_config(layout="wide")
11
 
12
- st.title('Code Interpreter API πŸš€')
 
13
 
14
- # This will create a sidebar
15
- st.sidebar.title("Code Interpreter API πŸš€")
16
 
17
- st.sidebar.markdown("### 给勇ε“₯ε“₯打call πŸš€")
18
 
19
- st.sidebar.markdown(
20
- "![Code Interpreter](https://vercel.brzhang.club/_next/image?url=%2Fcoffer.jpg&w=640&q=75)")
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
- # Call the get_images function using asyncio.run to handle the async behavior
39
- output = asyncio.run(get_images(input_text, files=uploaded_files_list))
40
 
41
- # Display output and images
42
- if output:
43
- with st.spinner():
44
- st.write(output.content)
45
 
46
- for _file in output.files:
47
- st.image(_file.get_image(), caption=input_text,
48
- use_column_width=True)
 
 
49
 
50
- # Allowing the download of the results
51
- if len(output.files) == 1:
52
- st.download_button('Download Results', output.files[0].content,
53
- file_name=output.files[0].name,
54
- use_container_width=True)
55
- else:
56
- target_path = tempfile.mkdtemp()
57
- for _file in output.files:
58
- _file.save(os.path.join(target_path, _file.name))
59
 
60
- zip_path = os.path.join(
61
- os.path.dirname(target_path), "archive")
62
- shutil.make_archive(zip_path, 'zip', target_path)
 
 
63
 
64
- with open(zip_path + ".zip", 'rb') as f:
65
- st.download_button('Download Results', f,
66
- file_name="archive.zip",
67
- use_container_width=True)
 
 
1
  import asyncio
 
 
 
 
 
2
 
3
+ import streamlit as st
 
4
 
5
+ from codeinterpreterapi import File
6
+ from frontend.utils import get_images
7
 
 
 
8
 
9
+ async def main():
10
 
11
+ # Page configuration
12
+ st.set_page_config(layout="wide")
13
 
14
+ st.title('Code Interpreter API πŸš€')
 
 
 
15
 
16
+ # This will create a sidebar
17
+ st.sidebar.title("Code Interpreter API πŸš€")
 
 
 
18
 
19
+ st.sidebar.markdown("### 给勇ε“₯ε“₯打call πŸš€")
 
20
 
21
+ st.sidebar.markdown(
22
+ "![Code Interpreter](https://vercel.brzhang.club/_next/image?url=%2Fcoffer.jpg&w=640&q=75)")
 
 
23
 
24
+ # This will create a textbox where you can input text
25
+ input_text = st.text_area("Write your prompt")
26
+ uploaded_files = st.file_uploader(
27
+ "Upload your files", accept_multiple_files=True)
28
 
29
+ uploaded_files_list = []
30
+ for uploaded_file in uploaded_files:
31
+ bytes_data = uploaded_file.read()
32
+ uploaded_files_list.append(File(name=uploaded_file.name,
33
+ content=bytes_data))
34
 
35
+ # This will create a button
36
+ button_pressed = st.button(
37
+ 'Run code interpreter', use_container_width=True)
 
 
 
 
 
 
38
 
39
+ # This will display the images only when the button is pressed
40
+ if button_pressed and input_text != "":
41
+ # asyncio.run(get_images(input_text, files=uploaded_files_list))
42
+ loop = asyncio.get_event_loop()
43
+ await loop.create_task(get_images(input_text, files=uploaded_files_list))
44
 
45
+ if __name__ == "__main__":
46
+ asyncio.run(main())