aneesqumar commited on
Commit
07dc94f
·
verified ·
1 Parent(s): f9a4b01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -16
app.py CHANGED
@@ -16,7 +16,7 @@ model = joblib.load("typhoidguard_model_reduced.pkl")
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,
@@ -27,17 +27,14 @@ def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, po
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"
@@ -48,20 +45,29 @@ def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, po
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")
64
- if hb < 10:
65
  factors.append("Low Hemoglobin")
66
  if duration > 7:
67
  factors.append("Prolonged Treatment Duration")
@@ -70,7 +76,7 @@ def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, po
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>
@@ -86,7 +92,6 @@ def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, po
86
  """
87
 
88
  return explanation
89
-
90
  # PDF Generation
91
  def generate_pdf(name, age, gender, platelets, hb, duration, calcium, potassium):
92
 
 
16
  # Risk & Agentic Logic
17
  def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, potassium):
18
 
19
+ # 1️⃣ Prepare input DataFrame
20
  data = pd.DataFrame([{
21
  "Platelet Count": platelets,
22
  "Age": age,
 
27
  "Gender": gender
28
  }])
29
 
30
+ # 2️⃣ Pass data through preprocessor
31
+ X_processed = model.named_steps["preprocessor"].transform(data)
 
 
 
 
 
32
 
33
+ # 3️⃣ Get probability from classifier
34
+ prob = model.named_steps["classifier"].predict_proba(X_processed)[0][1] # probability of treatment failure
35
  prob_pct = round(prob * 100, 2)
36
 
37
+ # 4️⃣ Risk label based on probability
38
  if prob > 0.65:
39
  risk_label = "🔴 HIGH RISK"
40
  color = "#d62828"
 
45
  risk_label = "🟢 LOW RISK"
46
  color = "#2a9d8f"
47
 
48
+ # 5️⃣ Agentic triage logic (include critical thresholds)
49
  patient_data = data.iloc[0].to_dict()
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
  elif 0.35 <= prob <= 0.65:
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
+ # 6️⃣ Top clinical factors
67
  factors = []
68
+ if low_platelets:
69
  factors.append("Low Platelet Count")
70
+ if low_hb:
71
  factors.append("Low Hemoglobin")
72
  if duration > 7:
73
  factors.append("Prolonged Treatment Duration")
 
76
  if potassium < 3.5 or potassium > 5.0:
77
  factors.append("Abnormal Potassium")
78
 
79
+ # 7️⃣ Construct HTML
80
  explanation = f"""
81
  <div style="font-family:Arial; padding:15px;">
82
  <h2 style="color:{color};">Patient: {name}</h2>
 
92
  """
93
 
94
  return explanation
 
95
  # PDF Generation
96
  def generate_pdf(name, age, gender, platelets, hb, duration, calcium, potassium):
97