Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
# app.py
|
| 2 |
-
#
|
| 3 |
# Universal AI Data Analyst with:
|
| 4 |
-
# -
|
| 5 |
-
# -
|
| 6 |
-
# -
|
| 7 |
-
# -
|
| 8 |
-
|
| 9 |
from __future__ import annotations
|
| 10 |
|
| 11 |
import io
|
|
@@ -20,28 +19,16 @@ import gradio as gr
|
|
| 20 |
import pandas as pd
|
| 21 |
import regex as re2
|
| 22 |
import re
|
| 23 |
-
|
| 24 |
from langchain_cohere import ChatCohere # noqa: F401
|
| 25 |
-
|
| 26 |
from settings import (
|
| 27 |
GENERAL_CONVERSATION_PROMPT,
|
| 28 |
COHERE_MODEL_PRIMARY,
|
| 29 |
-
COHERE_TIMEOUT_S,
|
| 30 |
-
USE_OPEN_FALLBACKS
|
| 31 |
)
|
| 32 |
-
from audit_log import log_event
|
| 33 |
-
from privacy import safety_filter, refusal_reply
|
| 34 |
-
from llm_router import cohere_chat, _co_client, cohere_embed
|
| 35 |
-
|
| 36 |
# Try to import optional HIPAA flags; fall back to safe defaults if not defined.
|
| 37 |
try:
|
| 38 |
-
from settings import
|
| 39 |
-
PHI_MODE,
|
| 40 |
-
PERSIST_HISTORY,
|
| 41 |
-
HISTORY_TTL_DAYS,
|
| 42 |
-
REDACT_BEFORE_LLM,
|
| 43 |
-
ALLOW_EXTERNAL_PHI,
|
| 44 |
-
)
|
| 45 |
except Exception:
|
| 46 |
PHI_MODE = False
|
| 47 |
PERSIST_HISTORY = True
|
|
@@ -49,8 +36,11 @@ except Exception:
|
|
| 49 |
REDACT_BEFORE_LLM = False
|
| 50 |
ALLOW_EXTERNAL_PHI = True
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
# ---------------------- Helpers (analysis logic
|
| 54 |
def load_markdown_text(filepath: str) -> str:
|
| 55 |
try:
|
| 56 |
with open(filepath, "r", encoding="utf-8") as f:
|
|
@@ -58,14 +48,12 @@ def load_markdown_text(filepath: str) -> str:
|
|
| 58 |
except FileNotFoundError:
|
| 59 |
return f"**Error:** Document `{os.path.basename(filepath)}` not found."
|
| 60 |
|
| 61 |
-
|
| 62 |
def _sanitize_text(s: str) -> str:
|
| 63 |
if not isinstance(s, str):
|
| 64 |
return s
|
| 65 |
# Remove control characters (except newline and tab)
|
| 66 |
return re2.sub(r"[\p{C}--[\n\t]]+", "", s)
|
| 67 |
|
| 68 |
-
|
| 69 |
# Conservative PHI redaction patterns (only applied if PHI_MODE & REDACT_BEFORE_LLM are enabled)
|
| 70 |
PHI_PATTERNS = [
|
| 71 |
(re.compile(r"\b\d{3}-\d{2}-\d{4}\b"), "[REDACTED_SSN]"),
|
|
@@ -77,7 +65,6 @@ PHI_PATTERNS = [
|
|
| 77 |
(re.compile(r"\b\d{5}(-\d{4})?\b"), "[REDACTED_ZIP]"),
|
| 78 |
]
|
| 79 |
|
| 80 |
-
|
| 81 |
def redact_phi(text: str) -> str:
|
| 82 |
if not isinstance(text, str):
|
| 83 |
return text
|
|
@@ -86,7 +73,6 @@ def redact_phi(text: str) -> str:
|
|
| 86 |
t = pat.sub(repl, t)
|
| 87 |
return t
|
| 88 |
|
| 89 |
-
|
| 90 |
def safe_log(event_name: str, meta: dict | None = None):
|
| 91 |
# Avoid logging raw PHI or payloads
|
| 92 |
try:
|
|
@@ -97,93 +83,46 @@ def safe_log(event_name: str, meta: dict | None = None):
|
|
| 97 |
# Never raise from logging
|
| 98 |
pass
|
| 99 |
|
| 100 |
-
|
| 101 |
def _create_python_script(user_scenario: str, schema_context: str) -> str:
|
| 102 |
-
"""
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
prompt_for_coder = f"""\
|
| 108 |
-
You are an expert
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
{user_scenario}
|
| 112 |
-
--- END SCENARIO ---
|
| 113 |
|
| 114 |
--- DATA SCHEMA ---
|
| 115 |
{schema_context}
|
| 116 |
--- END DATA SCHEMA ---
|
| 117 |
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
**
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
**Step 3: Write the Python Script.**
|
| 127 |
-
Based on your plan, write a complete Python script.
|
| 128 |
-
|
| 129 |
-
CRITICAL SCRIPTING RULES:
|
| 130 |
-
1. **DYNAMIC DATAFRAME IDENTIFICATION:** Your script MUST identify the correct DataFrame by checking for the presence of the columns you mapped in Step 1. Do NOT use hardcoded indices like `dfs[0]`.
|
| 131 |
-
2. **ROBUST SUCCESS CHECK (MOST IMPORTANT TO PREVENT AMBIGUITY ERROR):** After attempting to find a DataFrame, you MUST check for success by comparing the result to `None`. Do NOT use `if not my_dataframe:` as this is ambiguous.
|
| 132 |
-
```python
|
| 133 |
-
# Good, robust code
|
| 134 |
-
def find_df_by_cols(dfs, required_cols):
|
| 135 |
-
for df in dfs:
|
| 136 |
-
if all(col in df.columns for col in required_cols):
|
| 137 |
-
return df
|
| 138 |
-
return None
|
| 139 |
-
|
| 140 |
-
primary_df = find_df_by_cols(dfs, ['user_id', 'transaction_amount'])
|
| 141 |
-
|
| 142 |
-
# This is the correct way to check for failure
|
| 143 |
-
if primary_df is None:
|
| 144 |
-
raise ValueError("Could not find the primary dataframe based on its columns.")
|
| 145 |
-
```
|
| 146 |
-
3. **VERIFY COLUMN EXISTENCE:** Only use columns that you have explicitly identified and mapped.
|
| 147 |
-
4. **NO FILE READING:** The data is already in the `dfs` list.
|
| 148 |
-
5. **STRICTLY JSON OUTPUT:** The script's ONLY output must be a single JSON object.
|
| 149 |
-
6. **ROBUST & GENERIC:** Write robust code that can handle potential missing data (`errors='coerce'`, checking for `None`).
|
| 150 |
-
|
| 151 |
-
Now, provide your response in the following format:
|
| 152 |
-
|
| 153 |
-
**ANALYSIS PLAN:**
|
| 154 |
-
```text
|
| 155 |
-
**1. Concept-to-Column Mapping:**
|
| 156 |
-
- Concept: [e.g., 'Hospitals'] -> Mapped Column: [e.g., `Facility`]
|
| 157 |
-
- Concept: [e.g., 'Surgical Wait Time'] -> Mapped Column: [e.g., `Surgery_Median`]
|
| 158 |
-
|
| 159 |
-
**2. Step-by-Step Analysis:**
|
| 160 |
-
1. **Data Identification:** [e.g., "Define a helper function to find dataframes by checking for key columns..."]
|
| 161 |
-
2. **Data Cleaning:** [e.g., "Convert metric columns to numeric..."]
|
| 162 |
-
3. **Analysis Step A:** [e.g., "Group the primary dataframe by the 'Facility' column and calculate the mean of the 'Surgery_Median' column..."]
|
| 163 |
-
4. ...
|
| 164 |
-
|
| 165 |
-
the final JSON object]
|
| 166 |
-
|
| 167 |
-
# Your complete Python script starts here
|
| 168 |
-
import pandas as pd
|
| 169 |
-
import json
|
| 170 |
-
import re
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
print(json.dumps(final_data_structure, indent=4))```
|
| 176 |
"""
|
| 177 |
generated_text = cohere_chat(prompt_for_coder)
|
| 178 |
-
|
| 179 |
-
match = re2.search(r"PYTHON SCRIPT:\s*```python\n(.*?)```", generated_text, re2.DOTALL)
|
| 180 |
if match:
|
| 181 |
return match.group(1).strip()
|
| 182 |
-
|
| 183 |
-
fallback_match = re2.search(r"```python\n(.*?)```", generated_text, re2.DOTALL)
|
| 184 |
-
if fallback_match:
|
| 185 |
-
return fallback_match.group(1).strip()
|
| 186 |
-
return "print(json.dumps({'error': 'Failed to generate a valid Python script from the plan.'}))"
|
| 187 |
|
| 188 |
|
| 189 |
def _generate_long_report(prompt: str) -> str:
|
|
@@ -203,15 +142,13 @@ def _generate_long_report(prompt: str) -> str:
|
|
| 203 |
|
| 204 |
|
| 205 |
def _generate_final_report(user_scenario: str, raw_data_json: str) -> str:
|
| 206 |
-
"""
|
| 207 |
-
IMPROVED: Generates a professional, structured report from the JSON data.
|
| 208 |
-
The prompt guides the AI to synthesize insights in a standard consulting format,
|
| 209 |
-
ensuring a high level of detail and actionable recommendations.
|
| 210 |
-
"""
|
| 211 |
prompt_for_writer = f"""\
|
| 212 |
-
You are an expert management consultant
|
|
|
|
| 213 |
|
| 214 |
-
|
|
|
|
|
|
|
| 215 |
{user_scenario}
|
| 216 |
--- END SCENARIO ---
|
| 217 |
|
|
@@ -219,37 +156,11 @@ You are an expert management consultant specializing in data-driven strategy. A
|
|
| 219 |
{raw_data_json}
|
| 220 |
--- END RAW DATA ---
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
**### 1. [First Key Finding, e.g., Hospitals with the Longest Wait Times]**
|
| 229 |
-
- Present the relevant data in a Markdown table.
|
| 230 |
-
- Write a short narrative interpreting the data. What does it mean? Are there any outliers? Why might these facilities have long waits (e.g., specialized care, rural location, capacity issues)?
|
| 231 |
-
|
| 232 |
-
**### 2. [Second Key Finding, e.g., Specialties with the Longest Wait Times]**
|
| 233 |
-
- Present the relevant data in a Markdown table.
|
| 234 |
-
- Interpret the findings. Why are these specialties facing delays (e.g., specialist shortages, equipment needs)?
|
| 235 |
-
|
| 236 |
-
**### 3. [Third Key Finding, e.g., Zone-Level Performance]**
|
| 237 |
-
- Present the data in a table, including a comparison to a relevant average or baseline.
|
| 238 |
-
- Analyze the geographic or systemic issues this data reveals.
|
| 239 |
-
|
| 240 |
-
**### 4. [Fourth Key Finding, if applicable, e.g., Geographic Distribution]**
|
| 241 |
-
- Synthesize location data with the wait-time findings.
|
| 242 |
-
- Discuss the implications for patient equity, travel burdens, and access to care.
|
| 243 |
-
|
| 244 |
-
**### 5. Recommendations for Resource Allocation**
|
| 245 |
-
- Provide specific, actionable, and justified recommendations.
|
| 246 |
-
- Structure them by category (e.g., by facility, by specialty, by zone).
|
| 247 |
-
- For each recommendation, provide a clear rationale directly linked to the data findings above (e.g., "Allocate additional resources to Glace Bay Hospital because it is a rural facility in a high-wait zone, suggesting a capacity bottleneck.").
|
| 248 |
-
|
| 249 |
-
**### Data Limitations**
|
| 250 |
-
- Briefly mention any potential limitations of the analysis (e.g., missing data, use of proxies, case severity not included). This adds credibility to the report.
|
| 251 |
-
|
| 252 |
-
Do not just repeat the JSON data. Your value is in interpreting the numbers, connecting the dots between different findings, and providing clear, data-backed strategic advice.
|
| 253 |
"""
|
| 254 |
return _generate_long_report(prompt_for_writer)
|
| 255 |
|
|
@@ -284,7 +195,7 @@ def handle(user_msg: str, files: list, yield_update) -> str:
|
|
| 284 |
file_paths: List[str] = [getattr(f, "name", None) or f for f in (files or [])]
|
| 285 |
|
| 286 |
if file_paths:
|
| 287 |
-
# CSV analysis path
|
| 288 |
dataframes, schema_parts = [], []
|
| 289 |
for i, p in enumerate(file_paths):
|
| 290 |
if p.endswith(".csv"):
|
|
@@ -293,21 +204,8 @@ def handle(user_msg: str, files: list, yield_update) -> str:
|
|
| 293 |
except UnicodeDecodeError:
|
| 294 |
df = pd.read_csv(p, encoding="latin1")
|
| 295 |
dataframes.append(df)
|
| 296 |
-
# --- IMPROVEMENT: ENRICHED SCHEMA CONTEXT ---
|
| 297 |
-
schema_buffer = io.StringIO()
|
| 298 |
-
df.info(buf=schema_buffer)
|
| 299 |
-
schema_info = schema_buffer.getvalue()
|
| 300 |
schema_parts.append(
|
| 301 |
-
f"
|
| 302 |
-
### Head
|
| 303 |
-
{df.head().to_markdown()}
|
| 304 |
-
|
| 305 |
-
### Schema and Data Types
|
| 306 |
-
{schema_info}
|
| 307 |
-
|
| 308 |
-
### Summary Statistics
|
| 309 |
-
{df.describe(include='all').to_markdown()}
|
| 310 |
-
"""
|
| 311 |
)
|
| 312 |
|
| 313 |
if not dataframes:
|
|
@@ -316,13 +214,16 @@ def handle(user_msg: str, files: list, yield_update) -> str:
|
|
| 316 |
schema_context = "\n".join(schema_parts)
|
| 317 |
|
| 318 |
# If external PHI is not allowed, use redacted prompt; otherwise use original
|
| 319 |
-
prompt_for_code = (
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
|
|
|
| 323 |
analysis_script = _create_python_script(prompt_for_code, schema_context)
|
| 324 |
|
| 325 |
-
yield_update("
|
|
|
|
|
|
|
| 326 |
execution_namespace = {"dfs": dataframes, "pd": pd, "re": re, "json": json}
|
| 327 |
output_buffer = io.StringIO()
|
| 328 |
|
|
@@ -336,24 +237,21 @@ def handle(user_msg: str, files: list, yield_update) -> str:
|
|
| 336 |
f"```python\n{analysis_script}\n```"
|
| 337 |
)
|
| 338 |
|
| 339 |
-
yield_update("
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
)
|
| 343 |
final_report = _generate_final_report(writer_input, raw_data_output)
|
| 344 |
return _sanitize_text(final_report)
|
| 345 |
else:
|
| 346 |
# Pure chat path
|
| 347 |
-
chat_input = (
|
| 348 |
-
redacted_in if (PHI_MODE and not ALLOW_EXTERNAL_PHI) else safe_in
|
| 349 |
-
)
|
| 350 |
prompt = f"{GENERAL_CONVERSATION_PROMPT}\n\nUser: {chat_input}\nAssistant:"
|
| 351 |
return _sanitize_text(cohere_chat(prompt) or "How can I help further?")
|
| 352 |
|
| 353 |
except Exception as e:
|
| 354 |
tb = traceback.format_exc()
|
| 355 |
safe_log("app_error", {"err": str(e)})
|
| 356 |
-
return
|
| 357 |
|
| 358 |
|
| 359 |
PRIVACY_POLICY_TEXT = load_markdown_text("privacy_policy.md")
|
|
@@ -361,6 +259,7 @@ TERMS_OF_SERVICE_TEXT = load_markdown_text("terms_of_service.md")
|
|
| 361 |
|
| 362 |
|
| 363 |
# ---------------------- Sleek UI assets (CSS/JS only) ----------------------
|
|
|
|
| 364 |
SLEEK_CSS = """
|
| 365 |
/* Full-bleed, modern look */
|
| 366 |
:root, body, #root, .gradio-container { height: 100%; }
|
|
@@ -441,7 +340,7 @@ function rs_toggle_stt(elemId){
|
|
| 441 |
__rs_rec.onresult = (ev) => {
|
| 442 |
let t = "";
|
| 443 |
for (let i = ev.resultIndex; i < ev.results.length; i++){
|
| 444 |
-
t += ev.results[i]
|
| 445 |
}
|
| 446 |
box.value = (base + " " + t).trim();
|
| 447 |
box.dispatchEvent(new Event("input", { bubbles: true }));
|
|
@@ -454,14 +353,16 @@ function rs_toggle_stt(elemId){
|
|
| 454 |
|
| 455 |
|
| 456 |
# ---------------------- Sleek UI (with fixed State wiring) ----------------------
|
|
|
|
| 457 |
with gr.Blocks(theme=gr.themes.Soft(), css=SLEEK_CSS, fill_width=True) as demo:
|
| 458 |
# Persistent in-memory history component (fixes list/_id error)
|
| 459 |
assessment_history = gr.State([])
|
| 460 |
|
| 461 |
# Header
|
| 462 |
with gr.Row(elem_classes=["header"]):
|
| 463 |
-
gr.Markdown("<h1>Clarity Ops
|
| 464 |
-
pill =
|
|
|
|
| 465 |
gr.Markdown(f"<span class='badge'>{pill}</span>")
|
| 466 |
|
| 467 |
# Main layout
|
|
@@ -483,6 +384,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=SLEEK_CSS, fill_width=True) as demo:
|
|
| 483 |
elem_id="prompt_box",
|
| 484 |
autofocus=True,
|
| 485 |
)
|
|
|
|
| 486 |
with gr.Row(elem_classes=["actions"]):
|
| 487 |
send_btn = gr.Button("鈻讹笍 Run Analysis", variant="primary")
|
| 488 |
clear_btn = gr.Button("馃Ч Clear")
|
|
@@ -491,66 +393,72 @@ with gr.Blocks(theme=gr.themes.Soft(), css=SLEEK_CSS, fill_width=True) as demo:
|
|
| 491 |
gr.Markdown("<div class='voice-hint'>Click Voice to start/stop dictation into the prompt box.</div>")
|
| 492 |
ping_btn = gr.Button("馃攲 Ping Cohere")
|
| 493 |
ping_out = gr.Markdown()
|
|
|
|
| 494 |
gr.Markdown("<div class='hr'></div>")
|
| 495 |
if PHI_MODE:
|
| 496 |
gr.Markdown(
|
| 497 |
"鈿狅笍 **PHI Mode:** History persistence is disabled by default. Avoid unnecessary identifiers."
|
| 498 |
)
|
|
|
|
| 499 |
with gr.Accordion("Privacy & Terms", open=False):
|
| 500 |
gr.Markdown(PRIVACY_POLICY_TEXT)
|
| 501 |
gr.Markdown("<div class='hr'></div>")
|
| 502 |
gr.Markdown(TERMS_OF_SERVICE_TEXT)
|
|
|
|
| 503 |
# Right panel
|
| 504 |
with gr.Column(elem_classes=["right"]):
|
| 505 |
with gr.Tabs(elem_classes=["tabs"]):
|
| 506 |
with gr.TabItem("Current Assessment", id=0, elem_classes=["tabitem"]):
|
| 507 |
with gr.Column(elem_id="chatbot_container"):
|
| 508 |
-
chat_history_output = gr.Chatbot(
|
| 509 |
-
label="Analysis Output", type="messages"
|
| 510 |
-
)
|
| 511 |
with gr.TabItem("Assessment History", id=1, elem_classes=["tabitem"]):
|
| 512 |
gr.Markdown("### Review Past Assessments")
|
| 513 |
-
history_dropdown = gr.Dropdown(
|
| 514 |
-
label="Select an assessment to review", choices=[]
|
| 515 |
-
)
|
| 516 |
history_display = gr.Markdown(label="Selected Assessment Details")
|
| 517 |
|
| 518 |
# Inject voice-to-text helper
|
| 519 |
gr.HTML(VOICE_STT_HTML)
|
| 520 |
|
| 521 |
# --------- Event logic (unchanged analysis flow) ----------
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
):
|
| 525 |
if not prompt:
|
| 526 |
gr.Warning("Please enter a prompt.")
|
| 527 |
yield chat_history_list, history_state_list, gr.update()
|
| 528 |
return
|
| 529 |
|
|
|
|
| 530 |
chat_with_user_msg = _append_msg(chat_history_list, "user", prompt)
|
| 531 |
|
| 532 |
# Optional progress callback (not streaming in this UI)
|
| 533 |
def dummy_update(message: str):
|
| 534 |
pass
|
| 535 |
|
|
|
|
| 536 |
thinking_message = _append_msg(
|
| 537 |
chat_with_user_msg,
|
| 538 |
"assistant",
|
| 539 |
-
"
|
|
|
|
|
|
|
| 540 |
)
|
| 541 |
yield thinking_message, history_state_list, gr.update()
|
| 542 |
|
|
|
|
| 543 |
ai_response_text = handle(prompt, files, dummy_update)
|
| 544 |
|
|
|
|
| 545 |
final_chat = _append_msg(chat_with_user_msg, "assistant", ai_response_text)
|
| 546 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 547 |
|
|
|
|
| 548 |
file_names: List[str] = []
|
| 549 |
if files:
|
| 550 |
file_names = [
|
| 551 |
os.path.basename(f.name if hasattr(f, "name") else f) for f in files
|
| 552 |
]
|
| 553 |
|
|
|
|
| 554 |
new_entry = {
|
| 555 |
"id": timestamp,
|
| 556 |
"prompt": prompt,
|
|
@@ -559,17 +467,13 @@ with gr.Blocks(theme=gr.themes.Soft(), css=SLEEK_CSS, fill_width=True) as demo:
|
|
| 559 |
"chat_history": final_chat,
|
| 560 |
}
|
| 561 |
|
|
|
|
| 562 |
if PERSIST_HISTORY and (not PHI_MODE or (PHI_MODE and HISTORY_TTL_DAYS > 0)):
|
| 563 |
-
updated_history: List[Dict[str, Any]] = (history_state_list or []) + [
|
| 564 |
-
new_entry
|
| 565 |
-
]
|
| 566 |
else:
|
| 567 |
updated_history = history_state_list or []
|
| 568 |
|
| 569 |
-
history_labels = [
|
| 570 |
-
f"{item['id']} - {item['prompt'][:40]}..."
|
| 571 |
-
for item in updated_history
|
| 572 |
-
]
|
| 573 |
|
| 574 |
yield final_chat, updated_history, gr.update(choices=history_labels)
|
| 575 |
|
|
@@ -577,7 +481,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=SLEEK_CSS, fill_width=True) as demo:
|
|
| 577 |
if not selection or not history_state_list:
|
| 578 |
return ""
|
| 579 |
try:
|
| 580 |
-
selected_id = selection.split(" - ", 1)
|
| 581 |
except Exception:
|
| 582 |
selected_id = selection
|
| 583 |
|
|
@@ -633,7 +537,5 @@ with gr.Blocks(theme=gr.themes.Soft(), css=SLEEK_CSS, fill_width=True) as demo:
|
|
| 633 |
|
| 634 |
if __name__ == "__main__":
|
| 635 |
if not os.getenv("COHERE_API_KEY"):
|
| 636 |
-
print(
|
| 637 |
-
"馃敶 COHERE_API_KEY environment variable not set. Application may not function correctly."
|
| 638 |
-
)
|
| 639 |
demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))
|
|
|
|
| 1 |
# app.py
|
|
|
|
| 2 |
# Universal AI Data Analyst with:
|
| 3 |
+
# - Unchanged analysis & assessment logic
|
| 4 |
+
# - Fixed Gradio event wiring (uses gr.State for history)
|
| 5 |
+
# - Triple-quoted progress strings (no unterminated literals)
|
| 6 |
+
# - Sleek full-width UI and Voice-to-Text (browser Web Speech API)
|
| 7 |
+
# - Optional HIPAA flags (fallback defaults if not present in settings.py)
|
| 8 |
from __future__ import annotations
|
| 9 |
|
| 10 |
import io
|
|
|
|
| 19 |
import pandas as pd
|
| 20 |
import regex as re2
|
| 21 |
import re
|
|
|
|
| 22 |
from langchain_cohere import ChatCohere # noqa: F401
|
|
|
|
| 23 |
from settings import (
|
| 24 |
GENERAL_CONVERSATION_PROMPT,
|
| 25 |
COHERE_MODEL_PRIMARY,
|
| 26 |
+
COHERE_TIMEOUT_S, # noqa: F401
|
| 27 |
+
USE_OPEN_FALLBACKS # noqa: F401
|
| 28 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# Try to import optional HIPAA flags; fall back to safe defaults if not defined.
|
| 30 |
try:
|
| 31 |
+
from settings import PHI_MODE, PERSIST_HISTORY, HISTORY_TTL_DAYS, REDACT_BEFORE_LLM, ALLOW_EXTERNAL_PHI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception:
|
| 33 |
PHI_MODE = False
|
| 34 |
PERSIST_HISTORY = True
|
|
|
|
| 36 |
REDACT_BEFORE_LLM = False
|
| 37 |
ALLOW_EXTERNAL_PHI = True
|
| 38 |
|
| 39 |
+
from audit_log import log_event
|
| 40 |
+
from privacy import safety_filter, refusal_reply
|
| 41 |
+
from llm_router import cohere_chat, _co_client, cohere_embed
|
| 42 |
|
| 43 |
+
# ---------------------- Helpers (analysis logic unchanged) ----------------------
|
| 44 |
def load_markdown_text(filepath: str) -> str:
|
| 45 |
try:
|
| 46 |
with open(filepath, "r", encoding="utf-8") as f:
|
|
|
|
| 48 |
except FileNotFoundError:
|
| 49 |
return f"**Error:** Document `{os.path.basename(filepath)}` not found."
|
| 50 |
|
|
|
|
| 51 |
def _sanitize_text(s: str) -> str:
|
| 52 |
if not isinstance(s, str):
|
| 53 |
return s
|
| 54 |
# Remove control characters (except newline and tab)
|
| 55 |
return re2.sub(r"[\p{C}--[\n\t]]+", "", s)
|
| 56 |
|
|
|
|
| 57 |
# Conservative PHI redaction patterns (only applied if PHI_MODE & REDACT_BEFORE_LLM are enabled)
|
| 58 |
PHI_PATTERNS = [
|
| 59 |
(re.compile(r"\b\d{3}-\d{2}-\d{4}\b"), "[REDACTED_SSN]"),
|
|
|
|
| 65 |
(re.compile(r"\b\d{5}(-\d{4})?\b"), "[REDACTED_ZIP]"),
|
| 66 |
]
|
| 67 |
|
|
|
|
| 68 |
def redact_phi(text: str) -> str:
|
| 69 |
if not isinstance(text, str):
|
| 70 |
return text
|
|
|
|
| 73 |
t = pat.sub(repl, t)
|
| 74 |
return t
|
| 75 |
|
|
|
|
| 76 |
def safe_log(event_name: str, meta: dict | None = None):
|
| 77 |
# Avoid logging raw PHI or payloads
|
| 78 |
try:
|
|
|
|
| 83 |
# Never raise from logging
|
| 84 |
pass
|
| 85 |
|
|
|
|
| 86 |
def _create_python_script(user_scenario: str, schema_context: str) -> str:
|
| 87 |
+
EXPERT_ANALYTICAL_GUIDELINES = """
|
| 88 |
+
--- EXPERT ANALYTICAL GUIDELINES ---
|
| 89 |
+
When writing your script, you MUST follow these expert business rules:
|
| 90 |
+
1. **Linking Datasets Rule:** If you need to connect facilities to health zones when the 'zone' column is not in the facility list,
|
| 91 |
+
you must first identify the high-priority zone from the beds data, then find the major city (by facility count) in the facility list,
|
| 92 |
+
and *then* assess that city's capacity. Do not try to filter the facility list by a 'zone' column if it does not exist in the schema.
|
| 93 |
+
2. **Prioritization Rule:** To prioritize locations, you MUST combine the most recent population data with specific high-risk health indicators
|
| 94 |
+
to create a multi-factor risk score.
|
| 95 |
+
3. **Capacity Calculation Rule:** For capacity over a 3-month window, assume **60 working days**.
|
| 96 |
+
4. **Cost Calculation Rule:** Sum 'Startup cost' and 'Ongoing cost' per person before multiplying.
|
| 97 |
+
"""
|
| 98 |
prompt_for_coder = f"""\
|
| 99 |
+
You are an expert Python data scientist. Your job is to write a script to extract the data needed to answer the user's request.
|
| 100 |
+
You have dataframes in a list `dfs`.
|
| 101 |
|
| 102 |
+
{EXPERT_ANALYTICAL_GUIDELINES}
|
|
|
|
|
|
|
| 103 |
|
| 104 |
--- DATA SCHEMA ---
|
| 105 |
{schema_context}
|
| 106 |
--- END DATA SCHEMA ---
|
| 107 |
|
| 108 |
+
CRITICAL RULES:
|
| 109 |
+
1. **DO NOT READ FILES:** You MUST NOT include `pd.read_csv`. The data is ALREADY loaded in the `dfs` variable. You MUST use this variable. Failure to do so will cause a fatal error.
|
| 110 |
+
2. **JSON OUTPUT ONLY:** Your script's ONLY output must be a single JSON object printed to stdout containing the raw data findings.
|
| 111 |
+
3. **BE PRECISE:** Use the exact, case-sensitive column names from the schema and robustly clean strings (`re.sub()`) before converting to numbers.
|
| 112 |
+
4. **JSON SERIALIZATION:** Before adding data to your final dictionary for JSON conversion, you MUST convert any pandas-specific types (like `int64`) to standard Python types using `.item()` for single values or `.tolist()` for lists.
|
| 113 |
+
|
| 114 |
+
--- USER'S SCENARIO ---
|
| 115 |
+
{user_scenario}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
--- PYTHON SCRIPT ---
|
| 118 |
+
Now, write the complete Python script that performs the analysis and prints a single, serializable JSON object.
|
| 119 |
+
```python
|
|
|
|
| 120 |
"""
|
| 121 |
generated_text = cohere_chat(prompt_for_coder)
|
| 122 |
+
match = re2.search(r"```python\n(.*?)```", generated_text, re2.DOTALL)
|
|
|
|
| 123 |
if match:
|
| 124 |
return match.group(1).strip()
|
| 125 |
+
return "print(json.dumps({'error': 'Failed to generate a valid Python script.'}))"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
|
| 128 |
def _generate_long_report(prompt: str) -> str:
|
|
|
|
| 142 |
|
| 143 |
|
| 144 |
def _generate_final_report(user_scenario: str, raw_data_json: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
prompt_for_writer = f"""\
|
| 146 |
+
You are an expert management consultant and data analyst.
|
| 147 |
+
A data science script has run to extract key findings. You have the user's original request and the raw JSON data.
|
| 148 |
|
| 149 |
+
Your task is to synthesize these raw findings into a single, comprehensive, and professional report that directly answers all of the user's questions with detailed justifications.
|
| 150 |
+
|
| 151 |
+
--- USER'S ORIGINAL SCENARIO & DELIVERABLES ---
|
| 152 |
{user_scenario}
|
| 153 |
--- END SCENARIO ---
|
| 154 |
|
|
|
|
| 156 |
{raw_data_json}
|
| 157 |
--- END RAW DATA ---
|
| 158 |
|
| 159 |
+
Now, write the final, polished report. The report MUST:
|
| 160 |
+
1. Follow the "Expected Output Format" requested by the user.
|
| 161 |
+
2. Use tables, bullet points, and DETAILED narrative justifications for each recommendation.
|
| 162 |
+
3. Synthesize the raw data into actionable insights. Do not just copy the raw numbers; interpret them.
|
| 163 |
+
4. Ensure you fully address ALL evaluation questions, especially the final recommendations.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
"""
|
| 165 |
return _generate_long_report(prompt_for_writer)
|
| 166 |
|
|
|
|
| 195 |
file_paths: List[str] = [getattr(f, "name", None) or f for f in (files or [])]
|
| 196 |
|
| 197 |
if file_paths:
|
| 198 |
+
# CSV analysis path (unchanged)
|
| 199 |
dataframes, schema_parts = [], []
|
| 200 |
for i, p in enumerate(file_paths):
|
| 201 |
if p.endswith(".csv"):
|
|
|
|
| 204 |
except UnicodeDecodeError:
|
| 205 |
df = pd.read_csv(p, encoding="latin1")
|
| 206 |
dataframes.append(df)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
schema_parts.append(
|
| 208 |
+
f"DataFrame `dfs[{i}]` (`{os.path.basename(p)}`):\n{df.head().to_markdown()}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
)
|
| 210 |
|
| 211 |
if not dataframes:
|
|
|
|
| 214 |
schema_context = "\n".join(schema_parts)
|
| 215 |
|
| 216 |
# If external PHI is not allowed, use redacted prompt; otherwise use original
|
| 217 |
+
prompt_for_code = redacted_in if (PHI_MODE and not ALLOW_EXTERNAL_PHI) else safe_in
|
| 218 |
+
|
| 219 |
+
yield_update("""```
|
| 220 |
+
馃 Generating aligned analysis script...
|
| 221 |
+
```""")
|
| 222 |
analysis_script = _create_python_script(prompt_for_code, schema_context)
|
| 223 |
|
| 224 |
+
yield_update("""```
|
| 225 |
+
鈿欙笍 Executing script to extract raw data...
|
| 226 |
+
```""")
|
| 227 |
execution_namespace = {"dfs": dataframes, "pd": pd, "re": re, "json": json}
|
| 228 |
output_buffer = io.StringIO()
|
| 229 |
|
|
|
|
| 237 |
f"```python\n{analysis_script}\n```"
|
| 238 |
)
|
| 239 |
|
| 240 |
+
yield_update("""```
|
| 241 |
+
鉁嶏笍 Synthesizing final comprehensive report...```""")
|
| 242 |
+
writer_input = redacted_in if (PHI_MODE and not ALLOW_EXTERNAL_PHI) else safe_in
|
|
|
|
| 243 |
final_report = _generate_final_report(writer_input, raw_data_output)
|
| 244 |
return _sanitize_text(final_report)
|
| 245 |
else:
|
| 246 |
# Pure chat path
|
| 247 |
+
chat_input = redacted_in if (PHI_MODE and not ALLOW_EXTERNAL_PHI) else safe_in
|
|
|
|
|
|
|
| 248 |
prompt = f"{GENERAL_CONVERSATION_PROMPT}\n\nUser: {chat_input}\nAssistant:"
|
| 249 |
return _sanitize_text(cohere_chat(prompt) or "How can I help further?")
|
| 250 |
|
| 251 |
except Exception as e:
|
| 252 |
tb = traceback.format_exc()
|
| 253 |
safe_log("app_error", {"err": str(e)})
|
| 254 |
+
return "A critical error occurred. Please contact your administrator." if PHI_MODE else f"A critical error occurred: {e}"
|
| 255 |
|
| 256 |
|
| 257 |
PRIVACY_POLICY_TEXT = load_markdown_text("privacy_policy.md")
|
|
|
|
| 259 |
|
| 260 |
|
| 261 |
# ---------------------- Sleek UI assets (CSS/JS only) ----------------------
|
| 262 |
+
|
| 263 |
SLEEK_CSS = """
|
| 264 |
/* Full-bleed, modern look */
|
| 265 |
:root, body, #root, .gradio-container { height: 100%; }
|
|
|
|
| 340 |
__rs_rec.onresult = (ev) => {
|
| 341 |
let t = "";
|
| 342 |
for (let i = ev.resultIndex; i < ev.results.length; i++){
|
| 343 |
+
t += ev.results[i].transcript;
|
| 344 |
}
|
| 345 |
box.value = (base + " " + t).trim();
|
| 346 |
box.dispatchEvent(new Event("input", { bubbles: true }));
|
|
|
|
| 353 |
|
| 354 |
|
| 355 |
# ---------------------- Sleek UI (with fixed State wiring) ----------------------
|
| 356 |
+
|
| 357 |
with gr.Blocks(theme=gr.themes.Soft(), css=SLEEK_CSS, fill_width=True) as demo:
|
| 358 |
# Persistent in-memory history component (fixes list/_id error)
|
| 359 |
assessment_history = gr.State([])
|
| 360 |
|
| 361 |
# Header
|
| 362 |
with gr.Row(elem_classes=["header"]):
|
| 363 |
+
gr.Markdown("<h1>Clarity Ops Augemented Decision Support</h1>")
|
| 364 |
+
pill = "PHI Mode ON 路 history off" if (PHI_MODE and not PERSIST_HISTORY) else \
|
| 365 |
+
"PHI Mode ON" if PHI_MODE else "PHI Mode OFF"
|
| 366 |
gr.Markdown(f"<span class='badge'>{pill}</span>")
|
| 367 |
|
| 368 |
# Main layout
|
|
|
|
| 384 |
elem_id="prompt_box",
|
| 385 |
autofocus=True,
|
| 386 |
)
|
| 387 |
+
|
| 388 |
with gr.Row(elem_classes=["actions"]):
|
| 389 |
send_btn = gr.Button("鈻讹笍 Run Analysis", variant="primary")
|
| 390 |
clear_btn = gr.Button("馃Ч Clear")
|
|
|
|
| 393 |
gr.Markdown("<div class='voice-hint'>Click Voice to start/stop dictation into the prompt box.</div>")
|
| 394 |
ping_btn = gr.Button("馃攲 Ping Cohere")
|
| 395 |
ping_out = gr.Markdown()
|
| 396 |
+
|
| 397 |
gr.Markdown("<div class='hr'></div>")
|
| 398 |
if PHI_MODE:
|
| 399 |
gr.Markdown(
|
| 400 |
"鈿狅笍 **PHI Mode:** History persistence is disabled by default. Avoid unnecessary identifiers."
|
| 401 |
)
|
| 402 |
+
|
| 403 |
with gr.Accordion("Privacy & Terms", open=False):
|
| 404 |
gr.Markdown(PRIVACY_POLICY_TEXT)
|
| 405 |
gr.Markdown("<div class='hr'></div>")
|
| 406 |
gr.Markdown(TERMS_OF_SERVICE_TEXT)
|
| 407 |
+
|
| 408 |
# Right panel
|
| 409 |
with gr.Column(elem_classes=["right"]):
|
| 410 |
with gr.Tabs(elem_classes=["tabs"]):
|
| 411 |
with gr.TabItem("Current Assessment", id=0, elem_classes=["tabitem"]):
|
| 412 |
with gr.Column(elem_id="chatbot_container"):
|
| 413 |
+
chat_history_output = gr.Chatbot(label="Analysis Output", type="messages")
|
|
|
|
|
|
|
| 414 |
with gr.TabItem("Assessment History", id=1, elem_classes=["tabitem"]):
|
| 415 |
gr.Markdown("### Review Past Assessments")
|
| 416 |
+
history_dropdown = gr.Dropdown(label="Select an assessment to review", choices=[])
|
|
|
|
|
|
|
| 417 |
history_display = gr.Markdown(label="Selected Assessment Details")
|
| 418 |
|
| 419 |
# Inject voice-to-text helper
|
| 420 |
gr.HTML(VOICE_STT_HTML)
|
| 421 |
|
| 422 |
# --------- Event logic (unchanged analysis flow) ----------
|
| 423 |
+
|
| 424 |
+
def run_analysis_wrapper(prompt, files, chat_history_list, history_state_list):
|
|
|
|
| 425 |
if not prompt:
|
| 426 |
gr.Warning("Please enter a prompt.")
|
| 427 |
yield chat_history_list, history_state_list, gr.update()
|
| 428 |
return
|
| 429 |
|
| 430 |
+
# Append user's message
|
| 431 |
chat_with_user_msg = _append_msg(chat_history_list, "user", prompt)
|
| 432 |
|
| 433 |
# Optional progress callback (not streaming in this UI)
|
| 434 |
def dummy_update(message: str):
|
| 435 |
pass
|
| 436 |
|
| 437 |
+
# Thinking bubble
|
| 438 |
thinking_message = _append_msg(
|
| 439 |
chat_with_user_msg,
|
| 440 |
"assistant",
|
| 441 |
+
"""```
|
| 442 |
+
馃 Generating and executing analysis... Please wait.
|
| 443 |
+
```""",
|
| 444 |
)
|
| 445 |
yield thinking_message, history_state_list, gr.update()
|
| 446 |
|
| 447 |
+
# Run analysis/chat
|
| 448 |
ai_response_text = handle(prompt, files, dummy_update)
|
| 449 |
|
| 450 |
+
# Append final assistant response
|
| 451 |
final_chat = _append_msg(chat_with_user_msg, "assistant", ai_response_text)
|
| 452 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 453 |
|
| 454 |
+
# Capture filenames (if any)
|
| 455 |
file_names: List[str] = []
|
| 456 |
if files:
|
| 457 |
file_names = [
|
| 458 |
os.path.basename(f.name if hasattr(f, "name") else f) for f in files
|
| 459 |
]
|
| 460 |
|
| 461 |
+
# Build history record
|
| 462 |
new_entry = {
|
| 463 |
"id": timestamp,
|
| 464 |
"prompt": prompt,
|
|
|
|
| 467 |
"chat_history": final_chat,
|
| 468 |
}
|
| 469 |
|
| 470 |
+
# Respect PHI/history flags
|
| 471 |
if PERSIST_HISTORY and (not PHI_MODE or (PHI_MODE and HISTORY_TTL_DAYS > 0)):
|
| 472 |
+
updated_history: List[Dict[str, Any]] = (history_state_list or []) + [new_entry]
|
|
|
|
|
|
|
| 473 |
else:
|
| 474 |
updated_history = history_state_list or []
|
| 475 |
|
| 476 |
+
history_labels = [f"{item['id']} - {item['prompt'][:40]}..." for item in updated_history]
|
|
|
|
|
|
|
|
|
|
| 477 |
|
| 478 |
yield final_chat, updated_history, gr.update(choices=history_labels)
|
| 479 |
|
|
|
|
| 481 |
if not selection or not history_state_list:
|
| 482 |
return ""
|
| 483 |
try:
|
| 484 |
+
selected_id = selection.split(" - ", 1)
|
| 485 |
except Exception:
|
| 486 |
selected_id = selection
|
| 487 |
|
|
|
|
| 537 |
|
| 538 |
if __name__ == "__main__":
|
| 539 |
if not os.getenv("COHERE_API_KEY"):
|
| 540 |
+
print("馃敶 COHERE_API_KEY environment variable not set. Application may not function correctly.")
|
|
|
|
|
|
|
| 541 |
demo.launch(server_name="0.0.0.0", server_port=int(os.getenv("PORT", "7860")))
|