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
|
|
@@ -21,6 +21,8 @@ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
|
| 21 |
last_llm_report = ""
|
| 22 |
last_basic_data = {}
|
| 23 |
|
|
|
|
|
|
|
| 24 |
# ===============================
|
| 25 |
# LLM FUNCTION
|
| 26 |
# ===============================
|
|
@@ -124,7 +126,8 @@ 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"""
|
|
@@ -138,8 +141,13 @@ def predict_basic(name, age, gender, platelets, hb, duration,
|
|
| 138 |
return f"""
|
| 139 |
<div style="font-family:Arial;padding:15px;">
|
| 140 |
<h2 style="color:{color};">Patient: {name}</h2>
|
| 141 |
-
|
| 142 |
-
<h3>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
<h4>Clinical Risk Factors:</h4>
|
| 145 |
<ul>
|
|
@@ -161,7 +169,6 @@ 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"""
|
|
@@ -195,7 +202,7 @@ Medication: {d['medication']}
|
|
| 195 |
|
| 196 |
|
| 197 |
# ===============================
|
| 198 |
-
# PDF GENERATION
|
| 199 |
# ===============================
|
| 200 |
def generate_pdf(*inputs):
|
| 201 |
|
|
@@ -207,11 +214,33 @@ 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"]],
|
|
@@ -222,13 +251,10 @@ def generate_pdf(*inputs):
|
|
| 222 |
["Bacteria", d["bacteria"]],
|
| 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 |
('FONTNAME',(0,0),(-1,-1),'Helvetica')
|
| 234 |
]))
|
|
@@ -236,30 +262,18 @@ def generate_pdf(*inputs):
|
|
| 236 |
elements.append(table)
|
| 237 |
elements.append(Spacer(1,0.4*inch))
|
| 238 |
|
| 239 |
-
# ---- BASIC RECOMMENDATION ----
|
| 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 SECTION ----
|
| 246 |
if last_llm_report:
|
| 247 |
-
elements.append(Paragraph("<b>Detailed AI Report
|
| 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(
|
| 262 |
-
"Anees Qumar Abbasi
|
| 263 |
"Kaleem Hussain | Munazza Zahra | Kokub Khurishid",
|
| 264 |
styles["Normal"]
|
| 265 |
))
|
|
@@ -274,9 +288,14 @@ def generate_pdf(*inputs):
|
|
| 274 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 275 |
|
| 276 |
gr.Markdown("""
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
""")
|
| 281 |
|
| 282 |
with gr.Row():
|
|
@@ -303,10 +322,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 303 |
label="Current Medication"
|
| 304 |
)
|
| 305 |
|
| 306 |
-
# ---- BUTTONS ABOVE OUTPUT ----
|
| 307 |
with gr.Row():
|
| 308 |
-
predict_btn = gr.Button("Basic Risk Assessment"
|
| 309 |
-
detailed_btn = gr.Button("Detailed Report & Recommendations"
|
| 310 |
pdf_btn = gr.Button("Generate PDF Report")
|
| 311 |
|
| 312 |
output = gr.HTML()
|
|
|
|
| 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
|
|
|
|
| 21 |
last_llm_report = ""
|
| 22 |
last_basic_data = {}
|
| 23 |
|
| 24 |
+
LOGO_PATH = "logo.png" # 🔹 Place your logo file in HF repo
|
| 25 |
+
|
| 26 |
# ===============================
|
| 27 |
# LLM FUNCTION
|
| 28 |
# ===============================
|
|
|
|
| 126 |
"prob_pct": prob_pct,
|
| 127 |
"risk_label": risk_label,
|
| 128 |
"recommendation": recommendation,
|
| 129 |
+
"factors": factors,
|
| 130 |
+
"color": color
|
| 131 |
}
|
| 132 |
|
| 133 |
urdu = f"""
|
|
|
|
| 141 |
return f"""
|
| 142 |
<div style="font-family:Arial;padding:15px;">
|
| 143 |
<h2 style="color:{color};">Patient: {name}</h2>
|
| 144 |
+
|
| 145 |
+
<h3>
|
| 146 |
+
Risk of Treatment Failure:
|
| 147 |
+
<span style="background:{color};color:white;padding:6px 12px;border-radius:6px;">
|
| 148 |
+
{prob_pct}% - {risk_label}
|
| 149 |
+
</span>
|
| 150 |
+
</h3>
|
| 151 |
|
| 152 |
<h4>Clinical Risk Factors:</h4>
|
| 153 |
<ul>
|
|
|
|
| 169 |
global last_basic_data, last_llm_report
|
| 170 |
|
| 171 |
basic_html = predict_basic(*inputs)
|
|
|
|
| 172 |
d = last_basic_data
|
| 173 |
|
| 174 |
patient_info = f"""
|
|
|
|
| 202 |
|
| 203 |
|
| 204 |
# ===============================
|
| 205 |
+
# PDF GENERATION
|
| 206 |
# ===============================
|
| 207 |
def generate_pdf(*inputs):
|
| 208 |
|
|
|
|
| 214 |
elements = []
|
| 215 |
styles = getSampleStyleSheet()
|
| 216 |
|
| 217 |
+
# ---- LOGO ----
|
| 218 |
+
if os.path.exists(LOGO_PATH):
|
| 219 |
+
elements.append(Image(LOGO_PATH, width=1.5*inch, height=1.5*inch))
|
| 220 |
+
elements.append(Spacer(1,0.2*inch))
|
| 221 |
+
|
| 222 |
elements.append(Paragraph("<b>TyphoidGuard AI Clinical Report</b>", styles["Title"]))
|
| 223 |
elements.append(Spacer(1, 0.3 * inch))
|
| 224 |
|
| 225 |
+
# ---- RISK BADGE ----
|
| 226 |
+
risk_color = colors.HexColor(d["color"])
|
| 227 |
+
badge_style = ParagraphStyle(
|
| 228 |
+
name="Badge",
|
| 229 |
+
parent=styles["Normal"],
|
| 230 |
+
backColor=risk_color,
|
| 231 |
+
textColor=colors.white,
|
| 232 |
+
fontSize=12,
|
| 233 |
+
leftIndent=5,
|
| 234 |
+
rightIndent=5,
|
| 235 |
+
spaceAfter=10
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
elements.append(Paragraph(
|
| 239 |
+
f"Risk: {d['prob_pct']}% - {d['risk_label']}",
|
| 240 |
+
badge_style
|
| 241 |
+
))
|
| 242 |
+
elements.append(Spacer(1,0.3*inch))
|
| 243 |
+
|
| 244 |
table_data = [
|
| 245 |
["Patient Name", d["name"]],
|
| 246 |
["Age", d["age"]],
|
|
|
|
| 251 |
["Bacteria", d["bacteria"]],
|
| 252 |
["Severity", d["severity"]],
|
| 253 |
["Medication", d["medication"]],
|
|
|
|
|
|
|
| 254 |
]
|
| 255 |
|
| 256 |
table = Table(table_data, colWidths=[2.5*inch, 3*inch])
|
| 257 |
table.setStyle(TableStyle([
|
|
|
|
| 258 |
('GRID',(0,0),(-1,-1),1,colors.grey),
|
| 259 |
('FONTNAME',(0,0),(-1,-1),'Helvetica')
|
| 260 |
]))
|
|
|
|
| 262 |
elements.append(table)
|
| 263 |
elements.append(Spacer(1,0.4*inch))
|
| 264 |
|
|
|
|
| 265 |
elements.append(Paragraph("<b>Agent Recommendation</b>", styles["Heading2"]))
|
|
|
|
| 266 |
elements.append(Paragraph(d["recommendation"], styles["Normal"]))
|
| 267 |
elements.append(Spacer(1,0.4*inch))
|
| 268 |
|
|
|
|
| 269 |
if last_llm_report:
|
| 270 |
+
elements.append(Paragraph("<b>Detailed AI Report</b>", styles["Heading2"]))
|
|
|
|
| 271 |
elements.append(Paragraph(last_llm_report.replace("\n","<br/>"), styles["Normal"]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
|
|
|
| 273 |
elements.append(Spacer(1,0.5*inch))
|
| 274 |
elements.append(Paragraph("<b>Team Chinar AI (AJK)</b>", styles["Normal"]))
|
| 275 |
elements.append(Paragraph(
|
| 276 |
+
"Anees Qumar Abbasi | Sharafat Hussain | Salma Asghar | "
|
| 277 |
"Kaleem Hussain | Munazza Zahra | Kokub Khurishid",
|
| 278 |
styles["Normal"]
|
| 279 |
))
|
|
|
|
| 288 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 289 |
|
| 290 |
gr.Markdown("""
|
| 291 |
+
<div style="text-align:center;">
|
| 292 |
+
<img src="logo.png" width="120">
|
| 293 |
+
<h1>🏥 TyphoidGuard AI</h1>
|
| 294 |
+
<h4>HEC Generative AI Hackathon – Pakistan</h4>
|
| 295 |
+
<h4>Team Chinar AI (AJK)</h4>
|
| 296 |
+
<p><b>Anees Qumar Abbasi (Lead)</b> | Sharafat Hussain |
|
| 297 |
+
Salma Asghar | Kaleem Hussain | Munazza Zahra | Kokub Khurishid</p>
|
| 298 |
+
</div>
|
| 299 |
""")
|
| 300 |
|
| 301 |
with gr.Row():
|
|
|
|
| 322 |
label="Current Medication"
|
| 323 |
)
|
| 324 |
|
|
|
|
| 325 |
with gr.Row():
|
| 326 |
+
predict_btn = gr.Button("Basic Risk Assessment")
|
| 327 |
+
detailed_btn = gr.Button("Detailed Report & Recommendations")
|
| 328 |
pdf_btn = gr.Button("Generate PDF Report")
|
| 329 |
|
| 330 |
output = gr.HTML()
|