Spaces:
Build error
Build error
feat: add streamlit app
Browse files
app.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import py7zr
|
2 |
+
import streamlit as st
|
3 |
+
|
4 |
+
from planning_ai.common.utils import Paths
|
5 |
+
from planning_ai.main import main as report_main
|
6 |
+
from planning_ai.preprocessing.azure_doc import azure_process_pdfs
|
7 |
+
from planning_ai.preprocessing.gcpt3 import main as preprocess_main
|
8 |
+
|
9 |
+
UPLOAD_DIR = Paths.RAW / "gcpt3"
|
10 |
+
|
11 |
+
if "files_extracted" not in st.session_state:
|
12 |
+
st.session_state["files_extracted"] = False
|
13 |
+
if "files_processed" not in st.session_state:
|
14 |
+
st.session_state["files_processed"] = False
|
15 |
+
if "pdfs_processed" not in st.session_state:
|
16 |
+
st.session_state["pdfs_processed"] = False
|
17 |
+
|
18 |
+
st.title("Planning AI")
|
19 |
+
|
20 |
+
|
21 |
+
st.header("1. Upload JDL response `.json` files")
|
22 |
+
st.write(
|
23 |
+
"Upload your `.json` files here as a `7zip` file, they will be saved to the `data/raw/gcpt3` directory."
|
24 |
+
)
|
25 |
+
uploaded_file = st.file_uploader("Choose a `.7z` file.", type="7z")
|
26 |
+
|
27 |
+
if uploaded_file and not st.session_state["files_extracted"]:
|
28 |
+
with st.spinner("Extracting files..."):
|
29 |
+
try:
|
30 |
+
with py7zr.SevenZipFile(uploaded_file, mode="r") as archive:
|
31 |
+
archive.extractall(path=UPLOAD_DIR)
|
32 |
+
st.session_state["files_extracted"] = True
|
33 |
+
st.write(f"Extracted all files to `{UPLOAD_DIR}`.")
|
34 |
+
except Exception as e:
|
35 |
+
st.error(f"Failed to extract files {e}")
|
36 |
+
|
37 |
+
if not st.session_state["files_extracted"]:
|
38 |
+
st.write("No files uploaded yet.")
|
39 |
+
|
40 |
+
if st.session_state["files_extracted"]:
|
41 |
+
st.header("2. Process uploaded `.json` files")
|
42 |
+
st.write(
|
43 |
+
"Once the files are extracted, click the button below to start preprocessing the `.json` files."
|
44 |
+
)
|
45 |
+
if st.button("Process Files"):
|
46 |
+
with st.spinner("Running preprocessing..."):
|
47 |
+
try:
|
48 |
+
preprocess_main()
|
49 |
+
st.session_state["files_processed"] = True
|
50 |
+
st.success("Preprocessing completed successfully!")
|
51 |
+
except Exception as e:
|
52 |
+
st.error(f"An error occurred during preprocessing: {e}")
|
53 |
+
|
54 |
+
if st.session_state["files_extracted"] and st.session_state["files_processed"]:
|
55 |
+
st.header("3. Extract text from PDFs.")
|
56 |
+
st.write(
|
57 |
+
"After preprocessing the `.json` files, you can now extract text from the PDFs by clicking the button below."
|
58 |
+
)
|
59 |
+
if st.button("Process PDFs"):
|
60 |
+
with st.spinner("Extracting text from PDFs..."):
|
61 |
+
try:
|
62 |
+
azure_process_pdfs()
|
63 |
+
st.session_state["pdfs_processed"] = True
|
64 |
+
st.success("Text extraction completed successfully!")
|
65 |
+
except Exception as e:
|
66 |
+
st.error(f"An error occurred during PDF text extraction: {e}")
|
67 |
+
|
68 |
+
if (
|
69 |
+
st.session_state["files_extracted"]
|
70 |
+
and st.session_state["files_processed"]
|
71 |
+
and st.session_state["pdfs_processed"]
|
72 |
+
):
|
73 |
+
st.title("Build final report.")
|
74 |
+
st.write(
|
75 |
+
"After extracting text from PDFs, you can now run the full report building pipeline!"
|
76 |
+
)
|
77 |
+
if st.button("Build Report", type="primary"):
|
78 |
+
with st.spinner("Building report..."):
|
79 |
+
try:
|
80 |
+
report_main()
|
81 |
+
except Exception as e:
|
82 |
+
st.error(f"An error occurred during report building: {e}")
|
83 |
+
report_path = Paths.SUMMARY / "Summary_Documents.pdf"
|
84 |
+
summaries_path = Paths.SUMMARY / "Summary_of_Submitted_Responses.pdf"
|
85 |
+
|
86 |
+
if report_path.exists() and summaries_path.exists():
|
87 |
+
st.success("Report built successfully! Check the `data/out` directory.")
|
88 |
+
col1, col2 = st.columns(2)
|
89 |
+
with col1:
|
90 |
+
with open(summaries_path, "rb") as pdf_file:
|
91 |
+
st.download_button(
|
92 |
+
label="Download Report",
|
93 |
+
data=pdf_file,
|
94 |
+
file_name="Summary_of_Submitted_Responses.pdf",
|
95 |
+
mime="application/pdf",
|
96 |
+
)
|
97 |
+
with col2:
|
98 |
+
with open(report_path, "rb") as pdf_file:
|
99 |
+
st.download_button(
|
100 |
+
label="Download Summaries Report",
|
101 |
+
data=pdf_file,
|
102 |
+
file_name="Summary_Documents.pdf",
|
103 |
+
mime="application/pdf",
|
104 |
+
)
|