Spaces:
Paused
Paused
Yash goyal commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -150,75 +150,165 @@ def preprocess_image(image_bytes):
|
|
| 150 |
def generate_pdf(report, filepath):
|
| 151 |
c = canvas.Canvas(filepath, pagesize=A4)
|
| 152 |
width, height = A4
|
| 153 |
-
y = height -
|
| 154 |
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
try:
|
| 157 |
if os.path.exists(LOGO_PATH):
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
except Exception as e:
|
| 160 |
logger.warning("Logo error: %s", str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
|
| 163 |
-
c.setFont("Helvetica-Bold", 20)
|
| 164 |
-
c.setFillColor(colors.HexColor("#005e8a"))
|
| 165 |
-
c.drawCentredString(width / 2, y, "Skin Lesion Diagnosis Report")
|
| 166 |
-
c.setStrokeColor(colors.HexColor("#005e8a"))
|
| 167 |
-
c.setLineWidth(1.5)
|
| 168 |
-
c.line(60, y - 10, width - 60, y - 10)
|
| 169 |
-
y -= 40
|
| 170 |
-
|
| 171 |
-
def section_box(title, fields, extra_gap=20):
|
| 172 |
nonlocal y
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
c.setFont("Helvetica", 11)
|
| 181 |
-
c.setFillColor(colors.
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
y -= extra_gap
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
"Name": report["name"],
|
| 190 |
-
"Email": report["email"],
|
| 191 |
"Gender": report["gender"],
|
| 192 |
-
"Age": report[
|
| 193 |
-
})
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
disease = report["prediction"]
|
| 202 |
treatment = recommendations.get(disease, recommendations["Unknown"])
|
| 203 |
|
| 204 |
-
|
| 205 |
-
f"
|
| 206 |
-
})
|
| 207 |
|
| 208 |
-
|
| 209 |
-
f"
|
| 210 |
-
})
|
| 211 |
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
c.
|
| 217 |
-
c.
|
| 218 |
-
|
| 219 |
-
c.setFont("Helvetica-
|
| 220 |
-
c.
|
| 221 |
-
c.drawString(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
c.save()
|
| 224 |
|
|
|
|
| 150 |
def generate_pdf(report, filepath):
|
| 151 |
c = canvas.Canvas(filepath, pagesize=A4)
|
| 152 |
width, height = A4
|
| 153 |
+
y = height - 60
|
| 154 |
|
| 155 |
+
|
| 156 |
+
c.setFillColor(colors.Color(0.97, 0.98, 1, alpha=1)) # Very light blue
|
| 157 |
+
c.rect(0, 0, width, height, fill=1, stroke=0)
|
| 158 |
+
c.setFillColor(colors.Color(0.85, 0.93, 0.98, alpha=1))
|
| 159 |
+
c.rect(0, height-120, width, 120, fill=1, stroke=0)
|
| 160 |
try:
|
| 161 |
if os.path.exists(LOGO_PATH):
|
| 162 |
+
# Add circular background for logo
|
| 163 |
+
c.setFillColor(colors.white)
|
| 164 |
+
c.circle(90, y, 35, fill=1, stroke=1)
|
| 165 |
+
c.setStrokeColor(colors.Color(0.2, 0.4, 0.8, alpha=1))
|
| 166 |
+
c.setLineWidth(2)
|
| 167 |
+
c.circle(90, y, 35, fill=0, stroke=1)
|
| 168 |
+
c.drawImage(LOGO_PATH, 60, y-25, width=60, preserveAspectRatio=True, mask='auto')
|
| 169 |
except Exception as e:
|
| 170 |
logger.warning("Logo error: %s", str(e))
|
| 171 |
+
c.setFont("Helvetica-Bold", 24)
|
| 172 |
+
c.setFillColor(colors.Color(0.1, 0.3, 0.6, alpha=1)) # Deep blue
|
| 173 |
+
c.drawCentredString(width / 2, y + 10, "Skin Lesion Diagnosis Report")
|
| 174 |
+
|
| 175 |
+
c.setFont("Helvetica", 12)
|
| 176 |
+
c.setFillColor(colors.Color(0.4, 0.5, 0.7, alpha=1))
|
| 177 |
+
c.drawCentredString(width / 2, y - 10, "AI-Powered Medical Analysis Report")
|
| 178 |
+
|
| 179 |
+
c.setStrokeColor(colors.Color(0.2, 0.7, 0.8, alpha=1))
|
| 180 |
+
c.setLineWidth(3)
|
| 181 |
+
c.line(80, y - 30, width - 80, y - 30)
|
| 182 |
+
c.setStrokeColor(colors.Color(0.4, 0.8, 0.9, alpha=0.5))
|
| 183 |
+
c.setLineWidth(1)
|
| 184 |
+
c.line(80, y - 32, width - 80, y - 32)
|
| 185 |
+
|
| 186 |
+
y -= 80
|
| 187 |
|
| 188 |
+
def modern_section_box(title, fields, color_scheme="blue", extra_gap=25):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
nonlocal y
|
| 190 |
+
|
| 191 |
+
color_schemes = {
|
| 192 |
+
"blue": {
|
| 193 |
+
"bg": colors.Color(0.94, 0.97, 1, alpha=1),
|
| 194 |
+
"border": colors.Color(0.3, 0.6, 0.9, alpha=1),
|
| 195 |
+
"title": colors.Color(0.1, 0.3, 0.7, alpha=1),
|
| 196 |
+
"accent": colors.Color(0.2, 0.7, 0.8, alpha=1)
|
| 197 |
+
},
|
| 198 |
+
"green": {
|
| 199 |
+
"bg": colors.Color(0.94, 0.98, 0.94, alpha=1),
|
| 200 |
+
"border": colors.Color(0.3, 0.7, 0.4, alpha=1),
|
| 201 |
+
"title": colors.Color(0.1, 0.5, 0.2, alpha=1),
|
| 202 |
+
"accent": colors.Color(0.4, 0.8, 0.5, alpha=1)
|
| 203 |
+
},
|
| 204 |
+
"purple": {
|
| 205 |
+
"bg": colors.Color(0.97, 0.94, 0.98, alpha=1),
|
| 206 |
+
"border": colors.Color(0.6, 0.3, 0.8, alpha=1),
|
| 207 |
+
"title": colors.Color(0.4, 0.1, 0.6, alpha=1),
|
| 208 |
+
"accent": colors.Color(0.7, 0.4, 0.8, alpha=1)
|
| 209 |
+
},
|
| 210 |
+
"orange": {
|
| 211 |
+
"bg": colors.Color(0.99, 0.96, 0.92, alpha=1),
|
| 212 |
+
"border": colors.Color(0.9, 0.5, 0.2, alpha=1),
|
| 213 |
+
"title": colors.Color(0.7, 0.3, 0.1, alpha=1),
|
| 214 |
+
"accent": colors.Color(0.9, 0.6, 0.3, alpha=1)
|
| 215 |
+
}
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
scheme = color_schemes.get(color_scheme, color_schemes["blue"])
|
| 219 |
+
box_height = len(fields) * 22 + 45
|
| 220 |
+
|
| 221 |
+
c.setFillColor(scheme["bg"])
|
| 222 |
+
c.roundRect(40, y - box_height, width - 80, box_height, 8, fill=1, stroke=0)
|
| 223 |
+
|
| 224 |
+
c.setStrokeColor(scheme["border"])
|
| 225 |
+
c.setLineWidth(2)
|
| 226 |
+
c.roundRect(40, y - box_height, width - 80, box_height, 8, fill=0, stroke=1)
|
| 227 |
+
|
| 228 |
+
c.setFillColor(colors.Color(0.8, 0.8, 0.8, alpha=0.3))
|
| 229 |
+
c.roundRect(43, y - box_height - 3, width - 80, box_height, 8, fill=1, stroke=0)
|
| 230 |
+
|
| 231 |
+
c.setFillColor(scheme["accent"])
|
| 232 |
+
c.roundRect(40, y - 35, width - 80, 35, 8, fill=1, stroke=0)
|
| 233 |
+
|
| 234 |
+
c.setFont("Helvetica-Bold", 14)
|
| 235 |
+
c.setFillColor(colors.white)
|
| 236 |
+
icon_map = {
|
| 237 |
+
"Patient Details": "👤",
|
| 238 |
+
"Diagnosis": "🔬",
|
| 239 |
+
"Recommended Treatment": "💊",
|
| 240 |
+
"Suggested Medications": "🏥"
|
| 241 |
+
}
|
| 242 |
+
icon = icon_map.get(title, "📋")
|
| 243 |
+
|
| 244 |
+
c.drawString(55, y - 25, f"{icon} {title}")
|
| 245 |
+
|
| 246 |
+
y -= 50
|
| 247 |
c.setFont("Helvetica", 11)
|
| 248 |
+
c.setFillColor(colors.Color(0.2, 0.2, 0.2, alpha=1))
|
| 249 |
+
|
| 250 |
+
for i, (label, val) in enumerate(fields.items()):
|
| 251 |
+
if i % 2 == 0:
|
| 252 |
+
c.setFillColor(colors.Color(1, 1, 1, alpha=0.5))
|
| 253 |
+
c.rect(50, y - 3, width - 100, 18, fill=1, stroke=0)
|
| 254 |
+
|
| 255 |
+
c.setFillColor(colors.Color(0.3, 0.3, 0.3, alpha=1))
|
| 256 |
+
c.setFont("Helvetica-Bold", 10)
|
| 257 |
+
c.drawString(60, y, f"{label}:")
|
| 258 |
+
c.setFont("Helvetica", 10)
|
| 259 |
+
c.drawString(180, y, str(val))
|
| 260 |
+
y -= 22
|
| 261 |
+
|
| 262 |
y -= extra_gap
|
| 263 |
+
modern_section_box("Patient Details", {
|
| 264 |
+
"Full Name": report["name"],
|
| 265 |
+
"Email Address": report["email"],
|
|
|
|
|
|
|
| 266 |
"Gender": report["gender"],
|
| 267 |
+
"Age": f"{report['age']} years old"
|
| 268 |
+
}, "blue")
|
| 269 |
|
| 270 |
+
confidence_val = float(report["confidence"].replace('%', ''))
|
| 271 |
+
confidence_color = "green" if confidence_val > 80 else "orange" if confidence_val > 60 else "red"
|
| 272 |
+
|
| 273 |
+
modern_section_box("Diagnosis Results", {
|
| 274 |
+
"Medical Prediction": report["prediction"],
|
| 275 |
+
"Confidence Level": f"{report['confidence']} {'🟢' if confidence_val > 80 else '🟡' if confidence_val > 60 else '🔴'}",
|
| 276 |
+
"Clinical Notes": report["message"] if report["message"] else "No additional notes"
|
| 277 |
+
}, "green")
|
| 278 |
|
| 279 |
disease = report["prediction"]
|
| 280 |
treatment = recommendations.get(disease, recommendations["Unknown"])
|
| 281 |
|
| 282 |
+
modern_section_box("Recommended Treatment Plan", {
|
| 283 |
+
f"Step {i+1}": line for i, line in enumerate(treatment["solutions"])
|
| 284 |
+
}, "purple")
|
| 285 |
|
| 286 |
+
modern_section_box("Suggested Medications", {
|
| 287 |
+
f"Medication {i+1}": line for i, line in enumerate(treatment["medications"])
|
| 288 |
+
}, "orange")
|
| 289 |
|
| 290 |
+
y = 120
|
| 291 |
+
c.setFillColor(colors.Color(1, 0.95, 0.9, alpha=1)) # Light orange background
|
| 292 |
+
c.roundRect(40, 30, width - 80, 80, 8, fill=1, stroke=0)
|
| 293 |
+
c.setStrokeColor(colors.Color(0.9, 0.4, 0.1, alpha=1))
|
| 294 |
+
c.setLineWidth(2)
|
| 295 |
+
c.roundRect(40, 30, width - 80, 80, 8, fill=0, stroke=1)
|
| 296 |
+
|
| 297 |
+
c.setFont("Helvetica-Bold", 12)
|
| 298 |
+
c.setFillColor(colors.Color(0.8, 0.3, 0.1, alpha=1))
|
| 299 |
+
c.drawString(55, 95, "⚠️ IMPORTANT MEDICAL DISCLAIMER")
|
| 300 |
+
|
| 301 |
+
c.setFont("Helvetica", 9)
|
| 302 |
+
c.setFillColor(colors.Color(0.5, 0.2, 0.1, alpha=1))
|
| 303 |
+
disclaimer_lines = [
|
| 304 |
+
"This report is generated using artificial intelligence and machine learning algorithms.",
|
| 305 |
+
"The results provided are for informational purposes only and should not be considered",
|
| 306 |
+
"as a substitute for professional medical advice, diagnosis, or treatment.",
|
| 307 |
+
"Always consult with a qualified healthcare provider for proper medical evaluation."
|
| 308 |
+
]
|
| 309 |
+
|
| 310 |
+
for i, line in enumerate(disclaimer_lines):
|
| 311 |
+
c.drawString(55, 80 - (i * 12), line)
|
| 312 |
|
| 313 |
c.save()
|
| 314 |
|