aneesqumar commited on
Commit
5287c3c
·
verified ·
1 Parent(s): 8f3f7e5

Delete app2(working).py

Browse files
Files changed (1) hide show
  1. app2(working).py +0 -308
app2(working).py DELETED
@@ -1,308 +0,0 @@
1
- import gradio as gr
2
- import joblib
3
- import pandas as pd
4
- import numpy as np
5
- import os
6
- from groq import Groq
7
-
8
- from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table
9
- from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
10
- from reportlab.lib import colors
11
- from reportlab.lib.units import inch
12
- from reportlab.platypus import TableStyle
13
-
14
- # ===============================
15
- # Load Model
16
- # ===============================
17
- model = joblib.load("typhoidguard_model_reduced2.pkl")
18
-
19
- # Groq Client
20
- client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
21
-
22
- # Cache LLM result (avoid double call)
23
- last_llm_report = ""
24
- last_basic_output = ""
25
-
26
-
27
- # ===============================
28
- # LLM FUNCTION
29
- # ===============================
30
- def generate_llm_report(patient_info, prob_pct, risk_label, recommendation, factors):
31
-
32
- prompt = f"""
33
- You are a responsible medical AI assistant helping rural healthcare units in Pakistan.
34
-
35
- Patient Details:
36
- {patient_info}
37
-
38
- Predicted Risk of Treatment Failure: {prob_pct}%
39
- Risk Category: {risk_label}
40
-
41
- Clinical Risk Factors:
42
- {", ".join(factors) if factors else "None"}
43
-
44
- Agent Recommendation:
45
- {recommendation}
46
-
47
- Generate a structured detailed report including:
48
- 1. Clinical Interpretation
49
- 2. Possible Complications
50
- 3. Immediate Precautions
51
- 4. Preventative Measures
52
- 5. Follow-up Recommendations
53
-
54
- Use professional formatting.
55
- End with reminder that final decision must be taken by qualified doctor.
56
- """
57
-
58
- response = client.chat.completions.create(
59
- model="llama-3.3-70b-versatile",
60
- messages=[{"role": "user", "content": prompt}],
61
- temperature=0.4,
62
- )
63
-
64
- return response.choices[0].message.content
65
-
66
-
67
- # ===============================
68
- # PREDICT FUNCTION (UPDATED FEATURES)
69
- # ===============================
70
- def predict_basic(name, age, gender, platelets, hb, duration,
71
- blood_bacteria, severity, medication):
72
-
73
- global last_llm_report, last_basic_output
74
- last_llm_report = "" # reset
75
-
76
- data = pd.DataFrame([{
77
- "Age": age,
78
- "Platelet Count": platelets,
79
- "Hemoglobin (g/dL)": hb,
80
- "Treatment Duration": duration,
81
- "Gender": gender,
82
- "Blood Culture Bacteria": blood_bacteria,
83
- "Symptoms Severity": severity,
84
- "Current Medication": medication
85
- }])
86
-
87
- prob = model.predict_proba(data)[0][1]
88
- prob_pct = round(prob * 100, 2)
89
-
90
- # Agentic Override Logic
91
- low_platelets = platelets < 150000
92
- low_hb = hb < 10
93
- prolonged_treatment = duration > 7
94
-
95
- if low_platelets or low_hb:
96
- prob_pct = 90.0
97
- risk_label = "🔴 HIGH RISK"
98
- color = "#d62828"
99
- recommendation = "Immediate referral to secondary care facility recommended."
100
- elif prob >= 0.65:
101
- risk_label = "🔴 HIGH RISK"
102
- color = "#d62828"
103
- recommendation = "Urgent clinical evaluation required."
104
- elif prob >= 0.35:
105
- risk_label = "🟡 MODERATE RISK"
106
- color = "#f4a261"
107
- recommendation = "Close monitoring advised. Repeat tests in 48 hours."
108
- else:
109
- risk_label = "🟢 LOW RISK"
110
- color = "#2a9d8f"
111
- recommendation = "Continue standard treatment protocol."
112
-
113
- factors = []
114
- if low_platelets:
115
- factors.append("Low Platelet Count")
116
- if low_hb:
117
- factors.append("Low Hemoglobin")
118
- if prolonged_treatment:
119
- factors.append("Prolonged Treatment Duration")
120
- if severity == "High":
121
- factors.append("High Symptom Severity")
122
-
123
- urdu_summary = f"""
124
- <div style='background:#eef7ff;padding:10px;border-radius:8px;'>
125
- <b>اردو خلاصہ:</b><br>
126
- مریض میں علاج ناکامی کا خطرہ <b>{prob_pct}%</b> ہے۔ موجودہ کیفیت: <b>{risk_label}</b>۔<br>
127
- براہ کرم ڈاکٹر سے مشورہ ضرور کریں۔
128
- </div>
129
- """
130
-
131
- basic_output = f"""
132
- <div style="font-family:Arial; padding:15px;">
133
- <h2 style="color:{color};">Patient: {name}</h2>
134
- <h3>Risk of Treatment Failure: {prob_pct}%</h3>
135
- <h3>Status: {risk_label}</h3>
136
-
137
- <h4>Clinical Risk Factors:</h4>
138
- <ul>
139
- {''.join([f"<li>{f}</li>" for f in factors]) if factors else "<li>None</li>"}
140
- </ul>
141
-
142
- <h4>Agent Recommendation:</h4>
143
- <p><b>{recommendation}</b></p>
144
-
145
- {urdu_summary}
146
- </div>
147
- """
148
-
149
- last_basic_output = basic_output
150
-
151
- return basic_output
152
-
153
-
154
- # ===============================
155
- # DETAILED REPORT BUTTON
156
- # ===============================
157
- def generate_detailed(name, age, gender, platelets, hb, duration,
158
- blood_bacteria, severity, medication):
159
-
160
- global last_llm_report, last_basic_output
161
-
162
- basic_html = predict_basic(name, age, gender, platelets, hb,
163
- duration, blood_bacteria, severity, medication)
164
-
165
- data = pd.DataFrame([{
166
- "Age": age,
167
- "Platelet Count": platelets,
168
- "Hemoglobin (g/dL)": hb,
169
- "Treatment Duration": duration,
170
- "Gender": gender,
171
- "Blood Culture Bacteria": blood_bacteria,
172
- "Symptoms Severity": severity,
173
- "Current Medication": medication
174
- }])
175
-
176
- prob = model.predict_proba(data)[0][1]
177
- prob_pct = round(prob * 100, 2)
178
-
179
- factors = []
180
- if platelets < 150000:
181
- factors.append("Low Platelet Count")
182
- if hb < 10:
183
- factors.append("Low Hemoglobin")
184
-
185
- patient_info = f"""
186
- Age: {age}
187
- Gender: {gender}
188
- Platelets: {platelets}
189
- Hemoglobin: {hb}
190
- Treatment Duration: {duration}
191
- Blood Bacteria: {blood_bacteria}
192
- Severity: {severity}
193
- Medication: {medication}
194
- """
195
-
196
- llm_report = generate_llm_report(
197
- patient_info, prob_pct, "See Above", "See Above", factors
198
- )
199
-
200
- last_llm_report = llm_report
201
-
202
- formatted_llm = llm_report.replace("\n", "<br>")
203
-
204
- return basic_html + f"""
205
- <hr>
206
- <h3 style="color:#003366;">🧠 Detailed AI Report & Recommendations</h3>
207
- <div style="background:#f8f9fa;padding:15px;border-radius:10px;line-height:1.6;">
208
- {formatted_llm}
209
- </div>
210
- """
211
-
212
-
213
- # ===============================
214
- # PDF GENERATION
215
- # ===============================
216
- def generate_pdf(name, age, gender, platelets, hb, duration,
217
- blood_bacteria, severity, medication):
218
-
219
- global last_llm_report, last_basic_output
220
-
221
- file_path = f"{name}_Typhoid_Report.pdf"
222
- doc = SimpleDocTemplate(file_path)
223
- elements = []
224
- styles = getSampleStyleSheet()
225
-
226
- elements.append(Paragraph("<b>TyphoidGuard AI Clinical Report</b>", styles["Title"]))
227
- elements.append(Spacer(1, 0.3 * inch))
228
-
229
- # Basic Output
230
- elements.append(Paragraph(last_basic_output.replace("<br>", "<br/>"), styles["Normal"]))
231
- elements.append(Spacer(1, 0.3 * inch))
232
-
233
- # Detailed LLM Output (if exists)
234
- if last_llm_report:
235
- elements.append(Paragraph("<b>Detailed AI Report</b>", styles["Heading2"]))
236
- elements.append(Spacer(1, 0.2 * inch))
237
- elements.append(Paragraph(last_llm_report.replace("\n", "<br/>"), styles["Normal"]))
238
- elements.append(Spacer(1, 0.3 * inch))
239
-
240
- elements.append(Paragraph(
241
- "<i>Disclaimer: This report is AI-generated. Please consult a qualified doctor before making clinical decisions.</i>",
242
- styles["Normal"]
243
- ))
244
-
245
- doc.build(elements)
246
- return file_path
247
-
248
-
249
- # ===============================
250
- # INTERFACE
251
- # ===============================
252
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
253
-
254
- gr.Markdown("# 🏥 TyphoidGuard AI")
255
- gr.Markdown("### HEC Generative AI Training Hackathon – Pakistan")
256
-
257
- with gr.Row():
258
- name = gr.Textbox(label="Patient Name")
259
- age = gr.Number(label="Age")
260
- gender = gr.Radio(["Male", "Female"], label="Gender")
261
-
262
- with gr.Row():
263
- platelets = gr.Number(label="Platelet Count")
264
- hb = gr.Number(label="Hemoglobin (g/dL)")
265
- duration = gr.Number(label="Treatment Duration (Days)")
266
-
267
- with gr.Row():
268
- blood_bacteria = gr.Dropdown(
269
- ["Escherichia coli", "Salmonella typhi", "Unknown", "Staphylococcus"],
270
- label="Blood Culture Bacteria"
271
- )
272
- severity = gr.Dropdown(
273
- ["Low", "Moderate", "High", "Unknown"],
274
- label="Symptoms Severity"
275
- )
276
- medication = gr.Dropdown(
277
- ["Amoxicillin", "Ceftriaxone", "Azithromycin"],
278
- label="Current Medication"
279
- )
280
-
281
- output = gr.HTML()
282
-
283
- predict_btn = gr.Button("🔍 Basic Risk Assessment")
284
- detailed_btn = gr.Button("🧠 Detailed Report and Recommendations")
285
- pdf_btn = gr.Button("📄 Generate PDF Report")
286
-
287
- predict_btn.click(
288
- predict_basic,
289
- inputs=[name, age, gender, platelets, hb, duration,
290
- blood_bacteria, severity, medication],
291
- outputs=output
292
- )
293
-
294
- detailed_btn.click(
295
- generate_detailed,
296
- inputs=[name, age, gender, platelets, hb, duration,
297
- blood_bacteria, severity, medication],
298
- outputs=output
299
- )
300
-
301
- pdf_btn.click(
302
- generate_pdf,
303
- inputs=[name, age, gender, platelets, hb, duration,
304
- blood_bacteria, severity, medication],
305
- outputs=gr.File()
306
- )
307
-
308
- demo.launch()