zmbfeng commited on
Commit
897250e
1 Parent(s): 5535d25

file upload done

Browse files
Files changed (1) hide show
  1. app.py +60 -2
app.py CHANGED
@@ -1,3 +1,61 @@
1
  import streamlit as st
2
- x = st.slider('Select a value')
3
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import os
3
+ import json
4
+ def is_new_file_upload(uploaded_file):
5
+ if 'last_uploaded_file' in st.session_state:
6
+ # Check if the newly uploaded file is different from the last one
7
+ if (uploaded_file.name != st.session_state.last_uploaded_file['name'] or
8
+ uploaded_file.size != st.session_state.last_uploaded_file['size']):
9
+ st.session_state.last_uploaded_file = {'name': uploaded_file.name, 'size': uploaded_file.size}
10
+ # st.write("A new src image file has been uploaded.")
11
+ return True
12
+ else:
13
+ # st.write("The same src image file has been re-uploaded.")
14
+ return False
15
+ else:
16
+ # st.write("This is the first file upload detected.")
17
+ st.session_state.last_uploaded_file = {'name': uploaded_file.name, 'size': uploaded_file.size}
18
+ return True
19
+ big_text = """
20
+ <div style='text-align: center;'>
21
+ <h1 style='font-size: 30x;'>Knowledge Extraction 1</h1>
22
+ </div>
23
+ """
24
+ # Display the styled text
25
+ st.markdown(big_text, unsafe_allow_html=True)
26
+
27
+ uploaded_json_file = st.file_uploader("Upload a pre-processed file",
28
+ type=['json'])
29
+ st.markdown(
30
+ f'<a href="https://ikmtechnology.github.io/ikmtechnology/untethered_extracted_paragraphs.json" target="_blank">Sample 1 download and then upload to above</a>',
31
+ unsafe_allow_html=True)
32
+
33
+ if uploaded_json_file is not None:
34
+ if is_new_file_upload(uploaded_json_file):
35
+ print("is new file uploaded")
36
+ save_path = './uploaded_files'
37
+ if not os.path.exists(save_path):
38
+ os.makedirs(save_path)
39
+ with open(os.path.join(save_path, uploaded_json_file.name), "wb") as f:
40
+ f.write(uploaded_json_file.getbuffer()) # Write the file to the specified location
41
+ st.success(f'Saved file temp_{uploaded_json_file.name} in {save_path}')
42
+ st.session_state.uploaded_path=os.path.join(save_path, uploaded_json_file.name)
43
+ # st.session_state.page_count = utils.get_pdf_page_count(st.session_state.uploaded_pdf_path)
44
+ # print("page_count=",st.session_state.page_count)
45
+ content = uploaded_json_file.read()
46
+ try:
47
+ data = json.loads(content)
48
+ #print(data)
49
+ # Check if the parsed data is a dictionary
50
+ if isinstance(data, list):
51
+ # Count the number of top-level elements
52
+ st.session_state.list_count = len(data)
53
+ st.write(f'The number of elements at the top level of the hierarchy: {st.session_state.list_count }')
54
+ else:
55
+ st.write('The JSON content is not a dictionary.')
56
+ except json.JSONDecodeError:
57
+ st.write('Invalid JSON file.')
58
+ st.rerun()
59
+
60
+ if 'list_count' in st.session_state:
61
+ st.write(f'The number of elements at the top level of the hierarchy: {st.session_state.list_count }')