Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,8 +9,10 @@ import os
|
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
# Azure Form Recognizer credentials
|
| 12 |
-
endpoint = st.secrets["endpoint"]
|
| 13 |
-
key = st.secrets["key"]
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Initialize DocumentAnalysisClient
|
| 16 |
document_analysis_client = DocumentAnalysisClient(
|
|
@@ -76,7 +78,7 @@ if uploaded_file is not None:
|
|
| 76 |
|
| 77 |
table_df = pd.DataFrame(data, columns=["row_index", "column_index", "content"])
|
| 78 |
table_df = table_df.pivot(index="row_index", columns="column_index", values="content")
|
| 79 |
-
|
| 80 |
st.session_state.tables.append(table_df)
|
| 81 |
|
| 82 |
st.session_state.processed = True
|
|
@@ -103,38 +105,47 @@ if st.session_state.processed:
|
|
| 103 |
selected_columns = st.multiselect(
|
| 104 |
"Select columns from Key-Value Pairs to add to tables:", first_row_values
|
| 105 |
)
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
# Add the selected columns to the table
|
| 114 |
-
if selected_columns:
|
| 115 |
-
# Create a new DataFrame for the selected columns
|
| 116 |
-
selected_columns_df = st.session_state.kv_df[column_names].copy()
|
| 117 |
-
|
| 118 |
-
# Align the length of selected_columns_df with the table_df rows
|
| 119 |
-
selected_columns_df = selected_columns_df.iloc[:len(table_df)].reset_index(drop=True)
|
| 120 |
|
| 121 |
-
|
| 122 |
-
if
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
selected_columns_df = pd.concat([selected_columns_df, additional_rows], ignore_index=True)
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
label=f"Download Table {i + 1} as CSV",
|
| 134 |
data=table_df.to_csv(index=False, header=False).encode('utf-8'),
|
| 135 |
file_name=f"table_with_kv_{i + 1}.csv",
|
| 136 |
mime='text/csv',
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
else:
|
| 139 |
st.write("No tables found in the document.")
|
| 140 |
|
|
|
|
| 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 |
|
| 17 |
# Initialize DocumentAnalysisClient
|
| 18 |
document_analysis_client = DocumentAnalysisClient(
|
|
|
|
| 78 |
|
| 79 |
table_df = pd.DataFrame(data, columns=["row_index", "column_index", "content"])
|
| 80 |
table_df = table_df.pivot(index="row_index", columns="column_index", values="content")
|
| 81 |
+
|
| 82 |
st.session_state.tables.append(table_df)
|
| 83 |
|
| 84 |
st.session_state.processed = True
|
|
|
|
| 105 |
selected_columns = st.multiselect(
|
| 106 |
"Select columns from Key-Value Pairs to add to tables:", first_row_values
|
| 107 |
)
|
| 108 |
+
for i, table_df in enumerate(st.session_state.tables):
|
| 109 |
+
st.write(f"Table {i + 1} with Selected Columns Added:")
|
| 110 |
+
|
| 111 |
+
# Find the column names corresponding to the selected first row values
|
| 112 |
+
column_indices = [list(first_row_values).index(val) for val in selected_columns]
|
| 113 |
+
column_names = st.session_state.kv_df.columns[column_indices]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
# Add the selected columns to the table
|
| 116 |
+
if selected_columns:
|
| 117 |
+
# Create a new DataFrame for the selected columns
|
| 118 |
+
selected_columns_df = st.session_state.kv_df[column_names].copy()
|
|
|
|
| 119 |
|
| 120 |
+
# Align the length of selected_columns_df with the table_df rows
|
| 121 |
+
selected_columns_df = selected_columns_df.iloc[:len(table_df)].reset_index(drop=True)
|
| 122 |
+
|
| 123 |
+
rows_to_add = len(table_df) - len(selected_columns_df)
|
| 124 |
+
if rows_to_add > 0:
|
| 125 |
+
last_row = selected_columns_df.iloc[-1]
|
| 126 |
+
additional_rows = pd.DataFrame([last_row] * rows_to_add, columns=selected_columns_df.columns)
|
| 127 |
+
selected_columns_df = pd.concat([selected_columns_df, additional_rows], ignore_index=True)
|
| 128 |
+
|
| 129 |
+
# Concatenate the selected columns DataFrame with the table DataFrame
|
| 130 |
+
table_df = pd.concat([selected_columns_df, table_df.reset_index(drop=True)], axis=1, ignore_index=True)
|
| 131 |
+
|
| 132 |
+
st.dataframe(table_df)
|
| 133 |
+
st.download_button(
|
| 134 |
label=f"Download Table {i + 1} as CSV",
|
| 135 |
data=table_df.to_csv(index=False, header=False).encode('utf-8'),
|
| 136 |
file_name=f"table_with_kv_{i + 1}.csv",
|
| 137 |
mime='text/csv',
|
| 138 |
+
)
|
| 139 |
+
else:
|
| 140 |
+
for i, table_df in enumerate(st.session_state.tables):
|
| 141 |
+
st.write(f"Table {i + 1}:")
|
| 142 |
+
st.dataframe(table_df)
|
| 143 |
+
st.download_button(
|
| 144 |
+
label=f"Download Table {i + 1} as CSV",
|
| 145 |
+
data=table_df.to_csv(index=False, header=False).encode('utf-8'),
|
| 146 |
+
file_name=f"table{i + 1}.csv",
|
| 147 |
+
mime='text/csv',
|
| 148 |
+
)
|
| 149 |
else:
|
| 150 |
st.write("No tables found in the document.")
|
| 151 |
|