Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -281,6 +281,17 @@ def clarityops_reply(user_msg, history, tz, uploaded_files_paths, awaiting_answe
|
|
| 281 |
if cols:
|
| 282 |
return history + [(user_msg, "Here are the column names from your most recent CSV upload:\n\n- " + "\n- ".join(cols))], awaiting_answers
|
| 283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
# Session retrieval to enrich the system preamble
|
| 285 |
session_snips = "\n---\n".join(_session_rag.retrieve(
|
| 286 |
"diabetes screening Indigenous Métis mobile program cost throughput outcomes logistics",
|
|
@@ -297,11 +308,14 @@ def clarityops_reply(user_msg, history, tz, uploaded_files_paths, awaiting_answe
|
|
| 297 |
mdsi_extra = _mdsi_block() if ("diabetes" in user_lower or "mdsi" in user_lower or "mobile screening" in user_lower) else ""
|
| 298 |
|
| 299 |
scenario_block = safe_in if len((safe_in or "")) > 0 else ""
|
|
|
|
|
|
|
|
|
|
| 300 |
system_preamble = build_system_preamble(
|
| 301 |
snapshot=snapshot,
|
| 302 |
policy_context=policy_context,
|
| 303 |
computed_numbers=computed,
|
| 304 |
-
scenario_text=
|
| 305 |
session_snips=session_snips
|
| 306 |
)
|
| 307 |
|
|
@@ -427,7 +441,6 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 427 |
state_uploaded = gr.State(value=[])
|
| 428 |
state_awaiting = gr.State(value=False) # False -> Phase 1 next; True -> awaiting answers for Phase 2
|
| 429 |
|
| 430 |
-
# ---- Uploads
|
| 431 |
def _store_uploads(files, current):
|
| 432 |
paths = []
|
| 433 |
for f in (files or []):
|
|
@@ -436,11 +449,9 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 436 |
|
| 437 |
uploads.change(fn=_store_uploads, inputs=[uploads, state_uploaded], outputs=state_uploaded)
|
| 438 |
|
| 439 |
-
# ---- Core send (used by both hero input and chat input)
|
| 440 |
def _on_send(user_msg, history, up_paths, awaiting):
|
| 441 |
try:
|
| 442 |
if not user_msg or not user_msg.strip():
|
| 443 |
-
# no toggle on empty
|
| 444 |
return history, "", history, awaiting
|
| 445 |
new_history, new_awaiting = clarityops_reply(
|
| 446 |
user_msg.strip(), history or [], None, up_paths or [], awaiting_answers=awaiting
|
|
@@ -453,7 +464,6 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 453 |
new_hist = (history or []) + [(user_msg or "", err)]
|
| 454 |
return new_hist, "", new_hist, awaiting
|
| 455 |
|
| 456 |
-
# ---- Hero -> App transition + first send
|
| 457 |
def _hero_start(user_msg, history, up_paths, awaiting):
|
| 458 |
chat_o, msg_o, hist_o, await_o = _on_send(user_msg, history, up_paths, awaiting)
|
| 459 |
return (
|
|
@@ -476,7 +486,6 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 476 |
concurrency_limit=2, queue=True
|
| 477 |
)
|
| 478 |
|
| 479 |
-
# ---- Normal chat interactions after hero is gone
|
| 480 |
send.click(_on_send, inputs=[msg, state_history, state_uploaded, state_awaiting],
|
| 481 |
outputs=[chat, msg, state_history, state_awaiting],
|
| 482 |
concurrency_limit=2, queue=True)
|
|
@@ -485,7 +494,6 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 485 |
concurrency_limit=2, queue=True)
|
| 486 |
|
| 487 |
def _on_clear():
|
| 488 |
-
# reset to fresh hero screen
|
| 489 |
return (
|
| 490 |
[], "", [], False,
|
| 491 |
gr.update(visible=True), # show hero
|
|
|
|
| 281 |
if cols:
|
| 282 |
return history + [(user_msg, "Here are the column names from your most recent CSV upload:\n\n- " + "\n- ".join(cols))], awaiting_answers
|
| 283 |
|
| 284 |
+
# Pull CSV summaries (NEW) and format a concise block for the prompt
|
| 285 |
+
csv_summaries = _session_rag.get_csv_summaries()
|
| 286 |
+
csv_block = ""
|
| 287 |
+
if csv_summaries:
|
| 288 |
+
# Cap to 3 latest digests to keep prompt lean
|
| 289 |
+
lines = ["Uploaded Data Summaries (auto-parsed)"]
|
| 290 |
+
for s in csv_summaries[:3]:
|
| 291 |
+
digest = s.get("digest") or ""
|
| 292 |
+
lines.append(f"- {digest}")
|
| 293 |
+
csv_block = "\n" + "\n".join(lines)
|
| 294 |
+
|
| 295 |
# Session retrieval to enrich the system preamble
|
| 296 |
session_snips = "\n---\n".join(_session_rag.retrieve(
|
| 297 |
"diabetes screening Indigenous Métis mobile program cost throughput outcomes logistics",
|
|
|
|
| 308 |
mdsi_extra = _mdsi_block() if ("diabetes" in user_lower or "mdsi" in user_lower or "mobile screening" in user_lower) else ""
|
| 309 |
|
| 310 |
scenario_block = safe_in if len((safe_in or "")) > 0 else ""
|
| 311 |
+
# Inject the CSV summary digests into the system preamble so both phases see them
|
| 312 |
+
scenario_plus_csv = scenario_block + (("\n\n" + csv_block) if csv_block else "")
|
| 313 |
+
|
| 314 |
system_preamble = build_system_preamble(
|
| 315 |
snapshot=snapshot,
|
| 316 |
policy_context=policy_context,
|
| 317 |
computed_numbers=computed,
|
| 318 |
+
scenario_text=scenario_plus_csv + (f"\n\nExecutive Pre-Computed Blocks:\n{mdsi_extra}" if mdsi_extra else ""),
|
| 319 |
session_snips=session_snips
|
| 320 |
)
|
| 321 |
|
|
|
|
| 441 |
state_uploaded = gr.State(value=[])
|
| 442 |
state_awaiting = gr.State(value=False) # False -> Phase 1 next; True -> awaiting answers for Phase 2
|
| 443 |
|
|
|
|
| 444 |
def _store_uploads(files, current):
|
| 445 |
paths = []
|
| 446 |
for f in (files or []):
|
|
|
|
| 449 |
|
| 450 |
uploads.change(fn=_store_uploads, inputs=[uploads, state_uploaded], outputs=state_uploaded)
|
| 451 |
|
|
|
|
| 452 |
def _on_send(user_msg, history, up_paths, awaiting):
|
| 453 |
try:
|
| 454 |
if not user_msg or not user_msg.strip():
|
|
|
|
| 455 |
return history, "", history, awaiting
|
| 456 |
new_history, new_awaiting = clarityops_reply(
|
| 457 |
user_msg.strip(), history or [], None, up_paths or [], awaiting_answers=awaiting
|
|
|
|
| 464 |
new_hist = (history or []) + [(user_msg or "", err)]
|
| 465 |
return new_hist, "", new_hist, awaiting
|
| 466 |
|
|
|
|
| 467 |
def _hero_start(user_msg, history, up_paths, awaiting):
|
| 468 |
chat_o, msg_o, hist_o, await_o = _on_send(user_msg, history, up_paths, awaiting)
|
| 469 |
return (
|
|
|
|
| 486 |
concurrency_limit=2, queue=True
|
| 487 |
)
|
| 488 |
|
|
|
|
| 489 |
send.click(_on_send, inputs=[msg, state_history, state_uploaded, state_awaiting],
|
| 490 |
outputs=[chat, msg, state_history, state_awaiting],
|
| 491 |
concurrency_limit=2, queue=True)
|
|
|
|
| 494 |
concurrency_limit=2, queue=True)
|
| 495 |
|
| 496 |
def _on_clear():
|
|
|
|
| 497 |
return (
|
| 498 |
[], "", [], False,
|
| 499 |
gr.update(visible=True), # show hero
|