Update app.py
Browse files
app.py
CHANGED
|
@@ -13,22 +13,51 @@ from reportlab.platypus import TableStyle
|
|
| 13 |
model = joblib.load("typhoidguard_model_reduced.pkl")
|
| 14 |
|
| 15 |
# Risk & Agent Logic
|
|
|
|
| 16 |
def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, potassium):
|
| 17 |
|
|
|
|
| 18 |
data = pd.DataFrame([{
|
| 19 |
"Platelet Count": platelets,
|
| 20 |
"Age": age,
|
| 21 |
"Hemoglobin (g/dL)": hb,
|
| 22 |
"Treatment Duration": duration,
|
| 23 |
"Calcium (mg/dL)": calcium,
|
| 24 |
-
"Potassium
|
| 25 |
"Gender": gender
|
| 26 |
}])
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
factors = []
|
| 33 |
if platelets < 150000:
|
| 34 |
factors.append("Low Platelet Count")
|
|
@@ -36,25 +65,17 @@ def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, po
|
|
| 36 |
factors.append("Low Hemoglobin")
|
| 37 |
if duration > 7:
|
| 38 |
factors.append("Prolonged Treatment Duration")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
status = "🔴 HIGH RISK"
|
| 42 |
-
recommendation = "Immediate referral to secondary care facility recommended. Close monitoring required."
|
| 43 |
-
color = "#d62828"
|
| 44 |
-
elif 35 <= risk <= 65:
|
| 45 |
-
status = "🟡 MODERATE RISK"
|
| 46 |
-
recommendation = "Close monitoring advised. Repeat blood tests within 48 hours."
|
| 47 |
-
color = "#f4a261"
|
| 48 |
-
else:
|
| 49 |
-
status = "🟢 LOW RISK"
|
| 50 |
-
recommendation = "Continue standard treatment protocol."
|
| 51 |
-
color = "#2a9d8f"
|
| 52 |
-
|
| 53 |
explanation = f"""
|
| 54 |
<div style="font-family:Arial; padding:15px;">
|
| 55 |
<h2 style="color:{color};">Patient: {name}</h2>
|
| 56 |
-
<h3>Risk of Treatment Failure: {
|
| 57 |
-
<h3>Status: {
|
| 58 |
<h4>Clinical Risk Factors:</h4>
|
| 59 |
<ul>
|
| 60 |
{''.join([f"<li>{f}</li>" for f in factors])}
|
|
@@ -66,7 +87,6 @@ def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, po
|
|
| 66 |
|
| 67 |
return explanation
|
| 68 |
|
| 69 |
-
|
| 70 |
# PDF Generation
|
| 71 |
def generate_pdf(name, age, gender, platelets, hb, duration, calcium, potassium):
|
| 72 |
|
|
|
|
| 13 |
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 |
+
# Prepare input
|
| 20 |
data = pd.DataFrame([{
|
| 21 |
"Platelet Count": platelets,
|
| 22 |
"Age": age,
|
| 23 |
"Hemoglobin (g/dL)": hb,
|
| 24 |
"Treatment Duration": duration,
|
| 25 |
"Calcium (mg/dL)": calcium,
|
| 26 |
+
"Potassium": potassium,
|
| 27 |
"Gender": gender
|
| 28 |
}])
|
| 29 |
|
| 30 |
+
# 1️⃣ Model probability
|
| 31 |
+
# If your model is a pipeline: classifier is last step
|
| 32 |
+
try:
|
| 33 |
+
prob = model.predict_proba(data)[0][1] # probability of treatment failure
|
| 34 |
+
except:
|
| 35 |
+
# fallback
|
| 36 |
+
prob = 0.0
|
| 37 |
|
| 38 |
+
prob_pct = round(prob * 100, 2)
|
| 39 |
+
|
| 40 |
+
# 2️⃣ Risk label
|
| 41 |
+
if prob > 0.65:
|
| 42 |
+
risk_label = "🔴 HIGH RISK"
|
| 43 |
+
color = "#d62828"
|
| 44 |
+
elif prob >= 0.35:
|
| 45 |
+
risk_label = "🟡 MODERATE RISK"
|
| 46 |
+
color = "#f4a261"
|
| 47 |
+
else:
|
| 48 |
+
risk_label = "🟢 LOW RISK"
|
| 49 |
+
color = "#2a9d8f"
|
| 50 |
+
|
| 51 |
+
# 3️⃣ Agent triage logic (like your local function)
|
| 52 |
+
patient_data = data.iloc[0].to_dict()
|
| 53 |
+
if (prob > 0.65 and patient_data.get("Platelet Count", 999999) < 150000):
|
| 54 |
+
recommendation = "Immediate referral to secondary care facility recommended. Close monitoring required."
|
| 55 |
+
elif 0.35 <= prob <= 0.65:
|
| 56 |
+
recommendation = "Close monitoring advised. Repeat blood tests within 48 hours."
|
| 57 |
+
else:
|
| 58 |
+
recommendation = "Continue standard treatment protocol."
|
| 59 |
+
|
| 60 |
+
# 4️⃣ Top clinical risk factors
|
| 61 |
factors = []
|
| 62 |
if platelets < 150000:
|
| 63 |
factors.append("Low Platelet Count")
|
|
|
|
| 65 |
factors.append("Low Hemoglobin")
|
| 66 |
if duration > 7:
|
| 67 |
factors.append("Prolonged Treatment Duration")
|
| 68 |
+
if calcium < 8.5:
|
| 69 |
+
factors.append("Low Calcium")
|
| 70 |
+
if potassium < 3.5 or potassium > 5.0:
|
| 71 |
+
factors.append("Abnormal Potassium")
|
| 72 |
|
| 73 |
+
# 5️⃣ Construct HTML response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
explanation = f"""
|
| 75 |
<div style="font-family:Arial; padding:15px;">
|
| 76 |
<h2 style="color:{color};">Patient: {name}</h2>
|
| 77 |
+
<h3>Risk of Treatment Failure: {prob_pct}%</h3>
|
| 78 |
+
<h3>Status: {risk_label}</h3>
|
| 79 |
<h4>Clinical Risk Factors:</h4>
|
| 80 |
<ul>
|
| 81 |
{''.join([f"<li>{f}</li>" for f in factors])}
|
|
|
|
| 87 |
|
| 88 |
return explanation
|
| 89 |
|
|
|
|
| 90 |
# PDF Generation
|
| 91 |
def generate_pdf(name, age, gender, platelets, hb, duration, calcium, potassium):
|
| 92 |
|