Update app.py
Browse files
app.py
CHANGED
|
@@ -14,27 +14,33 @@ model = joblib.load("typhoidguard_model_reduced.pkl")
|
|
| 14 |
|
| 15 |
# Risk & Agent Logic
|
| 16 |
# Risk & Agentic Logic
|
|
|
|
| 17 |
def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, potassium):
|
| 18 |
|
| 19 |
-
# 1️⃣ Prepare
|
| 20 |
data = pd.DataFrame([{
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
}])
|
| 29 |
|
| 30 |
-
# 2️⃣
|
| 31 |
X_processed = model.named_steps["preprocessor"].transform(data)
|
| 32 |
|
| 33 |
-
# 3️⃣
|
| 34 |
-
prob = model.named_steps["classifier"].predict_proba(X_processed)[0][1]
|
| 35 |
prob_pct = round(prob * 100, 2)
|
| 36 |
|
| 37 |
-
# 4️⃣
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
if prob > 0.65:
|
| 39 |
risk_label = "🔴 HIGH RISK"
|
| 40 |
color = "#d62828"
|
|
@@ -45,38 +51,30 @@ def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, po
|
|
| 45 |
risk_label = "🟢 LOW RISK"
|
| 46 |
color = "#2a9d8f"
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
low_platelets = patient_data.get("Platelet Count", 999999) < 150000
|
| 51 |
-
low_hb = patient_data.get("Hemoglobin (g/dL)", 999) < 10
|
| 52 |
-
|
| 53 |
-
if prob > 0.65 or low_platelets or low_hb:
|
| 54 |
-
recommendation = "Immediate referral to secondary care facility recommended. Close monitoring required."
|
| 55 |
risk_label = "🔴 HIGH RISK"
|
| 56 |
color = "#d62828"
|
| 57 |
-
|
|
|
|
| 58 |
recommendation = "Close monitoring advised. Repeat blood tests within 48 hours."
|
| 59 |
-
risk_label = "🟡 MODERATE RISK"
|
| 60 |
-
color = "#f4a261"
|
| 61 |
else:
|
| 62 |
recommendation = "Continue standard treatment protocol."
|
| 63 |
-
risk_label = "🟢 LOW RISK"
|
| 64 |
-
color = "#2a9d8f"
|
| 65 |
|
| 66 |
-
#
|
| 67 |
factors = []
|
| 68 |
if low_platelets:
|
| 69 |
factors.append("Low Platelet Count")
|
| 70 |
if low_hb:
|
| 71 |
factors.append("Low Hemoglobin")
|
| 72 |
-
if
|
| 73 |
factors.append("Prolonged Treatment Duration")
|
| 74 |
if calcium < 8.5:
|
| 75 |
factors.append("Low Calcium")
|
| 76 |
if potassium < 3.5 or potassium > 5.0:
|
| 77 |
factors.append("Abnormal Potassium")
|
| 78 |
|
| 79 |
-
#
|
| 80 |
explanation = f"""
|
| 81 |
<div style="font-family:Arial; padding:15px;">
|
| 82 |
<h2 style="color:{color};">Patient: {name}</h2>
|
|
@@ -123,7 +121,7 @@ def generate_pdf(name, age, gender, platelets, hb, duration, calcium, potassium)
|
|
| 123 |
elements.append(table)
|
| 124 |
elements.append(Spacer(1, 0.5 * inch))
|
| 125 |
elements.append(Paragraph("Generated under HEC Generative AI Training Hackathon", styles["Normal"]))
|
| 126 |
-
elements.append(Paragraph("Team
|
| 127 |
|
| 128 |
doc.build(elements)
|
| 129 |
return file_path
|
|
@@ -135,7 +133,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 135 |
gr.Markdown("""
|
| 136 |
# 🏥 TyphoidGuard AI
|
| 137 |
### HEC Generative AI Training Hackathon – Pakistan
|
| 138 |
-
#### Team
|
| 139 |
""")
|
| 140 |
|
| 141 |
with gr.Row():
|
|
|
|
| 14 |
|
| 15 |
# Risk & Agent Logic
|
| 16 |
# Risk & Agentic Logic
|
| 17 |
+
|
| 18 |
def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, potassium):
|
| 19 |
|
| 20 |
+
# 1️⃣ Prepare data with exact column names
|
| 21 |
data = pd.DataFrame([{
|
| 22 |
+
"Platelet Count": platelets,
|
| 23 |
+
"Age": age,
|
| 24 |
+
"Hemoglobin (g/dL)": hb,
|
| 25 |
+
"Treatment Duration": duration,
|
| 26 |
+
"Calcium (mg/dL)": calcium,
|
| 27 |
+
"Potassium (mmol/L)": potassium,
|
| 28 |
+
"Gender": gender
|
| 29 |
}])
|
| 30 |
|
| 31 |
+
# 2️⃣ Transform through preprocessor
|
| 32 |
X_processed = model.named_steps["preprocessor"].transform(data)
|
| 33 |
|
| 34 |
+
# 3️⃣ Model probability
|
| 35 |
+
prob = model.named_steps["classifier"].predict_proba(X_processed)[0][1]
|
| 36 |
prob_pct = round(prob * 100, 2)
|
| 37 |
|
| 38 |
+
# 4️⃣ Clinical thresholds (override)
|
| 39 |
+
low_platelets = platelets < 150000
|
| 40 |
+
low_hb = hb < 10
|
| 41 |
+
prolonged_treatment = duration > 7
|
| 42 |
+
|
| 43 |
+
# Default risk label based on probability
|
| 44 |
if prob > 0.65:
|
| 45 |
risk_label = "🔴 HIGH RISK"
|
| 46 |
color = "#d62828"
|
|
|
|
| 51 |
risk_label = "🟢 LOW RISK"
|
| 52 |
color = "#2a9d8f"
|
| 53 |
|
| 54 |
+
# Override risk for critical lab values
|
| 55 |
+
if low_platelets or low_hb:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
risk_label = "🔴 HIGH RISK"
|
| 57 |
color = "#d62828"
|
| 58 |
+
recommendation = "Immediate referral to secondary care facility recommended. Close monitoring required."
|
| 59 |
+
elif prob >= 0.35:
|
| 60 |
recommendation = "Close monitoring advised. Repeat blood tests within 48 hours."
|
|
|
|
|
|
|
| 61 |
else:
|
| 62 |
recommendation = "Continue standard treatment protocol."
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# 5️⃣ Risk factors list
|
| 65 |
factors = []
|
| 66 |
if low_platelets:
|
| 67 |
factors.append("Low Platelet Count")
|
| 68 |
if low_hb:
|
| 69 |
factors.append("Low Hemoglobin")
|
| 70 |
+
if prolonged_treatment:
|
| 71 |
factors.append("Prolonged Treatment Duration")
|
| 72 |
if calcium < 8.5:
|
| 73 |
factors.append("Low Calcium")
|
| 74 |
if potassium < 3.5 or potassium > 5.0:
|
| 75 |
factors.append("Abnormal Potassium")
|
| 76 |
|
| 77 |
+
# 6️⃣ Construct HTML
|
| 78 |
explanation = f"""
|
| 79 |
<div style="font-family:Arial; padding:15px;">
|
| 80 |
<h2 style="color:{color};">Patient: {name}</h2>
|
|
|
|
| 121 |
elements.append(table)
|
| 122 |
elements.append(Spacer(1, 0.5 * inch))
|
| 123 |
elements.append(Paragraph("Generated under HEC Generative AI Training Hackathon", styles["Normal"]))
|
| 124 |
+
elements.append(Paragraph("Team Chinar AI - AJK", styles["Normal"]))
|
| 125 |
|
| 126 |
doc.build(elements)
|
| 127 |
return file_path
|
|
|
|
| 133 |
gr.Markdown("""
|
| 134 |
# 🏥 TyphoidGuard AI
|
| 135 |
### HEC Generative AI Training Hackathon – Pakistan
|
| 136 |
+
#### Team Chinar AI (AJK)
|
| 137 |
""")
|
| 138 |
|
| 139 |
with gr.Row():
|