Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
from HCCDiagnosisListEngine import HCCDiagnosisListEngine
|
| 8 |
+
from chartdiagnosischecker import ChartDiagnosisChecker
|
| 9 |
+
from MeatValidatorAgent import MEATValidatorAgent
|
| 10 |
+
|
| 11 |
+
APP_TITLE = "HCC Chart Validation & MEAT Criteria Extraction"
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# ---------- Helper: JSON β Markdown ----------
|
| 15 |
+
def json_to_markdown(meat_results: list[dict]) -> str:
|
| 16 |
+
if not meat_results:
|
| 17 |
+
return "β οΈ **No results found.**"
|
| 18 |
+
|
| 19 |
+
md = "## π₯ HCC Chart Validation Report\n\n"
|
| 20 |
+
|
| 21 |
+
for entry in meat_results:
|
| 22 |
+
md += f"### π {entry['diagnosis']} ({entry['icd10']})\n"
|
| 23 |
+
md += f"- **Reference:** {entry.get('reference', 'N/A')}\n"
|
| 24 |
+
md += f"- **Answer:** {entry.get('answer', 'unknown').upper()}\n"
|
| 25 |
+
md += f"- **Rationale:** {entry.get('rationale', '')}\n"
|
| 26 |
+
md += f"- **Clinical Status:** {entry.get('clinical_status', 'N/A')}\n"
|
| 27 |
+
md += f"- **MEAT Criteria:**\n"
|
| 28 |
+
meat = entry.get("meat", {})
|
| 29 |
+
if meat:
|
| 30 |
+
md += " - Monitor: β
\n" if meat.get("monitor") else " - Monitor: β\n"
|
| 31 |
+
md += " - Evaluate: β
\n" if meat.get("evaluate") else " - Evaluate: β\n"
|
| 32 |
+
md += " - Assess: β
\n" if meat.get("assess") else " - Assess: β\n"
|
| 33 |
+
md += " - Treat: β
\n" if meat.get("treat") else " - Treat: β\n"
|
| 34 |
+
md += f"- **MEAT Rationale:** {entry.get('meat_rationale', '')}\n\n"
|
| 35 |
+
return md.strip()
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# ---------- Pipeline ----------
|
| 39 |
+
def process_pipeline(pdf_file, hcc_code, model_version, csv_path="hcc_mapping.csv"):
|
| 40 |
+
try:
|
| 41 |
+
if pdf_file is None:
|
| 42 |
+
return "β οΈ Please upload a patient chart PDF."
|
| 43 |
+
|
| 44 |
+
pdf_path = pdf_file.name
|
| 45 |
+
patient_name = os.path.splitext(os.path.basename(pdf_path))[0]
|
| 46 |
+
print(f"\n[PROCESSING] {patient_name}")
|
| 47 |
+
|
| 48 |
+
# Step 1: HCC Diagnoses
|
| 49 |
+
diag_engine = HCCDiagnosisListEngine(hcc_code, model_version, csv_path)
|
| 50 |
+
diagnoses = diag_engine.run()
|
| 51 |
+
|
| 52 |
+
# Step 2: Chart Checking
|
| 53 |
+
checker = ChartDiagnosisChecker(pdf_path)
|
| 54 |
+
checked = checker.run(diagnoses)
|
| 55 |
+
|
| 56 |
+
# Step 3: MEAT Validation
|
| 57 |
+
meat_validator = MEATValidatorAgent()
|
| 58 |
+
meat_results = meat_validator.run(checked)
|
| 59 |
+
|
| 60 |
+
return json_to_markdown(meat_results)
|
| 61 |
+
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"[ERROR] {e}")
|
| 64 |
+
return f"β οΈ Error during processing: {e}"
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# ---------- Gradio Theme ----------
|
| 68 |
+
simple_theme = gr.themes.Soft(
|
| 69 |
+
primary_hue=gr.themes.colors.blue,
|
| 70 |
+
secondary_hue=gr.themes.colors.slate,
|
| 71 |
+
neutral_hue=gr.themes.colors.slate,
|
| 72 |
+
).set(
|
| 73 |
+
button_primary_background_fill="#1e40af",
|
| 74 |
+
button_primary_background_fill_hover="#1d4ed8",
|
| 75 |
+
button_primary_text_color="white",
|
| 76 |
+
background_fill_primary="white",
|
| 77 |
+
background_fill_secondary="#f8fafc",
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# ---------- Gradio UI ----------
|
| 82 |
+
with gr.Blocks(theme=simple_theme, title=APP_TITLE) as interface:
|
| 83 |
+
gr.HTML(f"<h1 style='text-align:center;color:#1e40af;'>π₯ {APP_TITLE}</h1>")
|
| 84 |
+
gr.HTML("<p style='text-align:center;color:#64748b;'>Upload a chart, set HCC + model version, and validate MEAT criteria.</p>")
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
| 87 |
+
with gr.Column(scale=1):
|
| 88 |
+
pdf_upload = gr.File(label="Upload Patient Chart (PDF)", file_types=[".pdf"])
|
| 89 |
+
hcc_code = gr.Textbox(label="HCC Code (e.g., 12)", placeholder="Enter HCC code")
|
| 90 |
+
model_version = gr.Dropdown(choices=["V24", "V28"], label="Model Version", value="V24")
|
| 91 |
+
run_btn = gr.Button("π Run Validation", variant="primary")
|
| 92 |
+
|
| 93 |
+
with gr.Column(scale=2):
|
| 94 |
+
output_md = gr.Markdown(
|
| 95 |
+
label="Validation Report",
|
| 96 |
+
value="π Upload a PDF and click **Run Validation** to start.",
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
run_btn.click(
|
| 100 |
+
fn=process_pipeline,
|
| 101 |
+
inputs=[pdf_upload, hcc_code, model_version],
|
| 102 |
+
outputs=[output_md],
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
if __name__ == "__main__":
|
| 107 |
+
interface.queue().launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|