Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +121 -0
- requirements.txt +52 -0
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
import os
|
| 8 |
+
|
| 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(
|
| 17 |
+
endpoint=endpoint, credential=AzureKeyCredential(key)
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Sidebar for document type selection
|
| 21 |
+
document_type = st.sidebar.selectbox(
|
| 22 |
+
"Select Document Type", ["Invoice", "Receipt", "Identity Document"]
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Center area for file upload
|
| 26 |
+
uploaded_file = st.file_uploader("Choose or drag a file", type=["pdf", "png", "jpg", "jpeg"])
|
| 27 |
+
|
| 28 |
+
# Initialize session state for processing results
|
| 29 |
+
if "processed" not in st.session_state:
|
| 30 |
+
st.session_state.processed = False
|
| 31 |
+
st.session_state.tables = []
|
| 32 |
+
st.session_state.kv_df = pd.DataFrame()
|
| 33 |
+
|
| 34 |
+
# Process the uploaded file if the button is clicked
|
| 35 |
+
if uploaded_file is not None:
|
| 36 |
+
if st.button("Upload and Process"):
|
| 37 |
+
with st.spinner("Processing..."):
|
| 38 |
+
time.sleep(1) # Simulate some delay to show the spinner
|
| 39 |
+
|
| 40 |
+
# Analyze the document
|
| 41 |
+
poller = document_analysis_client.begin_analyze_document(
|
| 42 |
+
"prebuilt-document", document=uploaded_file
|
| 43 |
+
)
|
| 44 |
+
result = poller.result()
|
| 45 |
+
|
| 46 |
+
# Extract key-value pairs and store them in a dictionary
|
| 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 |
+
kv_dict[kv_pair.key.content] = kv_pair.value.content
|
| 51 |
+
|
| 52 |
+
kv_df = pd.DataFrame(list(kv_dict.items()), columns=["Key", "Value"]).T
|
| 53 |
+
|
| 54 |
+
# Set the first row as the header
|
| 55 |
+
header = kv_df.iloc[0]
|
| 56 |
+
|
| 57 |
+
# Create a new DataFrame with the header and the remaining rows
|
| 58 |
+
kv_df = kv_df[1:].reset_index(drop=True)
|
| 59 |
+
|
| 60 |
+
# Insert the new header row
|
| 61 |
+
header_df = pd.DataFrame([header], columns=range(len(header)))
|
| 62 |
+
|
| 63 |
+
# Concatenate the header row DataFrame with the original DataFrame
|
| 64 |
+
kv_df = pd.concat([header_df, kv_df], ignore_index=True)
|
| 65 |
+
|
| 66 |
+
st.session_state.kv_df = kv_df
|
| 67 |
+
st.session_state.tables = []
|
| 68 |
+
|
| 69 |
+
if result.tables:
|
| 70 |
+
for table in result.tables:
|
| 71 |
+
temp_kv_df = kv_df.copy()
|
| 72 |
+
|
| 73 |
+
data = []
|
| 74 |
+
for cell in table.cells:
|
| 75 |
+
data.append([cell.row_index, cell.column_index, cell.content])
|
| 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 |
+
rows_to_add = len(table_df) - len(temp_kv_df)
|
| 81 |
+
if rows_to_add > 0:
|
| 82 |
+
last_row = temp_kv_df.iloc[-1]
|
| 83 |
+
additional_rows = pd.DataFrame([last_row] * rows_to_add, columns=temp_kv_df.columns)
|
| 84 |
+
temp_kv_df = pd.concat([temp_kv_df, additional_rows], ignore_index=True)
|
| 85 |
+
|
| 86 |
+
table_with_kv = pd.concat([temp_kv_df, table_df], axis=1, ignore_index=True)
|
| 87 |
+
st.session_state.tables.append(table_with_kv)
|
| 88 |
+
|
| 89 |
+
st.session_state.processed = True
|
| 90 |
+
|
| 91 |
+
# Display the results if processing is done
|
| 92 |
+
if st.session_state.processed:
|
| 93 |
+
st.write("Extracted Key-Value Pairs:")
|
| 94 |
+
st.dataframe(st.session_state.kv_df)
|
| 95 |
+
|
| 96 |
+
if st.session_state.tables:
|
| 97 |
+
for i, table_with_kv in enumerate(st.session_state.tables):
|
| 98 |
+
st.write(f"Table {i + 1} with Key-Value Pairs:")
|
| 99 |
+
st.dataframe(table_with_kv)
|
| 100 |
+
|
| 101 |
+
st.download_button(
|
| 102 |
+
label=f"Download Table {i + 1} as CSV",
|
| 103 |
+
data=table_with_kv.to_csv(index=False, header=False).encode('utf-8'),
|
| 104 |
+
file_name=f"table_with_kv_{i + 1}.csv",
|
| 105 |
+
mime='text/csv',
|
| 106 |
+
)
|
| 107 |
+
else:
|
| 108 |
+
st.write("No tables found in the document.")
|
| 109 |
+
st.download_button(
|
| 110 |
+
label="Download Key-Value Pairs as CSV",
|
| 111 |
+
data=st.session_state.kv_df.to_csv(index=False, header=False).encode('utf-8'),
|
| 112 |
+
file_name="kv_df.csv",
|
| 113 |
+
mime='text/csv',
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# Option to remove the file and clear the session
|
| 117 |
+
if st.session_state.processed and st.button("Remove File"):
|
| 118 |
+
st.session_state.processed = False
|
| 119 |
+
st.session_state.tables = []
|
| 120 |
+
st.session_state.kv_df = pd.DataFrame()
|
| 121 |
+
st.rerun()
|
requirements.txt
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
altair==5.4.1
|
| 2 |
+
attrs==24.2.0
|
| 3 |
+
azure-ai-documentintelligence==1.0.0b2
|
| 4 |
+
azure-ai-formrecognizer==3.3.3
|
| 5 |
+
azure-ai-vision-imageanalysis==1.0.0b3
|
| 6 |
+
azure-common==1.1.28
|
| 7 |
+
azure-core==1.30.2
|
| 8 |
+
blinker==1.8.2
|
| 9 |
+
cachetools==5.5.0
|
| 10 |
+
certifi==2024.8.30
|
| 11 |
+
charset-normalizer==3.3.2
|
| 12 |
+
click==8.1.7
|
| 13 |
+
colorama==0.4.6
|
| 14 |
+
gitdb==4.0.11
|
| 15 |
+
GitPython==3.1.43
|
| 16 |
+
idna==3.8
|
| 17 |
+
isodate==0.6.1
|
| 18 |
+
Jinja2==3.1.4
|
| 19 |
+
jsonschema==4.23.0
|
| 20 |
+
jsonschema-specifications==2023.12.1
|
| 21 |
+
markdown-it-py==3.0.0
|
| 22 |
+
MarkupSafe==2.1.5
|
| 23 |
+
mdurl==0.1.2
|
| 24 |
+
msrest==0.7.1
|
| 25 |
+
narwhals==1.6.0
|
| 26 |
+
numpy==2.1.0
|
| 27 |
+
oauthlib==3.2.2
|
| 28 |
+
packaging==24.1
|
| 29 |
+
pandas==2.2.2
|
| 30 |
+
pillow==10.4.0
|
| 31 |
+
protobuf==5.28.0
|
| 32 |
+
pyarrow==17.0.0
|
| 33 |
+
pydeck==0.9.1
|
| 34 |
+
Pygments==2.18.0
|
| 35 |
+
python-dateutil==2.9.0.post0
|
| 36 |
+
python-dotenv==1.0.1
|
| 37 |
+
pytz==2024.1
|
| 38 |
+
referencing==0.35.1
|
| 39 |
+
requests==2.32.3
|
| 40 |
+
requests-oauthlib==2.0.0
|
| 41 |
+
rich==13.8.0
|
| 42 |
+
rpds-py==0.20.0
|
| 43 |
+
six==1.16.0
|
| 44 |
+
smmap==5.0.1
|
| 45 |
+
streamlit==1.38.0
|
| 46 |
+
tenacity==8.5.0
|
| 47 |
+
toml==0.10.2
|
| 48 |
+
tornado==6.4.1
|
| 49 |
+
typing_extensions==4.12.2
|
| 50 |
+
tzdata==2024.1
|
| 51 |
+
urllib3==2.2.2
|
| 52 |
+
watchdog==4.0.2
|