-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.py
executable file
·177 lines (166 loc) · 5.94 KB
/
frontend.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import streamlit as st
import requests
BACKEND_URL = "http://127.0.0.1:5000/"
@st.experimental_fragment
def show_download_button(output_path, label, filename):
with open(output_path, "rb") as zip_file_bytes:
st.download_button(
label=label,
data=zip_file_bytes,
file_name=filename,
)
@st.experimental_fragment
def invoice_processing():
st.title("Invoice Analysis")
st.write("Upload a invoice pdf to get extracted info")
uploaded_pdf_file = st.file_uploader("Choose a file", type=["pdf"])
if uploaded_pdf_file is not None and st.button("Process Invoice"):
files = {
"files": (
uploaded_pdf_file.name,
uploaded_pdf_file.read(),
)
}
filename = uploaded_pdf_file.name
with st.status("Summarizing Invoice...") as status:
response = requests.post(
BACKEND_URL + "/invoice/summarize_invoice", files=files
)
result = response.json()
if "error" in result:
status.update(
label="Invoice Summarization Failed!",
state="error",
expanded=True,
)
st.error(result["error"])
else:
status.update(
label="Invoice Summarization completed!",
state="complete",
expanded=True,
)
output_path = result["output_filepath"]
show_download_button(
output_path=output_path,
label="Download Invoice Info. CSV file",
filename=filename.replace(".pdf", ".csv"),
)
@st.experimental_fragment
def duplicate_detection():
st.title("Duplicate Analysis")
st.write("Upload a zip file containing all the documents")
uploaded_zip_file = st.file_uploader("Choose a zip file", type=["zip"])
if uploaded_zip_file is not None and st.button("Process"):
files = {
"files": (
uploaded_zip_file.name,
uploaded_zip_file.read(),
)
}
with st.status("Extracting data...") as status:
response = requests.post(
BACKEND_URL + "process_documents",
files=files,
)
status.update(
label="Data Extracted Successfully!",
state="complete",
expanded=True,
)
result = response.json()
if "error" in result:
st.error(result["error"])
else:
output_path = result["output_filepath"]
show_download_button(
output_path=output_path,
label="Download Zipped Archive",
filename="extracted_data.zip",
)
with st.status("Checking Duplicates...") as status:
response = requests.get(BACKEND_URL + "check_duplicates")
status.update(
label="Duplicates checking completed!",
state="complete",
expanded=True,
)
result = response.json()
if "error" in result:
st.error(result["error"])
else:
if "output_filepath" in result:
output_path = result["output_filepath"]
show_download_button(
output_path=output_path,
label="Download Duplicates CSV file",
filename="duplicates.csv",
)
st.markdown(result["response"])
else:
st.markdown(result["response"])
# st.set_page_config(layout="wide")
# invoice_summary_col, duplicate_detection_col = st.columns(2)
# with invoice_summary_col:
# invoice_processing()
# with duplicate_detection_col:
# duplicate_detection()
st.title("Document Processor")
st.write("Upload a pdf to process")
filetype = st.selectbox(
"Select the filetype", ["Invoice", "SalarySlip", "Work Completion Certificate"]
)
st.write("Upload a pdf to process")
uploaded_pdf_file = st.file_uploader("Choose a file", type=["pdf"])
if uploaded_pdf_file is not None and st.button("Process"):
files = {
"files": (
uploaded_pdf_file.name,
uploaded_pdf_file.read(),
)
}
filename = uploaded_pdf_file.name
with st.status("Extracting data...") as status:
response = requests.post(
BACKEND_URL + "v2/extract_data",
files=files,
)
status.update(
label="Data Extracted Successfully!",
state="complete",
expanded=True,
)
result = response.json()
if "error" in result:
st.error(result["error"])
else:
output_path = result["output_filepath"]
show_download_button(
output_path=output_path,
label="Download raw data",
filename=uploaded_pdf_file.name.replace(".pdf", ".xlsx"),
)
with st.status("Summarizing File...") as status:
response = requests.post(
BACKEND_URL + "v2/summarize_data", json={"filetype": filetype}
)
result = response.json()
if "error" in result:
status.update(
label="Summarization Failed!",
state="error",
expanded=True,
)
st.error(result["error"])
else:
status.update(
label="Summarization completed!",
state="complete",
expanded=True,
)
output_path = result["output_filepath"]
show_download_button(
output_path=output_path,
label="Download formatted data",
filename=filename.replace(".pdf", ".csv"),
)