Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
|
| 3 |
import os
|
|
@@ -10,47 +11,48 @@ from MeatValidatorAgent import MEATValidatorAgent
|
|
| 10 |
|
| 11 |
APP_TITLE = "Risk Adjustment (HCC Chart Validation)"
|
| 12 |
CSV_PATH = "hcc_mapping.csv"
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# ---------- Helper: JSON β Markdown ----------
|
| 15 |
def json_to_markdown(meat_results: list[dict]) -> str:
|
| 16 |
if not meat_results:
|
| 17 |
-
return "β οΈ
|
| 18 |
|
| 19 |
-
md = "
|
|
|
|
| 20 |
|
| 21 |
for entry in meat_results:
|
| 22 |
-
md += f"
|
| 23 |
-
md += f"
|
| 24 |
-
md += f"
|
| 25 |
-
md += f"
|
| 26 |
-
md += f"
|
| 27 |
-
md +=
|
| 28 |
meat = entry.get("meat", {})
|
| 29 |
-
if meat
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
md += f"
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
|
| 39 |
# ---------- Pipeline ----------
|
| 40 |
def process_pipeline(pdf_file, hcc_code, model_version, csv_path=CSV_PATH):
|
| 41 |
try:
|
| 42 |
if pdf_file is None:
|
| 43 |
-
return "β οΈ Please upload a patient chart PDF."
|
| 44 |
|
| 45 |
if not hcc_code or str(hcc_code).strip() == "":
|
| 46 |
-
return "β οΈ Please enter a valid HCC Code before running validation."
|
| 47 |
|
| 48 |
pdf_path = pdf_file.name
|
| 49 |
patient_name = os.path.splitext(os.path.basename(pdf_path))[0]
|
| 50 |
print(f"\n[PROCESSING] {patient_name}")
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
# Step 1: HCC Diagnoses
|
| 55 |
diag_engine = HCCDiagnosisListEngine(hcc_code, model_version, csv_path)
|
| 56 |
diagnoses = diag_engine.run()
|
|
@@ -59,10 +61,10 @@ def process_pipeline(pdf_file, hcc_code, model_version, csv_path=CSV_PATH):
|
|
| 59 |
checker = ChartDiagnosisChecker(pdf_path)
|
| 60 |
checked = checker.run(diagnoses)
|
| 61 |
|
| 62 |
-
# β
|
| 63 |
has_positive = any(entry.get("answer", "").lower() == "yes" for entry in checked)
|
| 64 |
if not has_positive:
|
| 65 |
-
return f"
|
| 66 |
|
| 67 |
# Step 3: MEAT Validation
|
| 68 |
meat_validator = MEATValidatorAgent()
|
|
@@ -72,9 +74,7 @@ def process_pipeline(pdf_file, hcc_code, model_version, csv_path=CSV_PATH):
|
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
print(f"[ERROR] {e}")
|
| 75 |
-
return f"β οΈ Error during processing: {e}"
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
|
| 80 |
# ---------- Gradio Theme ----------
|
|
@@ -93,8 +93,13 @@ simple_theme = gr.themes.Soft(
|
|
| 93 |
|
| 94 |
# ---------- Gradio UI ----------
|
| 95 |
with gr.Blocks(theme=simple_theme, title=APP_TITLE) as interface:
|
| 96 |
-
gr.HTML(f"
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
with gr.Row():
|
| 100 |
with gr.Column(scale=1):
|
|
@@ -106,7 +111,7 @@ with gr.Blocks(theme=simple_theme, title=APP_TITLE) as interface:
|
|
| 106 |
with gr.Column(scale=2):
|
| 107 |
output_md = gr.Markdown(
|
| 108 |
label="Validation Report",
|
| 109 |
-
value="π Upload a PDF and click
|
| 110 |
)
|
| 111 |
|
| 112 |
run_btn.click(
|
|
@@ -118,3 +123,143 @@ with gr.Blocks(theme=simple_theme, title=APP_TITLE) as interface:
|
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
| 120 |
interface.queue().launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# # app.py
|
| 2 |
# app.py
|
| 3 |
|
| 4 |
import os
|
|
|
|
| 11 |
|
| 12 |
APP_TITLE = "Risk Adjustment (HCC Chart Validation)"
|
| 13 |
CSV_PATH = "hcc_mapping.csv"
|
| 14 |
+
SAMPLE_PDF = "sample_patient_chart.pdf" # Place a sample PDF in the same folder
|
| 15 |
+
|
| 16 |
|
| 17 |
# ---------- Helper: JSON β Markdown ----------
|
| 18 |
def json_to_markdown(meat_results: list[dict]) -> str:
|
| 19 |
if not meat_results:
|
| 20 |
+
return "<div style='color:orange; font-weight:bold;'>β οΈ No results found.</div>"
|
| 21 |
|
| 22 |
+
md = "<div style='border:2px solid #1e40af; border-radius:12px; padding:15px; background-color:#f0f9ff;'>"
|
| 23 |
+
md += "<h2 style='color:#1e40af;'>π©Ί HCC Chart Validation Report</h2>"
|
| 24 |
|
| 25 |
for entry in meat_results:
|
| 26 |
+
md += f"<h3 style='color:#1d4ed8;'>π {entry['diagnosis']} ({entry['icd10']})</h3>"
|
| 27 |
+
md += f"<p><b>Reference:</b> {entry.get('reference', 'N/A')}<br>"
|
| 28 |
+
md += f"<b>Answer:</b> {entry.get('answer', 'unknown').upper()}<br>"
|
| 29 |
+
md += f"<b>Rationale:</b> {entry.get('rationale', '')}<br>"
|
| 30 |
+
md += f"<b>Clinical Status:</b> {entry.get('clinical_status', 'N/A')}</p>"
|
| 31 |
+
md += "<b>MEAT Criteria:</b><ul>"
|
| 32 |
meat = entry.get("meat", {})
|
| 33 |
+
md += f"<li>Monitor: {'β
' if meat.get('monitor') else 'β'}</li>"
|
| 34 |
+
md += f"<li>Evaluate: {'β
' if meat.get('evaluate') else 'β'}</li>"
|
| 35 |
+
md += f"<li>Assess: {'β
' if meat.get('assess') else 'β'}</li>"
|
| 36 |
+
md += f"<li>Treat: {'β
' if meat.get('treat') else 'β'}</li>"
|
| 37 |
+
md += "</ul>"
|
| 38 |
+
md += f"<b>MEAT Rationale:</b> {entry.get('meat_rationale', '')}<br><br>"
|
| 39 |
+
md += "</div>"
|
| 40 |
+
return md
|
| 41 |
|
| 42 |
|
| 43 |
# ---------- Pipeline ----------
|
| 44 |
def process_pipeline(pdf_file, hcc_code, model_version, csv_path=CSV_PATH):
|
| 45 |
try:
|
| 46 |
if pdf_file is None:
|
| 47 |
+
return "<div style='color:orange; font-weight:bold;'>β οΈ Please upload a patient chart PDF.</div>"
|
| 48 |
|
| 49 |
if not hcc_code or str(hcc_code).strip() == "":
|
| 50 |
+
return "<div style='color:orange; font-weight:bold;'>β οΈ Please enter a valid HCC Code before running validation.</div>"
|
| 51 |
|
| 52 |
pdf_path = pdf_file.name
|
| 53 |
patient_name = os.path.splitext(os.path.basename(pdf_path))[0]
|
| 54 |
print(f"\n[PROCESSING] {patient_name}")
|
| 55 |
|
|
|
|
|
|
|
| 56 |
# Step 1: HCC Diagnoses
|
| 57 |
diag_engine = HCCDiagnosisListEngine(hcc_code, model_version, csv_path)
|
| 58 |
diagnoses = diag_engine.run()
|
|
|
|
| 61 |
checker = ChartDiagnosisChecker(pdf_path)
|
| 62 |
checked = checker.run(diagnoses)
|
| 63 |
|
| 64 |
+
# β
Only proceed if at least one "yes"
|
| 65 |
has_positive = any(entry.get("answer", "").lower() == "yes" for entry in checked)
|
| 66 |
if not has_positive:
|
| 67 |
+
return f"<b>β Not found diagnosis in the chart related to HCC code {hcc_code}.</b>"
|
| 68 |
|
| 69 |
# Step 3: MEAT Validation
|
| 70 |
meat_validator = MEATValidatorAgent()
|
|
|
|
| 74 |
|
| 75 |
except Exception as e:
|
| 76 |
print(f"[ERROR] {e}")
|
| 77 |
+
return f"<div style='color:red; font-weight:bold;'>β οΈ Error during processing: {e}</div>"
|
|
|
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
# ---------- Gradio Theme ----------
|
|
|
|
| 93 |
|
| 94 |
# ---------- Gradio UI ----------
|
| 95 |
with gr.Blocks(theme=simple_theme, title=APP_TITLE) as interface:
|
| 96 |
+
gr.HTML(f"""
|
| 97 |
+
<h1 style='text-align:center;color:#1e40af;'>π©Ί {APP_TITLE}</h1>
|
| 98 |
+
<p style='text-align:center;color:#64748b;'>
|
| 99 |
+
Upload a chart, set HCC + model version, and validate MEAT criteria.<br>
|
| 100 |
+
π <a href='{SAMPLE_PDF}' download>Download Sample PDF</a>
|
| 101 |
+
</p>
|
| 102 |
+
""")
|
| 103 |
|
| 104 |
with gr.Row():
|
| 105 |
with gr.Column(scale=1):
|
|
|
|
| 111 |
with gr.Column(scale=2):
|
| 112 |
output_md = gr.Markdown(
|
| 113 |
label="Validation Report",
|
| 114 |
+
value="<div style='border:2px solid #1e40af; border-radius:12px; padding:15px; background-color:#f0f9ff;'>π Upload a PDF and click <b>Run Validation</b> to start.</div>",
|
| 115 |
)
|
| 116 |
|
| 117 |
run_btn.click(
|
|
|
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
interface.queue().launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
#### old
|
| 148 |
+
# import os
|
| 149 |
+
# import gradio as gr
|
| 150 |
+
# import json
|
| 151 |
+
|
| 152 |
+
# from HCCDiagnosisListEngine import HCCDiagnosisListEngine
|
| 153 |
+
# from chartdiagnosischecker import ChartDiagnosisChecker
|
| 154 |
+
# from MeatValidatorAgent import MEATValidatorAgent
|
| 155 |
+
|
| 156 |
+
# APP_TITLE = "Risk Adjustment (HCC Chart Validation)"
|
| 157 |
+
# CSV_PATH = "hcc_mapping.csv"
|
| 158 |
+
|
| 159 |
+
# # ---------- Helper: JSON β Markdown ----------
|
| 160 |
+
# def json_to_markdown(meat_results: list[dict]) -> str:
|
| 161 |
+
# if not meat_results:
|
| 162 |
+
# return "β οΈ **No results found.**"
|
| 163 |
+
|
| 164 |
+
# md = "## π₯ HCC Chart Validation Report\n\n"
|
| 165 |
+
|
| 166 |
+
# for entry in meat_results:
|
| 167 |
+
# md += f"### π {entry['diagnosis']} ({entry['icd10']})\n"
|
| 168 |
+
# md += f"- **Reference:** {entry.get('reference', 'N/A')}\n"
|
| 169 |
+
# md += f"- **Answer:** {entry.get('answer', 'unknown').upper()}\n"
|
| 170 |
+
# md += f"- **Rationale:** {entry.get('rationale', '')}\n"
|
| 171 |
+
# md += f"- **Clinical Status:** {entry.get('clinical_status', 'N/A')}\n"
|
| 172 |
+
# md += f"- **MEAT Criteria:**\n"
|
| 173 |
+
# meat = entry.get("meat", {})
|
| 174 |
+
# if meat:
|
| 175 |
+
# md += " - Monitor: β
\n" if meat.get("monitor") else " - Monitor: β\n"
|
| 176 |
+
# md += " - Evaluate: β
\n" if meat.get("evaluate") else " - Evaluate: β\n"
|
| 177 |
+
# md += " - Assess: β
\n" if meat.get("assess") else " - Assess: β\n"
|
| 178 |
+
# md += " - Treat: β
\n" if meat.get("treat") else " - Treat: β\n"
|
| 179 |
+
# md += f"- **MEAT Rationale:** {entry.get('meat_rationale', '')}\n\n"
|
| 180 |
+
# return md.strip()
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
# # ---------- Pipeline ----------
|
| 185 |
+
# def process_pipeline(pdf_file, hcc_code, model_version, csv_path=CSV_PATH):
|
| 186 |
+
# try:
|
| 187 |
+
# if pdf_file is None:
|
| 188 |
+
# return "β οΈ Please upload a patient chart PDF."
|
| 189 |
+
|
| 190 |
+
# if not hcc_code or str(hcc_code).strip() == "":
|
| 191 |
+
# return "β οΈ Please enter a valid HCC Code before running validation."
|
| 192 |
+
|
| 193 |
+
# pdf_path = pdf_file.name
|
| 194 |
+
# patient_name = os.path.splitext(os.path.basename(pdf_path))[0]
|
| 195 |
+
# print(f"\n[PROCESSING] {patient_name}")
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
# # Step 1: HCC Diagnoses
|
| 200 |
+
# diag_engine = HCCDiagnosisListEngine(hcc_code, model_version, csv_path)
|
| 201 |
+
# diagnoses = diag_engine.run()
|
| 202 |
+
|
| 203 |
+
# # Step 2: Chart Checking
|
| 204 |
+
# checker = ChartDiagnosisChecker(pdf_path)
|
| 205 |
+
# checked = checker.run(diagnoses)
|
| 206 |
+
|
| 207 |
+
# # β
New condition: only proceed if at least one "yes"
|
| 208 |
+
# has_positive = any(entry.get("answer", "").lower() == "yes" for entry in checked)
|
| 209 |
+
# if not has_positive:
|
| 210 |
+
# return f"**β Not found diagnosis in the chart related to HCC code {hcc_code}.**"
|
| 211 |
+
|
| 212 |
+
# # Step 3: MEAT Validation
|
| 213 |
+
# meat_validator = MEATValidatorAgent()
|
| 214 |
+
# meat_results = meat_validator.run(checked)
|
| 215 |
+
|
| 216 |
+
# return json_to_markdown(meat_results)
|
| 217 |
+
|
| 218 |
+
# except Exception as e:
|
| 219 |
+
# print(f"[ERROR] {e}")
|
| 220 |
+
# return f"β οΈ Error during processing: {e}"
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
# # ---------- Gradio Theme ----------
|
| 226 |
+
# simple_theme = gr.themes.Soft(
|
| 227 |
+
# primary_hue=gr.themes.colors.blue,
|
| 228 |
+
# secondary_hue=gr.themes.colors.slate,
|
| 229 |
+
# neutral_hue=gr.themes.colors.slate,
|
| 230 |
+
# ).set(
|
| 231 |
+
# button_primary_background_fill="#1e40af",
|
| 232 |
+
# button_primary_background_fill_hover="#1d4ed8",
|
| 233 |
+
# button_primary_text_color="white",
|
| 234 |
+
# background_fill_primary="white",
|
| 235 |
+
# background_fill_secondary="#f8fafc",
|
| 236 |
+
# )
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
# # ---------- Gradio UI ----------
|
| 240 |
+
# with gr.Blocks(theme=simple_theme, title=APP_TITLE) as interface:
|
| 241 |
+
# gr.HTML(f"<h1 style='text-align:center;color:#1e40af;'>π₯ {APP_TITLE}</h1>")
|
| 242 |
+
# gr.HTML("<p style='text-align:center;color:#64748b;'>Upload a chart, set HCC + model version, and validate MEAT criteria.</p>")
|
| 243 |
+
|
| 244 |
+
# with gr.Row():
|
| 245 |
+
# with gr.Column(scale=1):
|
| 246 |
+
# pdf_upload = gr.File(label="Upload Patient Chart (PDF)", file_types=[".pdf"])
|
| 247 |
+
# hcc_code = gr.Textbox(label="HCC Code (e.g., 12)", placeholder="Enter HCC code")
|
| 248 |
+
# model_version = gr.Dropdown(choices=["V24", "V28"], label="Model Version", value="V24")
|
| 249 |
+
# run_btn = gr.Button("π Run Validation", variant="primary")
|
| 250 |
+
|
| 251 |
+
# with gr.Column(scale=2):
|
| 252 |
+
# output_md = gr.Markdown(
|
| 253 |
+
# label="Validation Report",
|
| 254 |
+
# value="π Upload a PDF and click **Run Validation** to start.",
|
| 255 |
+
# )
|
| 256 |
+
|
| 257 |
+
# run_btn.click(
|
| 258 |
+
# fn=process_pipeline,
|
| 259 |
+
# inputs=[pdf_upload, hcc_code, model_version],
|
| 260 |
+
# outputs=[output_md],
|
| 261 |
+
# )
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
# if __name__ == "__main__":
|
| 265 |
+
# interface.queue().launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|