Update app.py
Browse files
app.py
CHANGED
|
@@ -204,7 +204,7 @@ Medication: {d['medication']}
|
|
| 204 |
# ===============================
|
| 205 |
# PDF GENERATION
|
| 206 |
# ===============================
|
| 207 |
-
def generate_pdf(*inputs):
|
| 208 |
|
| 209 |
global last_basic_data, last_llm_report
|
| 210 |
|
|
@@ -280,7 +280,147 @@ def generate_pdf(*inputs):
|
|
| 280 |
|
| 281 |
doc.build(elements)
|
| 282 |
return file_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
| 285 |
# ===============================
|
| 286 |
# INTERFACE
|
|
|
|
| 204 |
# ===============================
|
| 205 |
# PDF GENERATION
|
| 206 |
# ===============================
|
| 207 |
+
'''def generate_pdf(*inputs):
|
| 208 |
|
| 209 |
global last_basic_data, last_llm_report
|
| 210 |
|
|
|
|
| 280 |
|
| 281 |
doc.build(elements)
|
| 282 |
return file_path
|
| 283 |
+
'''
|
| 284 |
+
def generate_pdf(*inputs):
|
| 285 |
+
|
| 286 |
+
global last_basic_data, last_llm_report
|
| 287 |
+
|
| 288 |
+
d = last_basic_data
|
| 289 |
+
file_path = f"{d['name']}_Typhoid_Report.pdf"
|
| 290 |
+
doc = SimpleDocTemplate(file_path)
|
| 291 |
+
elements = []
|
| 292 |
+
styles = getSampleStyleSheet()
|
| 293 |
+
|
| 294 |
+
# ---- Custom Styles ----
|
| 295 |
+
heading_style = ParagraphStyle(
|
| 296 |
+
name="CustomHeading",
|
| 297 |
+
parent=styles["Heading2"],
|
| 298 |
+
fontSize=14,
|
| 299 |
+
spaceAfter=6,
|
| 300 |
+
textColor=colors.black
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
+
bullet_style = ParagraphStyle(
|
| 304 |
+
name="BulletStyle",
|
| 305 |
+
parent=styles["Normal"],
|
| 306 |
+
leftIndent=15,
|
| 307 |
+
bulletIndent=5,
|
| 308 |
+
spaceAfter=4
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
disclaimer_heading_style = ParagraphStyle(
|
| 312 |
+
name="DisclaimerHeading",
|
| 313 |
+
parent=styles["Heading2"],
|
| 314 |
+
textColor=colors.red,
|
| 315 |
+
fontSize=13,
|
| 316 |
+
spaceBefore=15
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
disclaimer_text_style = ParagraphStyle(
|
| 320 |
+
name="DisclaimerText",
|
| 321 |
+
parent=styles["Normal"],
|
| 322 |
+
fontSize=10,
|
| 323 |
+
textColor=colors.grey
|
| 324 |
+
)
|
| 325 |
+
|
| 326 |
+
# ---- LOGO ----
|
| 327 |
+
if os.path.exists(LOGO_PATH):
|
| 328 |
+
elements.append(Image(LOGO_PATH, width=1.5*inch, height=1.5*inch))
|
| 329 |
+
elements.append(Spacer(1,0.2*inch))
|
| 330 |
+
|
| 331 |
+
elements.append(Paragraph("<b>TyphoidGuard AI Clinical Report</b>", styles["Title"]))
|
| 332 |
+
elements.append(Spacer(1, 0.3 * inch))
|
| 333 |
+
|
| 334 |
+
# ---- RISK BADGE (Improved Height & Padding) ----
|
| 335 |
+
risk_color = colors.HexColor(d["color"])
|
| 336 |
+
badge_style = ParagraphStyle(
|
| 337 |
+
name="Badge",
|
| 338 |
+
parent=styles["Normal"],
|
| 339 |
+
backColor=risk_color,
|
| 340 |
+
textColor=colors.white,
|
| 341 |
+
fontSize=13,
|
| 342 |
+
leading=18,
|
| 343 |
+
leftIndent=10,
|
| 344 |
+
rightIndent=10,
|
| 345 |
+
spaceBefore=10,
|
| 346 |
+
spaceAfter=15
|
| 347 |
+
)
|
| 348 |
|
| 349 |
+
elements.append(Paragraph(
|
| 350 |
+
f"<b>Risk: {d['prob_pct']}% - {d['risk_label']}</b>",
|
| 351 |
+
badge_style
|
| 352 |
+
))
|
| 353 |
+
elements.append(Spacer(1,0.3*inch))
|
| 354 |
+
|
| 355 |
+
# ---- PATIENT TABLE ----
|
| 356 |
+
table_data = [
|
| 357 |
+
["Patient Name", d["name"]],
|
| 358 |
+
["Age", d["age"]],
|
| 359 |
+
["Gender", d["gender"]],
|
| 360 |
+
["Platelet Count", d["platelets"]],
|
| 361 |
+
["Hemoglobin", d["hb"]],
|
| 362 |
+
["Treatment Duration", d["duration"]],
|
| 363 |
+
["Bacteria", d["bacteria"]],
|
| 364 |
+
["Severity", d["severity"]],
|
| 365 |
+
["Medication", d["medication"]],
|
| 366 |
+
]
|
| 367 |
+
|
| 368 |
+
table = Table(table_data, colWidths=[2.5*inch, 3*inch])
|
| 369 |
+
table.setStyle(TableStyle([
|
| 370 |
+
('GRID',(0,0),(-1,-1),1,colors.grey),
|
| 371 |
+
('FONTNAME',(0,0),(-1,-1),'Helvetica')
|
| 372 |
+
]))
|
| 373 |
+
|
| 374 |
+
elements.append(table)
|
| 375 |
+
elements.append(Spacer(1,0.4*inch))
|
| 376 |
+
|
| 377 |
+
# ---- AGENT RECOMMENDATION ----
|
| 378 |
+
elements.append(Paragraph("<b>Agent Recommendation</b>", heading_style))
|
| 379 |
+
elements.append(Paragraph(d["recommendation"], styles["Normal"]))
|
| 380 |
+
elements.append(Spacer(1,0.4*inch))
|
| 381 |
+
|
| 382 |
+
# ---- DETAILED AI REPORT (Formatted with Headings + Bullets) ----
|
| 383 |
+
if last_llm_report:
|
| 384 |
+
elements.append(Paragraph("<b>Detailed AI Report</b>", heading_style))
|
| 385 |
+
elements.append(Spacer(1,0.2*inch))
|
| 386 |
+
|
| 387 |
+
lines = last_llm_report.split("\n")
|
| 388 |
+
|
| 389 |
+
for line in lines:
|
| 390 |
+
line = line.strip()
|
| 391 |
+
if not line:
|
| 392 |
+
continue
|
| 393 |
+
|
| 394 |
+
# Detect headings (e.g., lines ending with ":" )
|
| 395 |
+
if line.endswith(":"):
|
| 396 |
+
elements.append(Spacer(1,0.2*inch))
|
| 397 |
+
elements.append(Paragraph(f"<b>{line}</b>", heading_style))
|
| 398 |
+
else:
|
| 399 |
+
elements.append(Paragraph(f"• {line}", bullet_style))
|
| 400 |
+
|
| 401 |
+
elements.append(Spacer(1,0.5*inch))
|
| 402 |
+
|
| 403 |
+
# ---- DISCLAIMER ----
|
| 404 |
+
elements.append(Paragraph("Disclaimer", disclaimer_heading_style))
|
| 405 |
+
elements.append(Spacer(1,0.1*inch))
|
| 406 |
+
elements.append(Paragraph(
|
| 407 |
+
"Remember to consult a qualified doctor for personalized advice and treatment. "
|
| 408 |
+
"They will be able to provide guidance tailored to the patient's specific needs and circumstances.",
|
| 409 |
+
disclaimer_text_style
|
| 410 |
+
))
|
| 411 |
+
|
| 412 |
+
elements.append(Spacer(1,0.5*inch))
|
| 413 |
+
|
| 414 |
+
# ---- TEAM ----
|
| 415 |
+
elements.append(Paragraph("<b>Team Chinar AI (AJK)</b>", styles["Normal"]))
|
| 416 |
+
elements.append(Paragraph(
|
| 417 |
+
"Anees Qumar Abbasi | Sharafat Hussain | Salma Asghar | "
|
| 418 |
+
"Kaleem Hussain | Munazza Zahra | Kokub Khurishid",
|
| 419 |
+
styles["Normal"]
|
| 420 |
+
))
|
| 421 |
+
|
| 422 |
+
doc.build(elements)
|
| 423 |
+
return file_path
|
| 424 |
|
| 425 |
# ===============================
|
| 426 |
# INTERFACE
|