Spaces:
Sleeping
Sleeping
Update utils/uploadAndExample.py
Browse files- utils/uploadAndExample.py +38 -0
utils/uploadAndExample.py
CHANGED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tempfile
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
def add_upload(choice):
|
| 6 |
+
"""
|
| 7 |
+
Provdies the user with choice to either 'Upload Document' or 'Try Example'.
|
| 8 |
+
Based on user choice runs streamlit processes and save the path and name of
|
| 9 |
+
the 'file' to streamlit session_state which then can be fetched later.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
if choice == 'Upload Document':
|
| 13 |
+
|
| 14 |
+
# if 'filename' in st.session_state:
|
| 15 |
+
# Delete all the items in Session state
|
| 16 |
+
# for key in st.session_state.keys():
|
| 17 |
+
# del st.session_state[key]
|
| 18 |
+
|
| 19 |
+
uploaded_file = st.sidebar.file_uploader('Upload the File',
|
| 20 |
+
type=['pdf', 'docx', 'txt'])
|
| 21 |
+
if uploaded_file is not None:
|
| 22 |
+
with tempfile.NamedTemporaryFile(mode="wb", delete = False) as temp:
|
| 23 |
+
bytes_data = uploaded_file.getvalue()
|
| 24 |
+
temp.write(bytes_data)
|
| 25 |
+
st.session_state['filename'] = uploaded_file.name
|
| 26 |
+
st.session_state['filepath'] = temp.name
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
else:
|
| 30 |
+
# listing the options
|
| 31 |
+
with open('docStore/sample/files.json','r') as json_file:
|
| 32 |
+
files = json.load(json_file)
|
| 33 |
+
|
| 34 |
+
option = st.sidebar.selectbox('Select the example document',
|
| 35 |
+
list(files.keys()))
|
| 36 |
+
file_name = file_path = files[option]
|
| 37 |
+
st.session_state['filename'] = file_name
|
| 38 |
+
st.session_state['filepath'] = file_path
|