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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -35
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  import pandas as pd
 
3
  from azure.core.credentials import AzureKeyCredential
4
  from azure.ai.formrecognizer import DocumentAnalysisClient
5
  import time
@@ -74,20 +75,33 @@ if uploaded_file is not None:
74
 
75
  table_df = pd.DataFrame(data, columns=["row_index", "column_index", "content"])
76
  table_df = table_df.pivot(index="row_index", columns="column_index", values="content")
77
-
 
 
 
 
 
 
 
 
 
78
  st.session_state.tables.append(table_df)
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
 
@@ -104,6 +118,13 @@ if st.session_state.processed:
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
@@ -114,37 +135,32 @@ if st.session_state.processed:
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)
 
133
 
134
- # Add more rows if needed to match the table_df size
135
- rows_to_add = len(selected_table_df) - len(selected_columns_df)
136
- if rows_to_add > 0:
137
- last_row = selected_columns_df.iloc[-1]
138
- additional_rows = pd.DataFrame([last_row] * rows_to_add, columns=selected_columns_df.columns)
139
- selected_columns_df = pd.concat([selected_columns_df, additional_rows], ignore_index=True)
140
 
141
- # Concatenate the selected columns with the selected table DataFrame
142
- selected_table_df = pd.concat([selected_columns_df, selected_table_df.reset_index(drop=True)], axis=1, ignore_index=True)
143
 
144
  st.dataframe(selected_table_df)
145
  st.download_button(
146
  label=f"Download Table {selected_table_index + 1} as CSV",
147
- data=selected_table_df.to_csv(index=False, header=False).encode('utf-8'),
148
  file_name=f"table_with_kv_{selected_table_index + 1}.csv",
149
  mime='text/csv',
150
  )
@@ -152,10 +168,10 @@ if st.session_state.processed:
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.")
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import numpy as np
4
  from azure.core.credentials import AzureKeyCredential
5
  from azure.ai.formrecognizer import DocumentAnalysisClient
6
  import time
 
75
 
76
  table_df = pd.DataFrame(data, columns=["row_index", "column_index", "content"])
77
  table_df = table_df.pivot(index="row_index", columns="column_index", values="content")
78
+ # Reset the column index
79
+ table_df.reset_index(drop=True, inplace=True)
80
+
81
+ # Set the first row as header
82
+ table_df.columns = table_df.iloc[0] # Take the first row as column names
83
+ table_df = table_df.drop(table_df.index[0]) # Remove the first row after setting it as headers
84
+
85
+ # Optionally reset the index if required
86
+ table_df.reset_index(drop=True, inplace=True)
87
+
88
  st.session_state.tables.append(table_df)
89
 
90
  st.session_state.processed = True
91
 
92
+ # Predefine default columns in the selected table
93
+ default_columns = ['Invoice No', 'Invoice Date', 'Customer Name']
94
+
95
  # Sidebar for table and key-value pair selection
96
  if st.session_state.processed:
97
  if not st.session_state.kv_df.empty:
98
+ # st.sidebar.download_button(
99
+ # label="Download Key-Value Pairs as CSV",
100
+ # data=st.session_state.kv_df.to_csv(index=False, header=False).encode('utf-8'),
101
+ # file_name="kv_df.csv",
102
+ # mime='text/csv',
103
+ # )
104
+ pass
105
  else:
106
  st.sidebar.write("No Key-Value Pairs found in document.")
107
 
 
118
  selected_table_index = table_options.index(selected_table_option) - 1 # Adjust for "None"
119
  selected_table_df = st.session_state.tables[selected_table_index]
120
 
121
+ # Add default columns to the left side of the table with NaN values initially
122
+ for col in default_columns:
123
+ selected_table_df[col] = np.nan
124
+
125
+ # Move default columns to the left
126
+ selected_table_df = selected_table_df[[*default_columns, *selected_table_df.columns[:-len(default_columns)]]]
127
+
128
  if not st.session_state.kv_df.empty:
129
  # Allow the user to select multiple columns using checkboxes
130
  first_row_values = st.session_state.kv_df.iloc[0].values
 
135
 
136
  selected_columns = []
137
 
138
+ st.sidebar.write("Select key-value pairs to the default columns:")
 
 
 
 
 
139
 
140
+ # Create draggable interface for each key-value pair
141
+ for i, option in enumerate(key_value_options):
142
+ selected_column_option = st.sidebar.selectbox(
143
+ f"Insert {option} to the column:",
144
+ ["Select"] + default_columns,
145
+ key=f"col_select_{i}"
146
+ )
147
+
148
+ if selected_column_option != "Select":
149
+ selected_columns.append((i, selected_column_option))
150
 
151
+ # Insert selected key-value pairs into the appropriate columns
152
+ for idx, selected_column in selected_columns:
153
+ st.session_state.kv_df[selected_column] = second_row_values[idx]
154
+
155
+ # Fill the column in the table with the selected key-value pair
156
+ selected_table_df[selected_column] = st.session_state.kv_df.iloc[1, idx]
157
 
158
+ st.write(f"Selected Table {selected_table_index + 1} with Key-Value Pairs Added:")
 
159
 
160
  st.dataframe(selected_table_df)
161
  st.download_button(
162
  label=f"Download Table {selected_table_index + 1} as CSV",
163
+ data=selected_table_df.to_csv(index=False).encode('utf-8'),
164
  file_name=f"table_with_kv_{selected_table_index + 1}.csv",
165
  mime='text/csv',
166
  )
 
168
  st.write(f"Selected Table {selected_table_index + 1}:")
169
  st.dataframe(selected_table_df)
170
  st.download_button(
171
+ label=f"Download Table {selected_table_index + 1} as CSV",
172
+ data=selected_table_df.to_csv(index=False).encode('utf-8'),
173
+ file_name=f"table{selected_table_index + 1}.csv",
174
+ mime='text/csv',
175
  )
176
  else:
177
  st.sidebar.write("No tables found in the document.")