nithin81 commited on
Commit
5585dcf
·
verified ·
1 Parent(s): d26f874

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -25
app.py CHANGED
@@ -9,8 +9,6 @@ import os
9
  load_dotenv()
10
 
11
  # Azure Form Recognizer credentials
12
- # endpoint = st.secrets["endpoint"]
13
- # key = st.secrets["key"]
14
  endpoint = os.environ["endpoint"]
15
  key = os.environ["key"]
16
 
@@ -49,8 +47,8 @@ if uploaded_file is not None:
49
  kv_dict = {}
50
  for kv_pair in result.key_value_pairs:
51
  if kv_pair.key and kv_pair.value: # Ensure both key and value exist
52
- kv_dict[kv_pair.key.content] = kv_pair.value.content
53
-
54
  kv_df = pd.DataFrame(list(kv_dict.items()), columns=["Key", "Value"]).T
55
 
56
  # Set the first row as the header
@@ -81,55 +79,54 @@ if uploaded_file is not None:
81
 
82
  st.session_state.processed = True
83
 
84
- # Display the results if processing is done
85
  if st.session_state.processed:
86
  if not st.session_state.kv_df.empty:
87
- st.download_button(
88
  label="Download Key-Value Pairs as CSV",
89
  data=st.session_state.kv_df.to_csv(index=False, header=False).encode('utf-8'),
90
  file_name="kv_df.csv",
91
  mime='text/csv',
92
  )
93
  else:
94
- st.write("No Key-Value Pairs found in document.")
95
 
96
 
97
  if st.session_state.tables:
98
- # Display available tables for selection, with "None" as the first option
99
  table_options = ["Select"] + [f"Table {i + 1}" for i in range(len(st.session_state.tables))]
100
- selected_table_option = st.selectbox(
101
  "Select table to join key-value pairs:", table_options, index=0
102
  )
103
 
104
- # Proceed only if a table is selected (i.e., not "None")
105
  if selected_table_option != "Select":
106
  selected_table_index = table_options.index(selected_table_option) - 1 # Adjust for "None"
107
  selected_table_df = st.session_state.tables[selected_table_index]
108
 
109
  if not st.session_state.kv_df.empty:
110
- # Allow the user to select multiple columns
111
  first_row_values = st.session_state.kv_df.iloc[0].values
112
  second_row_values = st.session_state.kv_df.iloc[1].values
113
- # Format as "Key (row 0 value): Value (row 1 value)"
114
  key_value_options = [
115
  f"{key}: {value}" for key, value in zip(first_row_values, second_row_values)
116
  ]
117
 
118
- selected_columns = st.multiselect(
119
- "Select key-value pairs to add to the selected table:", key_value_options
120
- )
121
-
122
- # Extract the column indices of the selected items
123
- selected_indices = [
124
- key_value_options.index(selected) for selected in selected_columns
125
- ]
126
 
 
 
 
 
 
127
  st.write(f"Selected Table {selected_table_index + 1} with Key-Value Pairs Added:")
128
 
129
  # Add the selected columns to the table
130
  if selected_columns:
131
  # Create a DataFrame for the selected columns using the original kv_df
132
- selected_columns_df = st.session_state.kv_df.iloc[:, selected_indices].copy()
133
 
134
  # Align the length of selected_columns_df with the table_df rows
135
  selected_columns_df = selected_columns_df.iloc[:len(selected_table_df)].reset_index(drop=True)
@@ -152,20 +149,19 @@ if st.session_state.processed:
152
  mime='text/csv',
153
  )
154
  else:
155
-
156
  st.write(f"Selected Table {selected_table_index + 1}:")
157
  st.dataframe(selected_table_df)
158
  st.download_button(
159
  label=f"Download Table {selected_table_index + 1} as CSV",
160
- data=table_df.to_csv(index=False, header=False).encode('utf-8'),
161
  file_name=f"table{selected_table_index + 1}.csv",
162
  mime='text/csv',
163
  )
164
  else:
165
- st.write("No tables found in the document.")
166
 
167
  # Option to remove the file and clear the session
168
- if st.session_state.processed and st.button("Remove File"):
169
  st.session_state.processed = False
170
  st.session_state.tables = []
171
  st.session_state.kv_df = pd.DataFrame()
 
9
  load_dotenv()
10
 
11
  # Azure Form Recognizer credentials
 
 
12
  endpoint = os.environ["endpoint"]
13
  key = os.environ["key"]
14
 
 
47
  kv_dict = {}
48
  for kv_pair in result.key_value_pairs:
49
  if kv_pair.key and kv_pair.value: # Ensure both key and value exist
50
+ clean_key = kv_pair.key.content.rstrip(':')
51
+ kv_dict[clean_key] = kv_pair.value.content
52
  kv_df = pd.DataFrame(list(kv_dict.items()), columns=["Key", "Value"]).T
53
 
54
  # Set the first row as the header
 
79
 
80
  st.session_state.processed = True
81
 
82
+ # Sidebar for table and key-value pair selection
83
  if st.session_state.processed:
84
  if not st.session_state.kv_df.empty:
85
+ st.sidebar.download_button(
86
  label="Download Key-Value Pairs as CSV",
87
  data=st.session_state.kv_df.to_csv(index=False, header=False).encode('utf-8'),
88
  file_name="kv_df.csv",
89
  mime='text/csv',
90
  )
91
  else:
92
+ st.sidebar.write("No Key-Value Pairs found in document.")
93
 
94
 
95
  if st.session_state.tables:
96
+ # Display available tables for selection in the sidebar
97
  table_options = ["Select"] + [f"Table {i + 1}" for i in range(len(st.session_state.tables))]
98
+ selected_table_option = st.sidebar.selectbox(
99
  "Select table to join key-value pairs:", table_options, index=0
100
  )
101
 
102
+ # Proceed only if a table is selected
103
  if selected_table_option != "Select":
104
  selected_table_index = table_options.index(selected_table_option) - 1 # Adjust for "None"
105
  selected_table_df = st.session_state.tables[selected_table_index]
106
 
107
  if not st.session_state.kv_df.empty:
108
+ # Allow the user to select multiple columns using checkboxes
109
  first_row_values = st.session_state.kv_df.iloc[0].values
110
  second_row_values = st.session_state.kv_df.iloc[1].values
 
111
  key_value_options = [
112
  f"{key}: {value}" for key, value in zip(first_row_values, second_row_values)
113
  ]
114
 
115
+ selected_columns = []
116
+
117
+ st.sidebar.write("Select key-value pairs to add to the selected table:")
 
 
 
 
 
118
 
119
+ # Create checkboxes for each key-value pair
120
+ for i, option in enumerate(key_value_options):
121
+ if st.sidebar.checkbox(option):
122
+ selected_columns.append(i)
123
+
124
  st.write(f"Selected Table {selected_table_index + 1} with Key-Value Pairs Added:")
125
 
126
  # Add the selected columns to the table
127
  if selected_columns:
128
  # Create a DataFrame for the selected columns using the original kv_df
129
+ selected_columns_df = st.session_state.kv_df.iloc[:, selected_columns].copy()
130
 
131
  # Align the length of selected_columns_df with the table_df rows
132
  selected_columns_df = selected_columns_df.iloc[:len(selected_table_df)].reset_index(drop=True)
 
149
  mime='text/csv',
150
  )
151
  else:
 
152
  st.write(f"Selected Table {selected_table_index + 1}:")
153
  st.dataframe(selected_table_df)
154
  st.download_button(
155
  label=f"Download Table {selected_table_index + 1} as CSV",
156
+ data=selected_table_df.to_csv(index=False, header=False).encode('utf-8'),
157
  file_name=f"table{selected_table_index + 1}.csv",
158
  mime='text/csv',
159
  )
160
  else:
161
+ st.sidebar.write("No tables found in the document.")
162
 
163
  # Option to remove the file and clear the session
164
+ if st.session_state.processed and st.sidebar.button("Remove File"):
165
  st.session_state.processed = False
166
  st.session_state.tables = []
167
  st.session_state.kv_df = pd.DataFrame()