Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
290 lines (249 loc) ยท 8.38 KB
/
Copy pathapp.py
File metadata and controls
290 lines (249 loc) ยท 8.38 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from __future__ import annotations
import json
import shutil
import tempfile
from pathlib import Path
import streamlit as st
from workflow import run_payment_review
st.set_page_config(
page_title="BuildPay AI | Construction Payment Review",
page_icon="๐๏ธ",
layout="wide",
initial_sidebar_state="expanded",
)
# ---------- Styling ----------
st.markdown(
"""
<style>
:root {
--ink: #102033;
--muted: #667085;
--line: #E7ECF2;
--surface: #FFFFFF;
--soft: #F5F7FA;
}
.block-container { max-width: 1440px; padding-top: 2rem; }
.hero {
padding: 1.5rem 1.7rem;
border: 1px solid #E7ECF2;
border-radius: 22px;
background: linear-gradient(135deg, #ffffff 0%, #f5f8fc 100%);
box-shadow: 0 12px 35px rgba(16, 32, 51, .06);
}
.hero h1 { margin: 0; color: #102033; font-size: 2.25rem; }
.hero p { color: #667085; margin: .45rem 0 0; font-size: 1.02rem; }
.agent-card {
border: 1px solid #E7ECF2;
border-radius: 16px;
padding: 14px 16px;
background: white;
margin-bottom: 10px;
}
.kpi {
border: 1px solid #E7ECF2;
border-radius: 16px;
padding: 16px;
background: white;
}
.kpi .label { color: #667085; font-size: .82rem; }
.kpi .value { color: #102033; font-size: 1.25rem; font-weight: 700; margin-top: 3px; }
.human-gate {
border: 1px solid #F2D18B;
background: #FFF9EA;
border-radius: 18px;
padding: 18px;
}
.small { color: #667085; font-size: .85rem; }
</style>
""",
unsafe_allow_html=True,
)
AGENTS = [
("intake", "๐", "Intake & Evidence"),
("contract", "๐", "Contract Compliance"),
("valuation", "๐", "Measurement & Valuation"),
("payment", "๐งฎ", "Payment Reconciliation"),
("risk", "๐ก๏ธ", "Risk & Exceptions"),
("review", "๐งโ๐ผ", "Human Review Brief"),
]
def save_uploads(uploaded_files) -> str:
run_dir = Path(tempfile.mkdtemp(prefix="buildpay_"))
for uploaded in uploaded_files:
(run_dir / uploaded.name).write_bytes(uploaded.getvalue())
return str(run_dir)
def status_panel(container, current_name: str, state: str, progress: int):
rows = []
for key, icon, name in AGENTS:
if name == current_name:
marker = "๐ข" if state == "Completed" else "๐ต"
label = f"{marker} **{name}** โ {state}"
else:
label = f"โช {name}"
rows.append(label)
container.markdown("\n\n".join(rows))
container.progress(min(progress, 100), text=f"{current_name}: {state}")
# ---------- Header ----------
st.markdown(
"""
<div class="hero">
<h1>๐๏ธ BuildPay AI</h1>
<p>Multi-agent construction payment review โ evidence first, calculations second, human decision always.</p>
</div>
""",
unsafe_allow_html=True,
)
st.write("")
# ---------- Sidebar ----------
with st.sidebar:
st.header("Review setup")
st.caption("AI assists the payment review. It never releases or authorizes money.")
project_context = st.text_area(
"Project context",
placeholder=(
"Example: Project name, contract type, payment application number, "
"currency, reporting period, known retention %, and any special instructions."
),
height=180,
)
uploaded_files = st.file_uploader(
"Upload payment documents",
type=["pdf", "docx", "xlsx", "csv", "txt", "md"],
accept_multiple_files=True,
help="Upload contract, payment application, certificate, schedule of values, change orders, waivers, and supporting records.",
)
run_button = st.button(
"โถ Run payment review",
type="primary",
use_container_width=True,
disabled=not uploaded_files,
)
st.divider()
st.caption("Model")
st.code("Groq โข openai/gpt-oss-120b", language="text")
st.caption("Orchestration: CrewAI โข UI: Streamlit")
# ---------- Main workspace ----------
left, right = st.columns([1.55, 1], gap="large")
with left:
st.subheader("Review workspace")
if not uploaded_files:
st.info("Upload the payment package in the sidebar to begin.")
else:
cols = st.columns(3)
cols[0].markdown(
f'<div class="kpi"><div class="label">Documents</div><div class="value">{len(uploaded_files)}</div></div>',
unsafe_allow_html=True,
)
total_mb = sum(f.size for f in uploaded_files) / (1024 * 1024)
cols[1].markdown(
f'<div class="kpi"><div class="label">Package size</div><div class="value">{total_mb:.1f} MB</div></div>',
unsafe_allow_html=True,
)
cols[2].markdown(
'<div class="kpi"><div class="label">Decision authority</div><div class="value">Human</div></div>',
unsafe_allow_html=True,
)
with st.expander("View uploaded files", expanded=False):
for file in uploaded_files:
st.write(f"โข {file.name}")
result_container = st.container()
with right:
st.subheader("Agent activity")
activity = st.empty()
activity.markdown("โช Waiting for a review run.")
progress_box = st.empty()
# ---------- Run ----------
if run_button:
if not st.secrets.get("GROQ_API_KEY"):
st.error("GROQ_API_KEY is missing. Add it under Streamlit โ App settings โ Secrets.")
st.stop()
run_dir = save_uploads(uploaded_files)
results = []
def update_status(name: str, state: str, progress: int):
status_panel(activity, name, state, progress)
try:
with st.spinner("AI workforce is reviewing the payment package..."):
results = run_payment_review(
run_dir,
project_context or "No additional project context was supplied.",
update_status,
)
st.session_state["review_results"] = {
item.key: {"name": item.name, "result": item.result}
for item in results
}
st.session_state["run_dir"] = run_dir
st.success("Automated review completed. Human decision gate is now active.")
except Exception as exc:
st.error(f"Review stopped safely: {exc}")
st.caption(
"No payment action was executed. Check the agent activity and deployment logs."
)
# ---------- Results ----------
stored = st.session_state.get("review_results", {})
if stored:
st.divider()
st.subheader("Agent reports")
tabs = st.tabs(
[
"๐ Intake",
"๐ Contract",
"๐ Valuation",
"๐งฎ Payment",
"๐ก๏ธ Risk",
"๐งโ๐ผ Human Brief",
]
)
keys = ["intake", "contract", "valuation", "payment", "risk", "review"]
for tab, key in zip(tabs, keys):
with tab:
report = stored.get(key)
if report:
st.markdown(report["result"])
else:
st.info("No report was produced for this stage.")
st.divider()
st.markdown(
"""
<div class="human-gate">
<h3>๐งโโ๏ธ Human decision gate</h3>
<p>These controls are deliberately outside the agent workflow. Selecting an option below records a human workflow decision only; it does not send a payment, modify an ERP, or authorize a bank transfer.</p>
</div>
""",
unsafe_allow_html=True,
)
decision = st.radio(
"Human reviewer decision",
[
"Pending human decision",
"Approve for next internal process",
"Reject / return for correction",
"Request additional evidence",
],
horizontal=True,
)
reviewer_note = st.text_area(
"Reviewer note",
placeholder="Explain the human decision or the evidence still required.",
height=100,
)
if st.button("Record human decision", type="primary"):
st.session_state["human_decision"] = {
"decision": decision,
"note": reviewer_note,
}
st.success("Human decision recorded in the current Streamlit session.")
if st.session_state.get("human_decision"):
st.json(st.session_state["human_decision"])
st.download_button(
"Download review bundle (JSON)",
data=json.dumps(
{
"agents": stored,
"human_decision": st.session_state.get("human_decision"),
},
indent=2,
),
file_name="buildpay_review_bundle.json",
mime="application/json",
use_container_width=True,
)