aneesqumar commited on
Commit
b9c48cc
·
verified ·
1 Parent(s): bad4863

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -19
app.py CHANGED
@@ -12,9 +12,7 @@ from reportlab.platypus import TableStyle
12
  # Load model
13
  model = joblib.load("typhoidguard_model_reduced.pkl")
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
@@ -33,13 +31,18 @@ def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, po
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"
@@ -61,7 +64,7 @@ def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, po
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")
@@ -74,7 +77,7 @@ def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, po
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>
@@ -90,43 +93,125 @@ def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, po
90
  """
91
 
92
  return explanation
 
 
93
  # PDF Generation
 
94
  def generate_pdf(name, age, gender, platelets, hb, duration, calcium, potassium):
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  file_path = f"{name}_Typhoid_Report.pdf"
97
  doc = SimpleDocTemplate(file_path)
98
  elements = []
99
  styles = getSampleStyleSheet()
100
 
 
101
  elements.append(Paragraph("<b>TyphoidGuard AI Clinical Report</b>", styles["Title"]))
102
- elements.append(Spacer(1, 0.5 * inch))
103
 
104
- data = [
 
105
  ["Patient Name", name],
106
  ["Age", age],
107
  ["Gender", gender],
108
  ["Platelet Count", platelets],
109
- ["Hemoglobin", hb],
110
- ["Treatment Duration", duration],
111
- ["Calcium", calcium],
112
- ["Potassium", potassium],
 
 
113
  ]
114
 
115
- table = Table(data)
116
  table.setStyle(TableStyle([
117
  ('BACKGROUND', (0,0), (-1,0), colors.lightblue),
118
- ('GRID', (0,0), (-1,-1), 1, colors.grey)
 
 
 
 
119
  ]))
120
-
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
128
-
129
-
130
  # Interface
131
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
132
 
@@ -177,7 +262,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
177
  - Munazza Zahra
178
  - Kokub Khurishid
179
 
180
- © 2026 Team Kohsar AI – All Rights Reserved
181
  """)
182
 
183
  demo.launch()
 
12
  # Load model
13
  model = joblib.load("typhoidguard_model_reduced.pkl")
14
 
 
15
  # Risk & Agentic Logic
 
16
  def predict_and_generate(name, age, gender, platelets, hb, duration, calcium, potassium):
17
 
18
  # 1️⃣ Prepare data with exact column names
 
31
 
32
  # 3️⃣ Model probability
33
  prob = model.named_steps["classifier"].predict_proba(X_processed)[0][1]
 
34
 
35
+ # 4️⃣ Clinical thresholds
36
  low_platelets = platelets < 150000
37
  low_hb = hb < 10
38
  prolonged_treatment = duration > 7
39
 
40
+ # 5️⃣ Adjust probability for extreme lab values
41
+ if low_platelets or low_hb:
42
+ prob_pct = 95.0 # clinically override
43
+ else:
44
+ prob_pct = round(prob * 100, 2)
45
+
46
  # Default risk label based on probability
47
  if prob > 0.65:
48
  risk_label = "🔴 HIGH RISK"
 
64
  else:
65
  recommendation = "Continue standard treatment protocol."
66
 
67
+ # 6️⃣ Risk factors list
68
  factors = []
69
  if low_platelets:
70
  factors.append("Low Platelet Count")
 
77
  if potassium < 3.5 or potassium > 5.0:
78
  factors.append("Abnormal Potassium")
79
 
80
+ # 7️⃣ Construct HTML
81
  explanation = f"""
82
  <div style="font-family:Arial; padding:15px;">
83
  <h2 style="color:{color};">Patient: {name}</h2>
 
93
  """
94
 
95
  return explanation
96
+
97
+
98
  # PDF Generation
99
+ # PDF Generation (with color coding & disclaimer)
100
  def generate_pdf(name, age, gender, platelets, hb, duration, calcium, potassium):
101
 
102
+ # 1️⃣ Prepare data for model & agentic logic
103
+ data = pd.DataFrame([{
104
+ "Platelet Count": platelets,
105
+ "Age": age,
106
+ "Hemoglobin (g/dL)": hb,
107
+ "Treatment Duration": duration,
108
+ "Calcium (mg/dL)": calcium,
109
+ "Potassium (mmol/L)": potassium,
110
+ "Gender": gender
111
+ }])
112
+
113
+ X_processed = model.named_steps["preprocessor"].transform(data)
114
+ prob = model.named_steps["classifier"].predict_proba(X_processed)[0][1]
115
+
116
+ low_platelets = platelets < 150000
117
+ low_hb = hb < 10
118
+ prolonged_treatment = duration > 7
119
+
120
+ # Adjust probability for critical lab values
121
+ if low_platelets or low_hb:
122
+ prob_pct = 95.0
123
+ else:
124
+ prob_pct = round(prob * 100, 2)
125
+
126
+ # Determine status and recommendation
127
+ if prob > 0.65:
128
+ risk_label = "HIGH RISK"
129
+ color = colors.red
130
+ elif prob >= 0.35:
131
+ risk_label = "MODERATE RISK"
132
+ color = colors.orange
133
+ else:
134
+ risk_label = "LOW RISK"
135
+ color = colors.green
136
+
137
+ if low_platelets or low_hb:
138
+ risk_label = "HIGH RISK"
139
+ color = colors.red
140
+ recommendation = "Immediate referral to secondary care facility recommended. Close monitoring required."
141
+ elif prob >= 0.35:
142
+ recommendation = "Close monitoring advised. Repeat blood tests within 48 hours."
143
+ else:
144
+ recommendation = "Continue standard treatment protocol."
145
+
146
+ # Clinical risk factors
147
+ factors = []
148
+ if low_platelets:
149
+ factors.append("Low Platelet Count")
150
+ if low_hb:
151
+ factors.append("Low Hemoglobin")
152
+ if prolonged_treatment:
153
+ factors.append("Prolonged Treatment Duration")
154
+ if calcium < 8.5:
155
+ factors.append("Low Calcium")
156
+ if potassium < 3.5 or potassium > 5.0:
157
+ factors.append("Abnormal Potassium")
158
+
159
+ # 2️⃣ Generate PDF
160
  file_path = f"{name}_Typhoid_Report.pdf"
161
  doc = SimpleDocTemplate(file_path)
162
  elements = []
163
  styles = getSampleStyleSheet()
164
 
165
+ # Title
166
  elements.append(Paragraph("<b>TyphoidGuard AI Clinical Report</b>", styles["Title"]))
167
+ elements.append(Spacer(1, 0.3 * inch))
168
 
169
+ # Patient Info Table
170
+ patient_data = [
171
  ["Patient Name", name],
172
  ["Age", age],
173
  ["Gender", gender],
174
  ["Platelet Count", platelets],
175
+ ["Hemoglobin (g/dL)", hb],
176
+ ["Treatment Duration (Days)", duration],
177
+ ["Calcium (mg/dL)", calcium],
178
+ ["Potassium (mmol/L)", potassium],
179
+ ["Risk of Treatment Failure (%)", prob_pct],
180
+ ["Status", risk_label],
181
  ]
182
 
183
+ table = Table(patient_data, colWidths=[200, 250])
184
  table.setStyle(TableStyle([
185
  ('BACKGROUND', (0,0), (-1,0), colors.lightblue),
186
+ ('GRID', (0,0), (-1,-1), 1, colors.grey),
187
+ ('FONTNAME', (0,0), (-1,0), 'Helvetica-Bold'),
188
+ ('VALIGN', (0,0), (-1,-1), 'MIDDLE'),
189
+ # Color coding for risk status
190
+ ('TEXTCOLOR', (-1,-1), (-1,-1), color)
191
  ]))
 
192
  elements.append(table)
193
+ elements.append(Spacer(1, 0.3 * inch))
194
+
195
+ # Clinical Risk Factors
196
+ if factors:
197
+ elements.append(Paragraph("<b>Clinical Risk Factors:</b>", styles["Heading3"]))
198
+ for f in factors:
199
+ elements.append(Paragraph(f"- {f}", styles["Normal"]))
200
+ elements.append(Spacer(1, 0.2 * inch))
201
+
202
+ # Agent Recommendation
203
+ elements.append(Paragraph("<b>Agent Recommendation:</b>", styles["Heading3"]))
204
+ elements.append(Paragraph(recommendation, styles["Normal"]))
205
+ elements.append(Spacer(1, 0.3 * inch))
206
+
207
+ # Footer
208
  elements.append(Paragraph("Generated under HEC Generative AI Training Hackathon", styles["Normal"]))
209
  elements.append(Paragraph("Team Chinar AI - AJK", styles["Normal"]))
210
+ elements.append(Spacer(1, 0.2 * inch))
211
+ elements.append(Paragraph("<i>Disclaimer: This report is AI-generated. Please consult a qualified doctor for clinical decisions.</i>", styles["Normal"]))
212
 
213
  doc.build(elements)
214
  return file_path
 
 
215
  # Interface
216
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
217
 
 
262
  - Munazza Zahra
263
  - Kokub Khurishid
264
 
265
+ © 2026 Team Chinar AI – All Rights Reserved
266
  """)
267
 
268
  demo.launch()