File size: 2,950 Bytes
5535d25
897250e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import streamlit as st
import os
import json
def is_new_file_upload(uploaded_file):
    if 'last_uploaded_file' in st.session_state:
        # Check if the newly uploaded file is different from the last one
        if (uploaded_file.name != st.session_state.last_uploaded_file['name'] or
                uploaded_file.size != st.session_state.last_uploaded_file['size']):
            st.session_state.last_uploaded_file = {'name': uploaded_file.name, 'size': uploaded_file.size}
            # st.write("A new src image file has been uploaded.")
            return True
        else:
            # st.write("The same src image file has been re-uploaded.")
            return False
    else:
        # st.write("This is the first file upload detected.")
        st.session_state.last_uploaded_file = {'name': uploaded_file.name, 'size': uploaded_file.size}
        return True
big_text = """
    <div style='text-align: center;'>
        <h1 style='font-size: 30x;'>Knowledge Extraction 1</h1>
    </div>
    """
    # Display the styled text
st.markdown(big_text, unsafe_allow_html=True)

uploaded_json_file = st.file_uploader("Upload a pre-processed file",
                                           type=['json'])
st.markdown(
    f'<a href="https://ikmtechnology.github.io/ikmtechnology/untethered_extracted_paragraphs.json" target="_blank">Sample 1 download and then upload to above</a>',
    unsafe_allow_html=True)

if uploaded_json_file is not None:
    if is_new_file_upload(uploaded_json_file):
        print("is new file uploaded")
        save_path = './uploaded_files'
        if not os.path.exists(save_path):
            os.makedirs(save_path)
        with open(os.path.join(save_path, uploaded_json_file.name), "wb") as f:
            f.write(uploaded_json_file.getbuffer())  # Write the file to the specified location
            st.success(f'Saved file temp_{uploaded_json_file.name} in {save_path}')
            st.session_state.uploaded_path=os.path.join(save_path, uploaded_json_file.name)
            # st.session_state.page_count = utils.get_pdf_page_count(st.session_state.uploaded_pdf_path)
            # print("page_count=",st.session_state.page_count)
        content = uploaded_json_file.read()
        try:
            data = json.loads(content)
            #print(data)
            # Check if the parsed data is a dictionary
            if isinstance(data, list):
                # Count the number of top-level elements
                st.session_state.list_count  = len(data)
                st.write(f'The number of elements at the top level of the hierarchy: {st.session_state.list_count }')
            else:
                st.write('The JSON content is not a dictionary.')
        except json.JSONDecodeError:
            st.write('Invalid JSON file.')
        st.rerun()

if 'list_count' in st.session_state:
    st.write(f'The number of elements at the top level of the hierarchy: {st.session_state.list_count }')