Yash goyal commited on
Commit
5d5321e
·
verified ·
1 Parent(s): c10a935

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -137
app.py CHANGED
@@ -152,186 +152,131 @@ def generate_pdf(report, filepath):
152
  width, height = A4
153
  y = height - 60
154
 
155
- # Background gradient effect (simulated with rectangles)
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
 
159
- # Header background with gradient effect
160
- c.setFillColor(colors.Color(0.85, 0.93, 0.98, alpha=1))
161
- c.rect(0, height-120, width, 120, fill=1, stroke=0)
162
 
163
- # Logo with modern styling
164
  try:
165
  if os.path.exists(LOGO_PATH):
166
- # Add circular background for logo
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
- # Modern title with gradient-like effect
177
- c.setFont("Helvetica-Bold", 24)
178
- c.setFillColor(colors.Color(0.1, 0.3, 0.6, alpha=1)) # Deep blue
179
- c.drawCentredString(width / 2, y + 10, "Skin Lesion Diagnosis Report")
180
 
181
  # Subtitle
182
- c.setFont("Helvetica", 12)
183
- c.setFillColor(colors.Color(0.4, 0.5, 0.7, alpha=1))
184
- c.drawCentredString(width / 2, y - 10, "AI-Powered Medical Analysis Report")
185
 
186
- # Decorative line with gradient effect
187
- c.setStrokeColor(colors.Color(0.2, 0.7, 0.8, alpha=1))
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(80, y - 32, width - 80, y - 32)
193
 
194
- y -= 80
195
 
196
- def modern_section_box(title, fields, color_scheme="blue", extra_gap=25):
197
  nonlocal y
198
 
199
- # Color schemes
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
- # Title with icon effect
248
- c.setFont("Helvetica-Bold", 14)
249
  c.setFillColor(colors.white)
 
250
 
251
- # Add simple icons based on section
252
- icon_map = {
253
- "Patient Details": "👤",
254
- "Diagnosis": "🔬",
255
- "Recommended Treatment": "💊",
256
- "Suggested Medications": "🏥"
257
- }
258
- icon = icon_map.get(title, "📋")
259
 
260
- c.drawString(55, y - 25, f"{icon} {title}")
 
 
261
 
262
- y -= 50
263
- c.setFont("Helvetica", 11)
264
- c.setFillColor(colors.Color(0.2, 0.2, 0.2, alpha=1))
 
265
 
266
- for i, (label, val) in enumerate(fields.items()):
267
- # Alternating row backgrounds
268
- if i % 2 == 0:
269
- c.setFillColor(colors.Color(1, 1, 1, alpha=0.5))
270
- c.rect(50, y - 3, width - 100, 18, fill=1, stroke=0)
271
-
272
- c.setFillColor(colors.Color(0.3, 0.3, 0.3, alpha=1))
273
  c.setFont("Helvetica-Bold", 10)
274
- c.drawString(60, y, f"{label}:")
 
 
 
275
  c.setFont("Helvetica", 10)
276
- c.drawString(180, y, str(val))
277
- y -= 22
278
 
279
  y -= extra_gap
280
 
281
- # Enhanced sections with different color schemes
282
- modern_section_box("Patient Details", {
283
- "Full Name": report["name"],
284
- "Email Address": report["email"],
285
  "Gender": report["gender"],
286
- "Age": f"{report['age']} years old"
287
- }, "blue")
288
 
289
- # Add confidence visualization
290
  confidence_val = float(report["confidence"].replace('%', ''))
291
- confidence_color = "green" if confidence_val > 80 else "orange" if confidence_val > 60 else "red"
292
 
293
- modern_section_box("Diagnosis Results", {
294
- "Medical Prediction": report["prediction"],
295
- "Confidence Level": f"{report['confidence']} {'🟢' if confidence_val > 80 else '🟡' if confidence_val > 60 else '🔴'}",
296
- "Clinical Notes": report["message"] if report["message"] else "No additional notes"
297
- }, "green")
298
 
299
  disease = report["prediction"]
300
  treatment = recommendations.get(disease, recommendations["Unknown"])
301
 
302
- modern_section_box("Recommended Treatment Plan", {
303
- f"Step {i+1}": line for i, line in enumerate(treatment["solutions"])
304
- }, "purple")
305
 
306
- modern_section_box("Suggested Medications", {
307
- f"Medication {i+1}": line for i, line in enumerate(treatment["medications"])
308
- }, "orange")
309
 
310
- # Modern disclaimer with warning styling
311
- y = 120
312
- c.setFillColor(colors.Color(1, 0.95, 0.9, alpha=1)) # Light orange background
313
- c.roundRect(40, 30, width - 80, 80, 8, fill=1, stroke=0)
314
- c.setStrokeColor(colors.Color(0.9, 0.4, 0.1, alpha=1))
315
- c.setLineWidth(2)
316
- c.roundRect(40, 30, width - 80, 80, 8, fill=0, stroke=1)
317
 
318
- # Warning icon and title
319
- c.setFont("Helvetica-Bold", 12)
320
- c.setFillColor(colors.Color(0.8, 0.3, 0.1, alpha=1))
321
- c.drawString(55, 95, "⚠️ IMPORTANT MEDICAL DISCLAIMER")
322
 
323
- # Disclaimer text with better formatting
324
- c.setFont("Helvetica", 9)
325
- c.setFillColor(colors.Color(0.5, 0.2, 0.1, alpha=1))
326
  disclaimer_lines = [
327
- "This report is generated using artificial intelligence and machine learning algorithms.",
328
- "The results provided are for informational purposes only and should not be considered",
329
- "as a substitute for professional medical advice, diagnosis, or treatment.",
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(55, 80 - (i * 12), line)
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