Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -50,6 +50,43 @@ USE_HOSTED_COHERE = bool(COHERE_API_KEY and _HAS_COHERE)
|
|
| 50 |
|
| 51 |
MAX_NEW_TOKENS = int(os.getenv("MAX_NEW_TOKENS", "512"))
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# ---------- Helpers ----------
|
| 54 |
def pick_dtype_and_map():
|
| 55 |
if torch.cuda.is_available():
|
|
@@ -86,7 +123,12 @@ def _iter_user_assistant(history):
|
|
| 86 |
yield u, a
|
| 87 |
|
| 88 |
def _history_to_prompt(message, history):
|
|
|
|
|
|
|
|
|
|
| 89 |
parts = []
|
|
|
|
|
|
|
| 90 |
for u, a in _iter_user_assistant(history):
|
| 91 |
if u: parts.append(f"User: {u}")
|
| 92 |
if a: parts.append(f"Assistant: {a}")
|
|
@@ -151,6 +193,8 @@ def load_local_model():
|
|
| 151 |
|
| 152 |
def build_inputs(tokenizer, message, history):
|
| 153 |
msgs = []
|
|
|
|
|
|
|
| 154 |
for u, a in _iter_user_assistant(history):
|
| 155 |
if u: msgs.append({"role": "user", "content": u})
|
| 156 |
if a: msgs.append({"role": "assistant", "content": a})
|
|
@@ -204,8 +248,13 @@ def _mdsi_block():
|
|
| 204 |
"outcomes_summary": outcomes
|
| 205 |
}, indent=2)
|
| 206 |
|
| 207 |
-
# ---------- Core chat logic
|
| 208 |
-
def clarityops_reply(user_msg, history, tz, uploaded_files_paths):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
try:
|
| 210 |
log_event("user_message", None, {"sizes": {"chars": len(user_msg or "")}})
|
| 211 |
|
|
@@ -213,12 +262,12 @@ def clarityops_reply(user_msg, history, tz, uploaded_files_paths):
|
|
| 213 |
safe_in, blocked_in, reason_in = safety_filter(user_msg, mode="input")
|
| 214 |
if blocked_in:
|
| 215 |
ans = refusal_reply(reason_in)
|
| 216 |
-
return history + [(user_msg, ans)]
|
| 217 |
|
| 218 |
# Identity short-circuit
|
| 219 |
if is_identity_query(safe_in, history):
|
| 220 |
ans = "I am ClarityOps, your strategic decision making AI partner."
|
| 221 |
-
return history + [(user_msg, ans)]
|
| 222 |
|
| 223 |
# Debug slash command: /diag
|
| 224 |
if (safe_in or "").strip().lower().startswith("/diag"):
|
|
@@ -232,14 +281,14 @@ def clarityops_reply(user_msg, history, tz, uploaded_files_paths):
|
|
| 232 |
"Sample retrieved snippets:",
|
| 233 |
*(sample or ["<no snippets>"])
|
| 234 |
]
|
| 235 |
-
return history + [(user_msg, "\n\n".join(msg))]
|
| 236 |
except Exception as e:
|
| 237 |
-
return history + [(user_msg, f"Diag error: {e}")]
|
| 238 |
|
| 239 |
# Ingest uploads: returns chunks + artifacts
|
| 240 |
if uploaded_files_paths:
|
| 241 |
ing = extract_text_from_files(uploaded_files_paths)
|
| 242 |
-
chunks = ing.get("chunks", []) if isinstance(ing, dict) else (
|
| 243 |
artifacts = ing.get("artifacts", []) if isinstance(ing, dict) else []
|
| 244 |
if chunks:
|
| 245 |
_session_rag.add_docs(chunks)
|
|
@@ -251,17 +300,7 @@ def clarityops_reply(user_msg, history, tz, uploaded_files_paths):
|
|
| 251 |
if re.search(r"\b(columns?|headers?)\b", (safe_in or "").lower()):
|
| 252 |
cols = _session_rag.get_latest_csv_columns()
|
| 253 |
if cols:
|
| 254 |
-
return history + [(user_msg, "Here are the column names from your most recent CSV upload:\n\n- " + "\n- ".join(cols))]
|
| 255 |
-
|
| 256 |
-
# Heuristic: scenario mode nudge if a long case study was pasted
|
| 257 |
-
plain = (safe_in or "").strip().lower()
|
| 258 |
-
looks_like_case = ("background" in plain and "objective" in plain) or ("case study" in plain)
|
| 259 |
-
if looks_like_case and len(plain) > 600:
|
| 260 |
-
safe_in += (
|
| 261 |
-
"\n\nPlease analyze the scenario above using the Expected Output Format: "
|
| 262 |
-
"produce structured recommendations, estimates and assumptions, include tables and bullet points, "
|
| 263 |
-
"and explicitly state how uploaded files (CSV/docs) influenced your estimates."
|
| 264 |
-
)
|
| 265 |
|
| 266 |
# Retrieve from session uploads (text chunks)
|
| 267 |
session_snips = "\n---\n".join(_session_rag.retrieve(
|
|
@@ -280,7 +319,7 @@ def clarityops_reply(user_msg, history, tz, uploaded_files_paths):
|
|
| 280 |
user_lower = (safe_in or "").lower()
|
| 281 |
mdsi_extra = _mdsi_block() if ("diabetes" in user_lower or "mdsi" in user_lower or "mobile screening" in user_lower) else ""
|
| 282 |
|
| 283 |
-
scenario_block = safe_in if len(safe_in) >
|
| 284 |
system_preamble = build_system_preamble(
|
| 285 |
snapshot=snapshot,
|
| 286 |
policy_context=policy_context,
|
|
@@ -289,7 +328,22 @@ def clarityops_reply(user_msg, history, tz, uploaded_files_paths):
|
|
| 289 |
session_snips=session_snips
|
| 290 |
)
|
| 291 |
|
| 292 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
# Cohere first
|
| 295 |
out = cohere_chat(augmented_user, history)
|
|
@@ -312,20 +366,30 @@ def clarityops_reply(user_msg, history, tz, uploaded_files_paths):
|
|
| 312 |
if blocked_out:
|
| 313 |
safe_out = refusal_reply(reason_out)
|
| 314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
# Audit (content-free fingerprints)
|
| 316 |
log_event("assistant_reply", None, {
|
| 317 |
**hash_summary("prompt", augmented_user if not PERSIST_CONTENT else ""),
|
| 318 |
**hash_summary("reply", safe_out if not PERSIST_CONTENT else ""),
|
|
|
|
| 319 |
})
|
| 320 |
|
| 321 |
-
return history + [(user_msg, safe_out)]
|
| 322 |
except Exception as e:
|
| 323 |
err = f"Error: {e}"
|
| 324 |
try:
|
| 325 |
traceback.print_exc()
|
| 326 |
except Exception:
|
| 327 |
pass
|
| 328 |
-
return history + [(user_msg, err)]
|
| 329 |
|
| 330 |
# ---------- Theme & CSS ----------
|
| 331 |
theme = gr.themes.Soft(primary_hue="teal", neutral_hue="slate", radius_size=gr.themes.sizes.radius_lg)
|
|
@@ -336,13 +400,36 @@ h1 { color: var(--brand-text); font-weight: 700; font-size: 28px !important; }
|
|
| 336 |
.chatbot header, .chatbot .label, .chatbot .label-wrap, .chatbot .top, .chatbot .header, .chatbot > .wrap > header { display: none !important; }
|
| 337 |
.message.user, .message.bot { background: var(--brand-accent) !important; color: var(--brand-text-light) !important; border-radius: 12px !important; padding: 8px 12px !important; }
|
| 338 |
textarea, input, .gr-input { border-radius: 12px !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
"""
|
| 340 |
|
| 341 |
# ---------- UI (single window; uploads at bottom) ----------
|
| 342 |
with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
| 343 |
gr.Markdown("# ClarityOps Augmented Decision AI")
|
| 344 |
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
with gr.Row():
|
| 348 |
uploads = gr.Files(
|
|
@@ -354,7 +441,7 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 354 |
msg = gr.Textbox(
|
| 355 |
label="",
|
| 356 |
show_label=False,
|
| 357 |
-
placeholder="
|
| 358 |
scale=10
|
| 359 |
)
|
| 360 |
send = gr.Button("Send", scale=1)
|
|
@@ -362,6 +449,7 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 362 |
|
| 363 |
state_history = gr.State(value=[])
|
| 364 |
state_uploaded = gr.State(value=[])
|
|
|
|
| 365 |
|
| 366 |
def _store_uploads(files, current):
|
| 367 |
paths = []
|
|
@@ -371,12 +459,14 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 371 |
|
| 372 |
uploads.change(fn=_store_uploads, inputs=[uploads, state_uploaded], outputs=state_uploaded)
|
| 373 |
|
| 374 |
-
def _on_send(user_msg, history, up_paths):
|
|
|
|
|
|
|
| 375 |
try:
|
| 376 |
if not user_msg or not user_msg.strip():
|
| 377 |
-
return history, "", history
|
| 378 |
-
new_history = clarityops_reply(user_msg.strip(), history or [], None, up_paths or [])
|
| 379 |
-
return new_history, "", new_history
|
| 380 |
except Exception as e:
|
| 381 |
err = f"Error: {e}"
|
| 382 |
try:
|
|
@@ -384,23 +474,24 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 384 |
except Exception:
|
| 385 |
pass
|
| 386 |
new_hist = (history or []) + [(user_msg or "", err)]
|
| 387 |
-
return new_hist, "", new_hist
|
| 388 |
|
| 389 |
-
send.click(_on_send, inputs=[msg, state_history, state_uploaded],
|
| 390 |
-
outputs=[chat, msg, state_history],
|
| 391 |
concurrency_limit=2, queue=True)
|
| 392 |
|
| 393 |
-
msg.submit(_on_send, inputs=[msg, state_history, state_uploaded],
|
| 394 |
-
outputs=[chat, msg, state_history],
|
| 395 |
concurrency_limit=2, queue=True)
|
| 396 |
|
| 397 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 398 |
|
| 399 |
if __name__ == "__main__":
|
| 400 |
port = int(os.environ.get("PORT", "7860"))
|
| 401 |
demo.launch(server_name="0.0.0.0", server_port=port, show_api=False, max_threads=8)
|
| 402 |
|
| 403 |
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
|
|
|
| 50 |
|
| 51 |
MAX_NEW_TOKENS = int(os.getenv("MAX_NEW_TOKENS", "512"))
|
| 52 |
|
| 53 |
+
# ---------- System Master (two-phase, LLM-only behavior) ----------
|
| 54 |
+
SYSTEM_MASTER = """
|
| 55 |
+
SYSTEM ROLE (fixed, always on)
|
| 56 |
+
You are ClarityOps, a medical analytics system that interacts only via this chat.
|
| 57 |
+
|
| 58 |
+
Absolute rules:
|
| 59 |
+
- Use ONLY information provided in this conversation (scenario text + uploaded files).
|
| 60 |
+
- Never invent data. If something required is missing after clarifications, output the literal token: INSUFFICIENT_DATA.
|
| 61 |
+
- Always run in TWO PHASES:
|
| 62 |
+
Phase 1: Ask up to 5 concise clarification questions, grouped by category (Prioritization, Capacity, Cost, Clinical, Recommendations). Then STOP and WAIT.
|
| 63 |
+
Phase 2: After answers are provided, produce the final structured analysis exactly in the required format.
|
| 64 |
+
|
| 65 |
+
Core behavior:
|
| 66 |
+
- Read and synthesize any user-uploaded files (e.g., CSV/XLSX/PDF) relevant to the scenario.
|
| 67 |
+
- Prefer analytics/longitudinal recommendations (risk targeting, follow-up, clustering) over generic ops advice.
|
| 68 |
+
- Show all calculations explicitly for capacity and costs (e.g., “6 teams × 8 clients/day × 60 days = 2,880”).
|
| 69 |
+
- Use correct clinical units and plausible ranges.
|
| 70 |
+
- Include a brief “Provenance” section mapping each key output to scenario text, files, and/or clarified answers.
|
| 71 |
+
|
| 72 |
+
Medical guardrails (always apply):
|
| 73 |
+
- Units: BP in mmHg, A1c in %, BMI in kg/m², Total Cholesterol in mmol/L (or as provided), Percentages in %.
|
| 74 |
+
- Plausible ranges: A1c 3–20 %, SBP 60–250 mmHg, DBP 30–150 mmHg, BMI 10–70 kg/m², Total Chol 2–12 mmol/L.
|
| 75 |
+
- Privacy: avoid PHI; aggregate only; apply small-cell suppression where cohort < 10 (describe at a higher level).
|
| 76 |
+
- When data includes mixed or ambiguous indicators, ask to confirm preferred indicators (e.g., obesity/metabolic syndrome vs self-reported diabetes).
|
| 77 |
+
|
| 78 |
+
Formatting hard rules:
|
| 79 |
+
- Phase 1 output MUST include the header line: “Clarification Questions”
|
| 80 |
+
- Phase 2 output MUST include the header line: “Structured Analysis”
|
| 81 |
+
- Phase 2 MUST follow this exact section order:
|
| 82 |
+
1. Prioritization
|
| 83 |
+
2. Capacity
|
| 84 |
+
3. Cost
|
| 85 |
+
4. Clinical Benefits
|
| 86 |
+
5. ClarityOps Top 3 Recommendations
|
| 87 |
+
(Include a short Provenance block at the end.)
|
| 88 |
+
""".strip()
|
| 89 |
+
|
| 90 |
# ---------- Helpers ----------
|
| 91 |
def pick_dtype_and_map():
|
| 92 |
if torch.cuda.is_available():
|
|
|
|
| 123 |
yield u, a
|
| 124 |
|
| 125 |
def _history_to_prompt(message, history):
|
| 126 |
+
"""
|
| 127 |
+
Build a simple chat-style prompt INCLUDING the System Master preamble.
|
| 128 |
+
"""
|
| 129 |
parts = []
|
| 130 |
+
# system master always first
|
| 131 |
+
parts.append(f"System: {SYSTEM_MASTER}")
|
| 132 |
for u, a in _iter_user_assistant(history):
|
| 133 |
if u: parts.append(f"User: {u}")
|
| 134 |
if a: parts.append(f"Assistant: {a}")
|
|
|
|
| 193 |
|
| 194 |
def build_inputs(tokenizer, message, history):
|
| 195 |
msgs = []
|
| 196 |
+
# Always inject system master into the chat template, if supported
|
| 197 |
+
msgs.append({"role": "system", "content": SYSTEM_MASTER})
|
| 198 |
for u, a in _iter_user_assistant(history):
|
| 199 |
if u: msgs.append({"role": "user", "content": u})
|
| 200 |
if a: msgs.append({"role": "assistant", "content": a})
|
|
|
|
| 248 |
"outcomes_summary": outcomes
|
| 249 |
}, indent=2)
|
| 250 |
|
| 251 |
+
# ---------- Core chat logic with two-phase behavior ----------
|
| 252 |
+
def clarityops_reply(user_msg, history, tz, uploaded_files_paths, awaiting_answers=False):
|
| 253 |
+
"""
|
| 254 |
+
awaiting_answers:
|
| 255 |
+
- False: Phase 1 mode -> generate clarification questions and WAIT
|
| 256 |
+
- True: Phase 2 mode -> consume clarifications and produce structured analysis
|
| 257 |
+
"""
|
| 258 |
try:
|
| 259 |
log_event("user_message", None, {"sizes": {"chars": len(user_msg or "")}})
|
| 260 |
|
|
|
|
| 262 |
safe_in, blocked_in, reason_in = safety_filter(user_msg, mode="input")
|
| 263 |
if blocked_in:
|
| 264 |
ans = refusal_reply(reason_in)
|
| 265 |
+
return history + [(user_msg, ans)], awaiting_answers
|
| 266 |
|
| 267 |
# Identity short-circuit
|
| 268 |
if is_identity_query(safe_in, history):
|
| 269 |
ans = "I am ClarityOps, your strategic decision making AI partner."
|
| 270 |
+
return history + [(user_msg, ans)], awaiting_answers
|
| 271 |
|
| 272 |
# Debug slash command: /diag
|
| 273 |
if (safe_in or "").strip().lower().startswith("/diag"):
|
|
|
|
| 281 |
"Sample retrieved snippets:",
|
| 282 |
*(sample or ["<no snippets>"])
|
| 283 |
]
|
| 284 |
+
return history + [(user_msg, "\n\n".join(msg))], awaiting_answers
|
| 285 |
except Exception as e:
|
| 286 |
+
return history + [(user_msg, f"Diag error: {e}")], awaiting_answers
|
| 287 |
|
| 288 |
# Ingest uploads: returns chunks + artifacts
|
| 289 |
if uploaded_files_paths:
|
| 290 |
ing = extract_text_from_files(uploaded_files_paths)
|
| 291 |
+
chunks = ing.get("chunks", []) if isinstance(ing, dict) else (ing or [])
|
| 292 |
artifacts = ing.get("artifacts", []) if isinstance(ing, dict) else []
|
| 293 |
if chunks:
|
| 294 |
_session_rag.add_docs(chunks)
|
|
|
|
| 300 |
if re.search(r"\b(columns?|headers?)\b", (safe_in or "").lower()):
|
| 301 |
cols = _session_rag.get_latest_csv_columns()
|
| 302 |
if cols:
|
| 303 |
+
return history + [(user_msg, "Here are the column names from your most recent CSV upload:\n\n- " + "\n- ".join(cols))], awaiting_answers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
|
| 305 |
# Retrieve from session uploads (text chunks)
|
| 306 |
session_snips = "\n---\n".join(_session_rag.retrieve(
|
|
|
|
| 319 |
user_lower = (safe_in or "").lower()
|
| 320 |
mdsi_extra = _mdsi_block() if ("diabetes" in user_lower or "mdsi" in user_lower or "mobile screening" in user_lower) else ""
|
| 321 |
|
| 322 |
+
scenario_block = safe_in if len((safe_in or "")) > 0 else ""
|
| 323 |
system_preamble = build_system_preamble(
|
| 324 |
snapshot=snapshot,
|
| 325 |
policy_context=policy_context,
|
|
|
|
| 328 |
session_snips=session_snips
|
| 329 |
)
|
| 330 |
|
| 331 |
+
# Phase-specific instruction appended to the user content
|
| 332 |
+
if not awaiting_answers:
|
| 333 |
+
phase_directive = (
|
| 334 |
+
"\n\n[INSTRUCTION TO MODEL]\n"
|
| 335 |
+
"Produce **Phase 1** only: output a header 'Clarification Questions' and ask up to 5 concise, grouped questions "
|
| 336 |
+
"(Prioritization, Capacity, Cost, Clinical, Recommendations). Then STOP and WAIT.\n"
|
| 337 |
+
)
|
| 338 |
+
else:
|
| 339 |
+
phase_directive = (
|
| 340 |
+
"\n\n[INSTRUCTION TO MODEL]\n"
|
| 341 |
+
"Produce **Phase 2** only: output a header 'Structured Analysis' and follow the exact section order "
|
| 342 |
+
"(Prioritization, Capacity, Cost, Clinical Benefits, ClarityOps Top 3 Recommendations). "
|
| 343 |
+
"Use uploaded files + the user's latest answers as authoritative. Show calculations, units, and a brief Provenance.\n"
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
augmented_user = SYSTEM_MASTER + "\n\n" + system_preamble + "\n\nUser message:\n" + safe_in + phase_directive
|
| 347 |
|
| 348 |
# Cohere first
|
| 349 |
out = cohere_chat(augmented_user, history)
|
|
|
|
| 366 |
if blocked_out:
|
| 367 |
safe_out = refusal_reply(reason_out)
|
| 368 |
|
| 369 |
+
# Decide next state:
|
| 370 |
+
# If we just asked clarifications, set awaiting_answers=True.
|
| 371 |
+
# If we just produced structured analysis, set awaiting_answers=False.
|
| 372 |
+
new_awaiting = awaiting_answers
|
| 373 |
+
if not awaiting_answers and "clarification questions" in safe_out.lower():
|
| 374 |
+
new_awaiting = True
|
| 375 |
+
elif awaiting_answers and "structured analysis" in safe_out.lower():
|
| 376 |
+
new_awaiting = False
|
| 377 |
+
|
| 378 |
# Audit (content-free fingerprints)
|
| 379 |
log_event("assistant_reply", None, {
|
| 380 |
**hash_summary("prompt", augmented_user if not PERSIST_CONTENT else ""),
|
| 381 |
**hash_summary("reply", safe_out if not PERSIST_CONTENT else ""),
|
| 382 |
+
"awaiting_next_phase": new_awaiting
|
| 383 |
})
|
| 384 |
|
| 385 |
+
return history + [(user_msg, safe_out)], new_awaiting
|
| 386 |
except Exception as e:
|
| 387 |
err = f"Error: {e}"
|
| 388 |
try:
|
| 389 |
traceback.print_exc()
|
| 390 |
except Exception:
|
| 391 |
pass
|
| 392 |
+
return history + [(user_msg, err)], awaiting_answers
|
| 393 |
|
| 394 |
# ---------- Theme & CSS ----------
|
| 395 |
theme = gr.themes.Soft(primary_hue="teal", neutral_hue="slate", radius_size=gr.themes.sizes.radius_lg)
|
|
|
|
| 400 |
.chatbot header, .chatbot .label, .chatbot .label-wrap, .chatbot .top, .chatbot .header, .chatbot > .wrap > header { display: none !important; }
|
| 401 |
.message.user, .message.bot { background: var(--brand-accent) !important; color: var(--brand-text-light) !important; border-radius: 12px !important; padding: 8px 12px !important; }
|
| 402 |
textarea, input, .gr-input { border-radius: 12px !important; }
|
| 403 |
+
|
| 404 |
+
/* Centered handshake overlay */
|
| 405 |
+
#handshake-overlay {
|
| 406 |
+
position: absolute;
|
| 407 |
+
z-index: 50;
|
| 408 |
+
top: 50%;
|
| 409 |
+
left: 50%;
|
| 410 |
+
transform: translate(-50%, -50%);
|
| 411 |
+
background: rgba(13, 148, 136, 0.92);
|
| 412 |
+
color: #fff;
|
| 413 |
+
padding: 18px 22px;
|
| 414 |
+
border-radius: 14px;
|
| 415 |
+
font-size: 16px;
|
| 416 |
+
max-width: 720px;
|
| 417 |
+
text-align: center;
|
| 418 |
+
box-shadow: 0 10px 24px rgba(0,0,0,0.2);
|
| 419 |
+
}
|
| 420 |
+
#handshake-overlay.hidden { display: none; }
|
| 421 |
+
#chat-container { position: relative; }
|
| 422 |
"""
|
| 423 |
|
| 424 |
# ---------- UI (single window; uploads at bottom) ----------
|
| 425 |
with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
| 426 |
gr.Markdown("# ClarityOps Augmented Decision AI")
|
| 427 |
|
| 428 |
+
with gr.Column(elem_id="chat-container"):
|
| 429 |
+
chat = gr.Chatbot(label="", show_label=False, height=700)
|
| 430 |
+
handshake = gr.HTML(
|
| 431 |
+
value='<div id="handshake-overlay">ClarityOps loaded. Paste your scenario and attach files. I’ll ask up to 5 clarifications, then produce the structured analysis</div>'
|
| 432 |
+
)
|
| 433 |
|
| 434 |
with gr.Row():
|
| 435 |
uploads = gr.Files(
|
|
|
|
| 441 |
msg = gr.Textbox(
|
| 442 |
label="",
|
| 443 |
show_label=False,
|
| 444 |
+
placeholder="Paste your scenario here (attach files below). ClarityOps will ask clarifications first.",
|
| 445 |
scale=10
|
| 446 |
)
|
| 447 |
send = gr.Button("Send", scale=1)
|
|
|
|
| 449 |
|
| 450 |
state_history = gr.State(value=[])
|
| 451 |
state_uploaded = gr.State(value=[])
|
| 452 |
+
state_awaiting = gr.State(value=False) # False = Phase 1 next; True = awaiting answers for Phase 2
|
| 453 |
|
| 454 |
def _store_uploads(files, current):
|
| 455 |
paths = []
|
|
|
|
| 459 |
|
| 460 |
uploads.change(fn=_store_uploads, inputs=[uploads, state_uploaded], outputs=state_uploaded)
|
| 461 |
|
| 462 |
+
def _on_send(user_msg, history, up_paths, awaiting):
|
| 463 |
+
# Hide handshake on first interaction by returning a class change
|
| 464 |
+
hide_overlay_js = gr.update(value='<div id="handshake-overlay" class="hidden"></div>')
|
| 465 |
try:
|
| 466 |
if not user_msg or not user_msg.strip():
|
| 467 |
+
return history, "", history, awaiting, hide_overlay_js
|
| 468 |
+
new_history, new_awaiting = clarityops_reply(user_msg.strip(), history or [], None, up_paths or [], awaiting_answers=awaiting)
|
| 469 |
+
return new_history, "", new_history, new_awaiting, hide_overlay_js
|
| 470 |
except Exception as e:
|
| 471 |
err = f"Error: {e}"
|
| 472 |
try:
|
|
|
|
| 474 |
except Exception:
|
| 475 |
pass
|
| 476 |
new_hist = (history or []) + [(user_msg or "", err)]
|
| 477 |
+
return new_hist, "", new_hist, awaiting, hide_overlay_js
|
| 478 |
|
| 479 |
+
send.click(_on_send, inputs=[msg, state_history, state_uploaded, state_awaiting],
|
| 480 |
+
outputs=[chat, msg, state_history, state_awaiting, handshake],
|
| 481 |
concurrency_limit=2, queue=True)
|
| 482 |
|
| 483 |
+
msg.submit(_on_send, inputs=[msg, state_history, state_uploaded, state_awaiting],
|
| 484 |
+
outputs=[chat, msg, state_history, state_awaiting, handshake],
|
| 485 |
concurrency_limit=2, queue=True)
|
| 486 |
|
| 487 |
+
def _on_clear():
|
| 488 |
+
# Reset everything, show handshake again
|
| 489 |
+
return [], "", [], False, '<div id="handshake-overlay">ClarityOps loaded. Paste your scenario and attach files. I’ll ask up to 5 clarifications, then produce the structured analysis</div>'
|
| 490 |
+
|
| 491 |
+
clear.click(_on_clear, None, [chat, msg, state_history, state_awaiting, handshake])
|
| 492 |
|
| 493 |
if __name__ == "__main__":
|
| 494 |
port = int(os.environ.get("PORT", "7860"))
|
| 495 |
demo.launch(server_name="0.0.0.0", server_port=port, show_api=False, max_threads=8)
|
| 496 |
|
| 497 |
|
|
|
|
|
|
|
|
|