Spaces:
Sleeping
Sleeping
Rajan Sharma
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,14 @@ from privacy import redact_text, safety_filter, refusal_reply
|
|
| 19 |
from data_registry import DataRegistry
|
| 20 |
from upload_ingest import extract_text_from_files
|
| 21 |
from healthcare_analysis import HealthcareAnalyzer
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# ---------- Writable caches (HF Spaces-safe) ----------
|
| 25 |
HOME = pathlib.Path.home()
|
|
@@ -62,6 +69,9 @@ COHERE_API_KEY = os.getenv("COHERE_API_KEY")
|
|
| 62 |
USE_HOSTED_COHERE = bool(COHERE_API_KEY and _HAS_COHERE)
|
| 63 |
MAX_NEW_TOKENS = int(os.getenv("MAX_NEW_TOKENS", MODEL_SETTINGS.get("max_new_tokens", 2048)))
|
| 64 |
|
|
|
|
|
|
|
|
|
|
| 65 |
# ---------- Helper Functions ----------
|
| 66 |
def find_column(df, patterns):
|
| 67 |
"""Find the first column in df that matches any of the patterns."""
|
|
@@ -188,18 +198,23 @@ def handle_healthcare_scenario(scenario_text, data_registry, history):
|
|
| 188 |
# Initialize analyzer
|
| 189 |
analyzer = HealthcareAnalyzer(data_registry)
|
| 190 |
|
| 191 |
-
# Perform comprehensive analysis
|
| 192 |
results = analyzer.comprehensive_analysis(scenario_text)
|
| 193 |
|
| 194 |
-
#
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
return response
|
| 199 |
except Exception as e:
|
| 200 |
log_event("healthcare_scenario_error", None, {"error": str(e)})
|
| 201 |
# Log the full traceback for better debugging
|
| 202 |
-
import traceback
|
| 203 |
tb_str = traceback.format_exc()
|
| 204 |
log_event("healthcare_scenario_traceback", None, {"traceback": tb_str})
|
| 205 |
return f"Error analyzing healthcare scenario: {str(e)}\n\nTechnical details:\n{tb_str}"
|
|
@@ -352,11 +367,11 @@ def clarityops_reply(user_msg, history, tz, uploaded_files_paths, awaiting_answe
|
|
| 352 |
|
| 353 |
# Determine the mode: healthcare scenario or general conversation
|
| 354 |
if is_healthcare_scenario(safe_in, uploaded_files_paths):
|
| 355 |
-
# Healthcare scenario mode
|
| 356 |
response = handle_healthcare_scenario(safe_in, data_registry, history)
|
| 357 |
return history + [(user_msg, response)], False
|
| 358 |
else:
|
| 359 |
-
# General conversation mode with enhanced handling
|
| 360 |
if USE_HOSTED_COHERE:
|
| 361 |
out = cohere_chat(safe_in, history)
|
| 362 |
if out:
|
|
@@ -449,7 +464,13 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 449 |
elem_classes="hero-box"
|
| 450 |
)
|
| 451 |
hero_send = gr.Button("➤", scale=0, elem_id="hero-send")
|
| 452 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 453 |
|
| 454 |
# --- MAIN APP (hidden until first message) ---
|
| 455 |
with gr.Column(elem_id="chat-container", visible=False) as app_wrap:
|
|
@@ -545,4 +566,4 @@ with gr.Blocks(theme=theme, css=custom_css, analytics_enabled=False) as demo:
|
|
| 545 |
|
| 546 |
if __name__ == "__main__":
|
| 547 |
port = int(os.environ.get("PORT", "7860"))
|
| 548 |
-
demo.launch(server_name="0.0.0.0", server_port=port, show_api=False, max_threads=40)
|
|
|
|
| 19 |
from data_registry import DataRegistry
|
| 20 |
from upload_ingest import extract_text_from_files
|
| 21 |
from healthcare_analysis import HealthcareAnalyzer
|
| 22 |
+
|
| 23 |
+
# ---- NEW: scenario-first engine (keeps general chat intact)
|
| 24 |
+
from scenario_engine import ScenarioEngine
|
| 25 |
+
# (Optional) keep old formatter if you want a fallback:
|
| 26 |
+
try:
|
| 27 |
+
from response_formatter import ResponseFormatter # noqa: F401
|
| 28 |
+
except Exception:
|
| 29 |
+
ResponseFormatter = None # type: ignore
|
| 30 |
|
| 31 |
# ---------- Writable caches (HF Spaces-safe) ----------
|
| 32 |
HOME = pathlib.Path.home()
|
|
|
|
| 69 |
USE_HOSTED_COHERE = bool(COHERE_API_KEY and _HAS_COHERE)
|
| 70 |
MAX_NEW_TOKENS = int(os.getenv("MAX_NEW_TOKENS", MODEL_SETTINGS.get("max_new_tokens", 2048)))
|
| 71 |
|
| 72 |
+
# ---- NEW: feature flag to toggle engines without code edits
|
| 73 |
+
USE_SCENARIO_ENGINE = os.getenv("USE_SCENARIO_ENGINE", "1") not in ("0", "false", "False")
|
| 74 |
+
|
| 75 |
# ---------- Helper Functions ----------
|
| 76 |
def find_column(df, patterns):
|
| 77 |
"""Find the first column in df that matches any of the patterns."""
|
|
|
|
| 198 |
# Initialize analyzer
|
| 199 |
analyzer = HealthcareAnalyzer(data_registry)
|
| 200 |
|
| 201 |
+
# Perform comprehensive analysis (returns dict of datasets/results)
|
| 202 |
results = analyzer.comprehensive_analysis(scenario_text)
|
| 203 |
|
| 204 |
+
# ---- NEW: Scenario-first exact-output engine
|
| 205 |
+
if USE_SCENARIO_ENGINE:
|
| 206 |
+
response = ScenarioEngine.render(scenario_text, results)
|
| 207 |
+
else:
|
| 208 |
+
# Optional fallback to legacy formatter if desired
|
| 209 |
+
if ResponseFormatter is None:
|
| 210 |
+
raise RuntimeError("ResponseFormatter not available and USE_SCENARIO_ENGINE is disabled.")
|
| 211 |
+
formatter = ResponseFormatter()
|
| 212 |
+
response = formatter.format_healthcare_response(scenario_text, results)
|
| 213 |
|
| 214 |
return response
|
| 215 |
except Exception as e:
|
| 216 |
log_event("healthcare_scenario_error", None, {"error": str(e)})
|
| 217 |
# Log the full traceback for better debugging
|
|
|
|
| 218 |
tb_str = traceback.format_exc()
|
| 219 |
log_event("healthcare_scenario_traceback", None, {"traceback": tb_str})
|
| 220 |
return f"Error analyzing healthcare scenario: {str(e)}\n\nTechnical details:\n{tb_str}"
|
|
|
|
| 367 |
|
| 368 |
# Determine the mode: healthcare scenario or general conversation
|
| 369 |
if is_healthcare_scenario(safe_in, uploaded_files_paths):
|
| 370 |
+
# Healthcare scenario mode (ScenarioEngine enforces exact asks)
|
| 371 |
response = handle_healthcare_scenario(safe_in, data_registry, history)
|
| 372 |
return history + [(user_msg, response)], False
|
| 373 |
else:
|
| 374 |
+
# General conversation mode with enhanced handling (unchanged)
|
| 375 |
if USE_HOSTED_COHERE:
|
| 376 |
out = cohere_chat(safe_in, history)
|
| 377 |
if out:
|
|
|
|
| 464 |
elem_classes="hero-box"
|
| 465 |
)
|
| 466 |
hero_send = gr.Button("➤", scale=0, elem_id="hero-send")
|
| 467 |
+
# ---- NEW: hint that directive-driven scenarios are supported
|
| 468 |
+
gr.Markdown(
|
| 469 |
+
'<div class="hint">I can chat normally or run directive-based analyses. '
|
| 470 |
+
'In scenarios, add directives like <code>format:</code>, <code>data_key:</code>, '
|
| 471 |
+
'<code>filter:</code>, <code>group_by:</code>, <code>agg:</code>, <code>pivot:</code>, '
|
| 472 |
+
'<code>sort_by:</code>, <code>top:</code>, <code>fields:</code>, <code>chart:</code> to control the output exactly.</div>'
|
| 473 |
+
)
|
| 474 |
|
| 475 |
# --- MAIN APP (hidden until first message) ---
|
| 476 |
with gr.Column(elem_id="chat-container", visible=False) as app_wrap:
|
|
|
|
| 566 |
|
| 567 |
if __name__ == "__main__":
|
| 568 |
port = int(os.environ.get("PORT", "7860"))
|
| 569 |
+
demo.launch(server_name="0.0.0.0", server_port=port, show_api=False, max_threads=40)
|