Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from groq import Groq
|
|
| 6 |
|
| 7 |
from reportlab.platypus import (
|
| 8 |
SimpleDocTemplate, Paragraph, Spacer,
|
| 9 |
-
Table, TableStyle
|
| 10 |
)
|
| 11 |
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
|
| 12 |
from reportlab.lib import colors
|
|
@@ -22,7 +22,7 @@ last_llm_report = ""
|
|
| 22 |
last_basic_data = {}
|
| 23 |
|
| 24 |
# ===============================
|
| 25 |
-
# LLM FUNCTION
|
| 26 |
# ===============================
|
| 27 |
def generate_llm_report(patient_info, prob_pct, risk_label, recommendation, factors):
|
| 28 |
|
|
@@ -62,7 +62,7 @@ End with reminder to consult qualified doctor.
|
|
| 62 |
|
| 63 |
|
| 64 |
# ===============================
|
| 65 |
-
# BASIC PREDICTION
|
| 66 |
# ===============================
|
| 67 |
def predict_basic(name, age, gender, platelets, hb, duration,
|
| 68 |
blood_bacteria, severity, medication):
|
|
@@ -124,78 +124,23 @@ def predict_basic(name, age, gender, platelets, hb, duration,
|
|
| 124 |
"prob_pct": prob_pct,
|
| 125 |
"risk_label": risk_label,
|
| 126 |
"recommendation": recommendation,
|
| 127 |
-
"factors": factors
|
|
|
|
| 128 |
}
|
| 129 |
|
| 130 |
-
urdu = f"""
|
| 131 |
-
<div style='background:#eef3fb;padding:10px;border-radius:8px;'>
|
| 132 |
-
<b>اردو خلاصہ:</b><br>
|
| 133 |
-
علاج ناکامی کا خطرہ <b>{prob_pct}%</b> ہے۔ درجہ: <b>{risk_label}</b>۔<br>
|
| 134 |
-
ڈاکٹر سے مشورہ ضرور کریں۔
|
| 135 |
-
</div>
|
| 136 |
-
"""
|
| 137 |
-
|
| 138 |
return f"""
|
| 139 |
<div style="font-family:Arial;padding:15px;">
|
| 140 |
<h2 style="color:{color};">Patient: {name}</h2>
|
| 141 |
<h3>Risk of Treatment Failure: {prob_pct}%</h3>
|
| 142 |
<h3>Status: {risk_label}</h3>
|
| 143 |
-
|
| 144 |
-
<h4>Clinical Risk Factors:</h4>
|
| 145 |
-
<ul>
|
| 146 |
-
{''.join([f"<li>{f}</li>" for f in factors]) if factors else "<li>None</li>"}
|
| 147 |
-
</ul>
|
| 148 |
-
|
| 149 |
<h4>Agent Recommendation:</h4>
|
| 150 |
<p><b>{recommendation}</b></p>
|
| 151 |
-
|
| 152 |
-
{urdu}
|
| 153 |
</div>
|
| 154 |
"""
|
| 155 |
|
| 156 |
|
| 157 |
# ===============================
|
| 158 |
-
#
|
| 159 |
-
# ===============================
|
| 160 |
-
def generate_detailed(*inputs):
|
| 161 |
-
global last_basic_data, last_llm_report
|
| 162 |
-
|
| 163 |
-
basic_html = predict_basic(*inputs)
|
| 164 |
-
|
| 165 |
-
d = last_basic_data
|
| 166 |
-
|
| 167 |
-
patient_info = f"""
|
| 168 |
-
Age: {d['age']}
|
| 169 |
-
Gender: {d['gender']}
|
| 170 |
-
Platelets: {d['platelets']}
|
| 171 |
-
Hemoglobin: {d['hb']}
|
| 172 |
-
Duration: {d['duration']}
|
| 173 |
-
Bacteria: {d['bacteria']}
|
| 174 |
-
Severity: {d['severity']}
|
| 175 |
-
Medication: {d['medication']}
|
| 176 |
-
"""
|
| 177 |
-
|
| 178 |
-
last_llm_report = generate_llm_report(
|
| 179 |
-
patient_info,
|
| 180 |
-
d["prob_pct"],
|
| 181 |
-
d["risk_label"],
|
| 182 |
-
d["recommendation"],
|
| 183 |
-
d["factors"]
|
| 184 |
-
)
|
| 185 |
-
|
| 186 |
-
formatted = last_llm_report.replace("\n", "<br>")
|
| 187 |
-
|
| 188 |
-
return basic_html + f"""
|
| 189 |
-
<hr>
|
| 190 |
-
<h3 style="color:#003366;">Detailed AI Report & Recommendations</h3>
|
| 191 |
-
<div style="background:#f9f9f9;padding:15px;border-radius:8px;">
|
| 192 |
-
{formatted}
|
| 193 |
-
</div>
|
| 194 |
-
"""
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
# ===============================
|
| 198 |
-
# PDF GENERATION (PROFESSIONAL)
|
| 199 |
# ===============================
|
| 200 |
def generate_pdf(*inputs):
|
| 201 |
|
|
@@ -207,11 +152,32 @@ def generate_pdf(*inputs):
|
|
| 207 |
elements = []
|
| 208 |
styles = getSampleStyleSheet()
|
| 209 |
|
| 210 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
elements.append(Paragraph("<b>TyphoidGuard AI Clinical Report</b>", styles["Title"]))
|
| 212 |
elements.append(Spacer(1, 0.3 * inch))
|
| 213 |
|
| 214 |
-
# ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
table_data = [
|
| 216 |
["Patient Name", d["name"]],
|
| 217 |
["Age", d["age"]],
|
|
@@ -223,39 +189,29 @@ def generate_pdf(*inputs):
|
|
| 223 |
["Severity", d["severity"]],
|
| 224 |
["Medication", d["medication"]],
|
| 225 |
["Predicted Risk %", d["prob_pct"]],
|
| 226 |
-
["Risk Level", d["risk_label"]],
|
| 227 |
]
|
| 228 |
|
| 229 |
table = Table(table_data, colWidths=[2.5*inch, 3*inch])
|
| 230 |
table.setStyle(TableStyle([
|
| 231 |
-
('BACKGROUND',(0,0),(-1,0),colors.lightgrey),
|
| 232 |
('GRID',(0,0),(-1,-1),1,colors.grey),
|
| 233 |
-
('
|
| 234 |
]))
|
| 235 |
|
| 236 |
elements.append(table)
|
| 237 |
elements.append(Spacer(1,0.4*inch))
|
| 238 |
|
| 239 |
-
# ----
|
| 240 |
elements.append(Paragraph("<b>Agent Recommendation</b>", styles["Heading2"]))
|
| 241 |
elements.append(Spacer(1,0.2*inch))
|
| 242 |
elements.append(Paragraph(d["recommendation"], styles["Normal"]))
|
| 243 |
elements.append(Spacer(1,0.4*inch))
|
| 244 |
|
| 245 |
-
# ---- LLM
|
| 246 |
if last_llm_report:
|
| 247 |
elements.append(Paragraph("<b>Detailed AI Report & Recommendations</b>", styles["Heading2"]))
|
| 248 |
elements.append(Spacer(1,0.2*inch))
|
| 249 |
elements.append(Paragraph(last_llm_report.replace("\n","<br/>"), styles["Normal"]))
|
| 250 |
-
elements.append(Spacer(1,0.4*inch))
|
| 251 |
-
|
| 252 |
-
# Disclaimer
|
| 253 |
-
elements.append(Paragraph(
|
| 254 |
-
"<i>This report is AI-generated. Final medical decisions must be made by a qualified doctor.</i>",
|
| 255 |
-
styles["Normal"]
|
| 256 |
-
))
|
| 257 |
|
| 258 |
-
# Team Footer
|
| 259 |
elements.append(Spacer(1,0.5*inch))
|
| 260 |
elements.append(Paragraph("<b>Team Chinar AI (AJK)</b>", styles["Normal"]))
|
| 261 |
elements.append(Paragraph(
|
|
@@ -279,6 +235,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 279 |
#### Team Chinar AI (AJK)
|
| 280 |
""")
|
| 281 |
|
|
|
|
|
|
|
|
|
|
| 282 |
with gr.Row():
|
| 283 |
name = gr.Textbox(label="Patient Name")
|
| 284 |
age = gr.Number(label="Age")
|
|
@@ -303,14 +262,38 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 303 |
label="Current Medication"
|
| 304 |
)
|
| 305 |
|
| 306 |
-
# ---- BUTTONS
|
| 307 |
with gr.Row():
|
| 308 |
-
predict_btn = gr.Button("Basic Risk Assessment",
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
output = gr.HTML()
|
| 313 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
predict_btn.click(predict_basic,
|
| 315 |
inputs=[name,age,gender,platelets,hb,duration,
|
| 316 |
blood_bacteria,severity,medication],
|
|
|
|
| 6 |
|
| 7 |
from reportlab.platypus import (
|
| 8 |
SimpleDocTemplate, Paragraph, Spacer,
|
| 9 |
+
Table, TableStyle, Image
|
| 10 |
)
|
| 11 |
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
|
| 12 |
from reportlab.lib import colors
|
|
|
|
| 22 |
last_basic_data = {}
|
| 23 |
|
| 24 |
# ===============================
|
| 25 |
+
# LLM FUNCTION (UNCHANGED)
|
| 26 |
# ===============================
|
| 27 |
def generate_llm_report(patient_info, prob_pct, risk_label, recommendation, factors):
|
| 28 |
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
# ===============================
|
| 65 |
+
# BASIC PREDICTION (UNCHANGED)
|
| 66 |
# ===============================
|
| 67 |
def predict_basic(name, age, gender, platelets, hb, duration,
|
| 68 |
blood_bacteria, severity, medication):
|
|
|
|
| 124 |
"prob_pct": prob_pct,
|
| 125 |
"risk_label": risk_label,
|
| 126 |
"recommendation": recommendation,
|
| 127 |
+
"factors": factors,
|
| 128 |
+
"color": color
|
| 129 |
}
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
return 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 |
<h4>Agent Recommendation:</h4>
|
| 137 |
<p><b>{recommendation}</b></p>
|
|
|
|
|
|
|
| 138 |
</div>
|
| 139 |
"""
|
| 140 |
|
| 141 |
|
| 142 |
# ===============================
|
| 143 |
+
# PDF GENERATION (UPDATED)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
# ===============================
|
| 145 |
def generate_pdf(*inputs):
|
| 146 |
|
|
|
|
| 152 |
elements = []
|
| 153 |
styles = getSampleStyleSheet()
|
| 154 |
|
| 155 |
+
# ---- LOGO ----
|
| 156 |
+
if os.path.exists("logo.png"):
|
| 157 |
+
elements.append(Image("logo.png", width=1.5*inch, height=1.5*inch))
|
| 158 |
+
elements.append(Spacer(1,0.2*inch))
|
| 159 |
+
|
| 160 |
+
# ---- TITLE ----
|
| 161 |
elements.append(Paragraph("<b>TyphoidGuard AI Clinical Report</b>", styles["Title"]))
|
| 162 |
elements.append(Spacer(1, 0.3 * inch))
|
| 163 |
|
| 164 |
+
# ---- RISK BADGE ----
|
| 165 |
+
badge_color = colors.red if "HIGH" in d["risk_label"] else \
|
| 166 |
+
colors.orange if "MODERATE" in d["risk_label"] else colors.green
|
| 167 |
+
|
| 168 |
+
badge = Table([[d["risk_label"]]], colWidths=[2*inch])
|
| 169 |
+
badge.setStyle(TableStyle([
|
| 170 |
+
('BACKGROUND',(0,0),(-1,-1),badge_color),
|
| 171 |
+
('TEXTCOLOR',(0,0),(-1,-1),colors.white),
|
| 172 |
+
('ALIGN',(0,0),(-1,-1),'CENTER'),
|
| 173 |
+
('FONTSIZE',(0,0),(-1,-1),14),
|
| 174 |
+
('GRID',(0,0),(-1,-1),1,colors.black)
|
| 175 |
+
]))
|
| 176 |
+
|
| 177 |
+
elements.append(badge)
|
| 178 |
+
elements.append(Spacer(1,0.3*inch))
|
| 179 |
+
|
| 180 |
+
# ---- PATIENT TABLE ----
|
| 181 |
table_data = [
|
| 182 |
["Patient Name", d["name"]],
|
| 183 |
["Age", d["age"]],
|
|
|
|
| 189 |
["Severity", d["severity"]],
|
| 190 |
["Medication", d["medication"]],
|
| 191 |
["Predicted Risk %", d["prob_pct"]],
|
|
|
|
| 192 |
]
|
| 193 |
|
| 194 |
table = Table(table_data, colWidths=[2.5*inch, 3*inch])
|
| 195 |
table.setStyle(TableStyle([
|
|
|
|
| 196 |
('GRID',(0,0),(-1,-1),1,colors.grey),
|
| 197 |
+
('BACKGROUND',(0,0),(-1,0),colors.lightgrey)
|
| 198 |
]))
|
| 199 |
|
| 200 |
elements.append(table)
|
| 201 |
elements.append(Spacer(1,0.4*inch))
|
| 202 |
|
| 203 |
+
# ---- RECOMMENDATION ----
|
| 204 |
elements.append(Paragraph("<b>Agent Recommendation</b>", styles["Heading2"]))
|
| 205 |
elements.append(Spacer(1,0.2*inch))
|
| 206 |
elements.append(Paragraph(d["recommendation"], styles["Normal"]))
|
| 207 |
elements.append(Spacer(1,0.4*inch))
|
| 208 |
|
| 209 |
+
# ---- LLM ----
|
| 210 |
if last_llm_report:
|
| 211 |
elements.append(Paragraph("<b>Detailed AI Report & Recommendations</b>", styles["Heading2"]))
|
| 212 |
elements.append(Spacer(1,0.2*inch))
|
| 213 |
elements.append(Paragraph(last_llm_report.replace("\n","<br/>"), styles["Normal"]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
|
|
|
| 215 |
elements.append(Spacer(1,0.5*inch))
|
| 216 |
elements.append(Paragraph("<b>Team Chinar AI (AJK)</b>", styles["Normal"]))
|
| 217 |
elements.append(Paragraph(
|
|
|
|
| 235 |
#### Team Chinar AI (AJK)
|
| 236 |
""")
|
| 237 |
|
| 238 |
+
if os.path.exists("logo.png"):
|
| 239 |
+
gr.Image("logo.png", width=150)
|
| 240 |
+
|
| 241 |
with gr.Row():
|
| 242 |
name = gr.Textbox(label="Patient Name")
|
| 243 |
age = gr.Number(label="Age")
|
|
|
|
| 262 |
label="Current Medication"
|
| 263 |
)
|
| 264 |
|
| 265 |
+
# ---- BUTTONS (ALL COLORED) ----
|
| 266 |
with gr.Row():
|
| 267 |
+
predict_btn = gr.Button("Basic Risk Assessment",
|
| 268 |
+
elem_id="btn1")
|
| 269 |
+
detailed_btn = gr.Button("Detailed Report & Recommendations",
|
| 270 |
+
elem_id="btn2")
|
| 271 |
+
pdf_btn = gr.Button("Generate PDF Report",
|
| 272 |
+
elem_id="btn3")
|
| 273 |
|
| 274 |
output = gr.HTML()
|
| 275 |
|
| 276 |
+
# ---- TEAM MEMBERS ON PAGE ----
|
| 277 |
+
gr.Markdown("""
|
| 278 |
+
---
|
| 279 |
+
### 👥 Team Members – Chinar AI
|
| 280 |
+
Anees Qumar Abbasi (Team Lead)
|
| 281 |
+
Sharafat Hussain
|
| 282 |
+
Salma Asghar
|
| 283 |
+
Kaleem Hussain
|
| 284 |
+
Munazza Zahra
|
| 285 |
+
Kokub Khurishid
|
| 286 |
+
""")
|
| 287 |
+
|
| 288 |
+
# ---- CUSTOM CSS ----
|
| 289 |
+
gr.HTML("""
|
| 290 |
+
<style>
|
| 291 |
+
#btn1 {background-color:#1565c0;color:white;}
|
| 292 |
+
#btn2 {background-color:#2e7d32;color:white;}
|
| 293 |
+
#btn3 {background-color:#455a64;color:white;}
|
| 294 |
+
</style>
|
| 295 |
+
""")
|
| 296 |
+
|
| 297 |
predict_btn.click(predict_basic,
|
| 298 |
inputs=[name,age,gender,platelets,hb,duration,
|
| 299 |
blood_bacteria,severity,medication],
|