Spaces:
Paused
Paused
Yash goyal commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -152,186 +152,131 @@ def generate_pdf(report, filepath):
|
|
| 152 |
width, height = A4
|
| 153 |
y = height - 60
|
| 154 |
|
| 155 |
-
#
|
| 156 |
-
c.setFillColor(colors.
|
| 157 |
c.rect(0, 0, width, height, fill=1, stroke=0)
|
| 158 |
|
| 159 |
-
#
|
| 160 |
-
c.setFillColor(colors.Color(0.
|
| 161 |
-
c.rect(0, height-
|
| 162 |
|
| 163 |
-
# Logo with
|
| 164 |
try:
|
| 165 |
if os.path.exists(LOGO_PATH):
|
| 166 |
-
|
| 167 |
-
c.setFillColor(colors.white)
|
| 168 |
-
c.circle(90, y, 35, fill=1, stroke=1)
|
| 169 |
-
c.setStrokeColor(colors.Color(0.2, 0.4, 0.8, alpha=1))
|
| 170 |
-
c.setLineWidth(2)
|
| 171 |
-
c.circle(90, y, 35, fill=0, stroke=1)
|
| 172 |
-
c.drawImage(LOGO_PATH, 60, y-25, width=60, preserveAspectRatio=True, mask='auto')
|
| 173 |
except Exception as e:
|
| 174 |
logger.warning("Logo error: %s", str(e))
|
| 175 |
|
| 176 |
-
#
|
| 177 |
-
c.setFont("Helvetica-Bold",
|
| 178 |
-
c.setFillColor(colors.Color(0.
|
| 179 |
-
c.drawCentredString(width / 2, y +
|
| 180 |
|
| 181 |
# Subtitle
|
| 182 |
-
c.setFont("Helvetica",
|
| 183 |
-
c.setFillColor(colors.Color(0.
|
| 184 |
-
c.drawCentredString(width / 2, y -
|
| 185 |
|
| 186 |
-
#
|
| 187 |
-
c.setStrokeColor(colors.Color(0.
|
| 188 |
-
c.setLineWidth(3)
|
| 189 |
-
c.line(80, y - 30, width - 80, y - 30)
|
| 190 |
-
c.setStrokeColor(colors.Color(0.4, 0.8, 0.9, alpha=0.5))
|
| 191 |
c.setLineWidth(1)
|
| 192 |
-
c.line(
|
| 193 |
|
| 194 |
-
y -=
|
| 195 |
|
| 196 |
-
def
|
| 197 |
nonlocal y
|
| 198 |
|
| 199 |
-
|
| 200 |
-
color_schemes = {
|
| 201 |
-
"blue": {
|
| 202 |
-
"bg": colors.Color(0.94, 0.97, 1, alpha=1),
|
| 203 |
-
"border": colors.Color(0.3, 0.6, 0.9, alpha=1),
|
| 204 |
-
"title": colors.Color(0.1, 0.3, 0.7, alpha=1),
|
| 205 |
-
"accent": colors.Color(0.2, 0.7, 0.8, alpha=1)
|
| 206 |
-
},
|
| 207 |
-
"green": {
|
| 208 |
-
"bg": colors.Color(0.94, 0.98, 0.94, alpha=1),
|
| 209 |
-
"border": colors.Color(0.3, 0.7, 0.4, alpha=1),
|
| 210 |
-
"title": colors.Color(0.1, 0.5, 0.2, alpha=1),
|
| 211 |
-
"accent": colors.Color(0.4, 0.8, 0.5, alpha=1)
|
| 212 |
-
},
|
| 213 |
-
"purple": {
|
| 214 |
-
"bg": colors.Color(0.97, 0.94, 0.98, alpha=1),
|
| 215 |
-
"border": colors.Color(0.6, 0.3, 0.8, alpha=1),
|
| 216 |
-
"title": colors.Color(0.4, 0.1, 0.6, alpha=1),
|
| 217 |
-
"accent": colors.Color(0.7, 0.4, 0.8, alpha=1)
|
| 218 |
-
},
|
| 219 |
-
"orange": {
|
| 220 |
-
"bg": colors.Color(0.99, 0.96, 0.92, alpha=1),
|
| 221 |
-
"border": colors.Color(0.9, 0.5, 0.2, alpha=1),
|
| 222 |
-
"title": colors.Color(0.7, 0.3, 0.1, alpha=1),
|
| 223 |
-
"accent": colors.Color(0.9, 0.6, 0.3, alpha=1)
|
| 224 |
-
}
|
| 225 |
-
}
|
| 226 |
-
|
| 227 |
-
scheme = color_schemes.get(color_scheme, color_schemes["blue"])
|
| 228 |
-
box_height = len(fields) * 22 + 45
|
| 229 |
-
|
| 230 |
-
# Main background with rounded effect (simulated)
|
| 231 |
-
c.setFillColor(scheme["bg"])
|
| 232 |
-
c.roundRect(40, y - box_height, width - 80, box_height, 8, fill=1, stroke=0)
|
| 233 |
-
|
| 234 |
-
# Border with shadow effect
|
| 235 |
-
c.setStrokeColor(scheme["border"])
|
| 236 |
-
c.setLineWidth(2)
|
| 237 |
-
c.roundRect(40, y - box_height, width - 80, box_height, 8, fill=0, stroke=1)
|
| 238 |
-
|
| 239 |
-
# Shadow effect
|
| 240 |
-
c.setFillColor(colors.Color(0.8, 0.8, 0.8, alpha=0.3))
|
| 241 |
-
c.roundRect(43, y - box_height - 3, width - 80, box_height, 8, fill=1, stroke=0)
|
| 242 |
-
|
| 243 |
-
# Title bar
|
| 244 |
-
c.setFillColor(scheme["accent"])
|
| 245 |
-
c.roundRect(40, y - 35, width - 80, 35, 8, fill=1, stroke=0)
|
| 246 |
|
| 247 |
-
#
|
| 248 |
-
c.setFont("Helvetica-Bold", 14)
|
| 249 |
c.setFillColor(colors.white)
|
|
|
|
| 250 |
|
| 251 |
-
#
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
"Recommended Treatment": "💊",
|
| 256 |
-
"Suggested Medications": "🏥"
|
| 257 |
-
}
|
| 258 |
-
icon = icon_map.get(title, "📋")
|
| 259 |
|
| 260 |
-
|
|
|
|
|
|
|
| 261 |
|
| 262 |
-
|
| 263 |
-
c.setFont("Helvetica",
|
| 264 |
-
c.setFillColor(colors.Color(0.
|
|
|
|
| 265 |
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
c.setFillColor(colors.Color(0.3, 0.3, 0.3, alpha=1))
|
| 273 |
c.setFont("Helvetica-Bold", 10)
|
| 274 |
-
c.drawString(
|
|
|
|
|
|
|
|
|
|
| 275 |
c.setFont("Helvetica", 10)
|
| 276 |
-
c.drawString(
|
| 277 |
-
y -=
|
| 278 |
|
| 279 |
y -= extra_gap
|
| 280 |
|
| 281 |
-
#
|
| 282 |
-
|
| 283 |
-
"
|
| 284 |
-
"Email
|
| 285 |
"Gender": report["gender"],
|
| 286 |
-
"Age":
|
| 287 |
-
}
|
| 288 |
|
| 289 |
-
#
|
| 290 |
confidence_val = float(report["confidence"].replace('%', ''))
|
| 291 |
-
|
| 292 |
|
| 293 |
-
|
| 294 |
-
"
|
| 295 |
-
"Confidence
|
| 296 |
-
"
|
| 297 |
-
}
|
| 298 |
|
| 299 |
disease = report["prediction"]
|
| 300 |
treatment = recommendations.get(disease, recommendations["Unknown"])
|
| 301 |
|
| 302 |
-
|
| 303 |
-
f"
|
| 304 |
-
}
|
| 305 |
|
| 306 |
-
|
| 307 |
-
f"
|
| 308 |
-
}
|
| 309 |
|
| 310 |
-
#
|
| 311 |
-
y =
|
| 312 |
-
c.setFillColor(colors.Color(
|
| 313 |
-
c.
|
| 314 |
-
c.setStrokeColor(colors.Color(0.
|
| 315 |
-
c.setLineWidth(
|
| 316 |
-
c.
|
| 317 |
|
| 318 |
-
#
|
| 319 |
-
c.setFont("Helvetica-Bold",
|
| 320 |
-
c.setFillColor(colors.Color(0.
|
| 321 |
-
c.drawString(
|
| 322 |
|
| 323 |
-
# Disclaimer text
|
| 324 |
-
c.setFont("Helvetica",
|
| 325 |
-
c.setFillColor(colors.Color(0.
|
| 326 |
disclaimer_lines = [
|
| 327 |
-
"This report is generated using
|
| 328 |
-
"
|
| 329 |
-
"
|
| 330 |
-
"Always consult with a qualified healthcare provider for proper medical evaluation."
|
| 331 |
]
|
| 332 |
|
| 333 |
for i, line in enumerate(disclaimer_lines):
|
| 334 |
-
c.drawString(
|
| 335 |
|
| 336 |
c.save()
|
| 337 |
|
|
|
|
| 152 |
width, height = A4
|
| 153 |
y = height - 60
|
| 154 |
|
| 155 |
+
# Clean white background
|
| 156 |
+
c.setFillColor(colors.white)
|
| 157 |
c.rect(0, 0, width, height, fill=1, stroke=0)
|
| 158 |
|
| 159 |
+
# Subtle header background
|
| 160 |
+
c.setFillColor(colors.Color(0.98, 0.98, 0.98, alpha=1)) # Very light gray
|
| 161 |
+
c.rect(0, height-100, width, 100, fill=1, stroke=0)
|
| 162 |
|
| 163 |
+
# Logo with clean styling
|
| 164 |
try:
|
| 165 |
if os.path.exists(LOGO_PATH):
|
| 166 |
+
c.drawImage(LOGO_PATH, 50, y, width=60, preserveAspectRatio=True, mask='auto')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
except Exception as e:
|
| 168 |
logger.warning("Logo error: %s", str(e))
|
| 169 |
|
| 170 |
+
# Professional title
|
| 171 |
+
c.setFont("Helvetica-Bold", 22)
|
| 172 |
+
c.setFillColor(colors.Color(0.2, 0.2, 0.2, alpha=1)) # Dark gray
|
| 173 |
+
c.drawCentredString(width / 2, y + 5, "Medical Diagnosis Report")
|
| 174 |
|
| 175 |
# Subtitle
|
| 176 |
+
c.setFont("Helvetica", 11)
|
| 177 |
+
c.setFillColor(colors.Color(0.5, 0.5, 0.5, alpha=1)) # Medium gray
|
| 178 |
+
c.drawCentredString(width / 2, y - 15, "Dermatological Analysis")
|
| 179 |
|
| 180 |
+
# Clean separator line
|
| 181 |
+
c.setStrokeColor(colors.Color(0.7, 0.7, 0.7, alpha=1))
|
|
|
|
|
|
|
|
|
|
| 182 |
c.setLineWidth(1)
|
| 183 |
+
c.line(60, y - 35, width - 60, y - 35)
|
| 184 |
|
| 185 |
+
y -= 70
|
| 186 |
|
| 187 |
+
def professional_section_box(title, fields, extra_gap=20):
|
| 188 |
nonlocal y
|
| 189 |
|
| 190 |
+
box_height = len(fields) * 20 + 40
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
+
# Clean white background with subtle border
|
|
|
|
| 193 |
c.setFillColor(colors.white)
|
| 194 |
+
c.rect(50, y - box_height, width - 100, box_height, fill=1, stroke=0)
|
| 195 |
|
| 196 |
+
# Professional border
|
| 197 |
+
c.setStrokeColor(colors.Color(0.8, 0.8, 0.8, alpha=1))
|
| 198 |
+
c.setLineWidth(1)
|
| 199 |
+
c.rect(50, y - box_height, width - 100, box_height, fill=0, stroke=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
+
# Title bar with minimal styling
|
| 202 |
+
c.setFillColor(colors.Color(0.95, 0.95, 0.95, alpha=1)) # Very light gray
|
| 203 |
+
c.rect(50, y - 30, width - 100, 30, fill=1, stroke=0)
|
| 204 |
|
| 205 |
+
# Section title
|
| 206 |
+
c.setFont("Helvetica-Bold", 12)
|
| 207 |
+
c.setFillColor(colors.Color(0.3, 0.3, 0.3, alpha=1))
|
| 208 |
+
c.drawString(60, y - 20, title)
|
| 209 |
|
| 210 |
+
y -= 45
|
| 211 |
+
c.setFont("Helvetica", 10)
|
| 212 |
+
|
| 213 |
+
for label, val in fields.items():
|
| 214 |
+
# Label
|
| 215 |
+
c.setFillColor(colors.Color(0.4, 0.4, 0.4, alpha=1))
|
|
|
|
| 216 |
c.setFont("Helvetica-Bold", 10)
|
| 217 |
+
c.drawString(65, y, f"{label}:")
|
| 218 |
+
|
| 219 |
+
# Value
|
| 220 |
+
c.setFillColor(colors.Color(0.2, 0.2, 0.2, alpha=1))
|
| 221 |
c.setFont("Helvetica", 10)
|
| 222 |
+
c.drawString(170, y, str(val))
|
| 223 |
+
y -= 20
|
| 224 |
|
| 225 |
y -= extra_gap
|
| 226 |
|
| 227 |
+
# Professional sections
|
| 228 |
+
professional_section_box("Patient Information", {
|
| 229 |
+
"Name": report["name"],
|
| 230 |
+
"Email": report["email"],
|
| 231 |
"Gender": report["gender"],
|
| 232 |
+
"Age": report["age"]
|
| 233 |
+
})
|
| 234 |
|
| 235 |
+
# Confidence indicator with professional styling
|
| 236 |
confidence_val = float(report["confidence"].replace('%', ''))
|
| 237 |
+
confidence_status = "High" if confidence_val > 85 else "Moderate" if confidence_val > 70 else "Low"
|
| 238 |
|
| 239 |
+
professional_section_box("Diagnostic Results", {
|
| 240 |
+
"Condition": report["prediction"],
|
| 241 |
+
"Confidence": f"{report['confidence']} ({confidence_status})",
|
| 242 |
+
"Notes": report["message"] if report["message"] else "None"
|
| 243 |
+
})
|
| 244 |
|
| 245 |
disease = report["prediction"]
|
| 246 |
treatment = recommendations.get(disease, recommendations["Unknown"])
|
| 247 |
|
| 248 |
+
professional_section_box("Treatment Recommendations", {
|
| 249 |
+
f"{i+1}. {line}": "" for i, line in enumerate(treatment["solutions"])
|
| 250 |
+
})
|
| 251 |
|
| 252 |
+
professional_section_box("Medication Guidelines", {
|
| 253 |
+
f"{i+1}. {line}": "" for i, line in enumerate(treatment["medications"])
|
| 254 |
+
})
|
| 255 |
|
| 256 |
+
# Professional disclaimer
|
| 257 |
+
y = 110
|
| 258 |
+
c.setFillColor(colors.Color(0.97, 0.97, 0.97, alpha=1)) # Very light gray
|
| 259 |
+
c.rect(50, 40, width - 100, 60, fill=1, stroke=0)
|
| 260 |
+
c.setStrokeColor(colors.Color(0.8, 0.8, 0.8, alpha=1))
|
| 261 |
+
c.setLineWidth(1)
|
| 262 |
+
c.rect(50, 40, width - 100, 60, fill=0, stroke=1)
|
| 263 |
|
| 264 |
+
# Disclaimer title
|
| 265 |
+
c.setFont("Helvetica-Bold", 10)
|
| 266 |
+
c.setFillColor(colors.Color(0.3, 0.3, 0.3, alpha=1))
|
| 267 |
+
c.drawString(60, 85, "MEDICAL DISCLAIMER")
|
| 268 |
|
| 269 |
+
# Disclaimer text
|
| 270 |
+
c.setFont("Helvetica", 8)
|
| 271 |
+
c.setFillColor(colors.Color(0.4, 0.4, 0.4, alpha=1))
|
| 272 |
disclaimer_lines = [
|
| 273 |
+
"This report is generated using AI technology for preliminary assessment purposes only.",
|
| 274 |
+
"Results should not replace professional medical consultation and diagnosis.",
|
| 275 |
+
"Please consult a qualified healthcare provider for comprehensive medical evaluation."
|
|
|
|
| 276 |
]
|
| 277 |
|
| 278 |
for i, line in enumerate(disclaimer_lines):
|
| 279 |
+
c.drawString(60, 70 - (i * 10), line)
|
| 280 |
|
| 281 |
c.save()
|
| 282 |
|