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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +133 -120
app.py CHANGED
@@ -152,176 +152,189 @@ def generate_pdf(report, filepath):
152
  width, height = A4
153
  y = height - 60
154
 
155
- # Modern gradient background header
156
- c.setFillColor(colors.HexColor("#f8fafc"))
157
- c.rect(0, height - 120, width, 120, fill=1, stroke=0)
158
 
159
- # Subtle gradient effect
160
- for i in range(40):
161
- alpha = 0.02 - (i * 0.0005)
162
- c.setFillColor(colors.Color(0.0, 0.37, 0.54, alpha))
163
- c.rect(0, height - 80 - i, width, 1, fill=1, stroke=0)
164
 
165
  # Logo with modern styling
166
  try:
167
  if os.path.exists(LOGO_PATH):
168
- # Add subtle shadow effect
169
- c.setFillColor(colors.Color(0, 0, 0, 0.1))
170
- c.circle(95, y + 35, 45, fill=1, stroke=0)
171
- c.drawImage(LOGO_PATH, 50, y, width=90, preserveAspectRatio=True, mask='auto')
 
 
 
172
  except Exception as e:
173
  logger.warning("Logo error: %s", str(e))
174
- # Fallback: Modern circular placeholder
175
- c.setFillColor(colors.HexColor("#e0f2fe"))
176
- c.setStrokeColor(colors.HexColor("#0ea5e9"))
177
- c.setLineWidth(3)
178
- c.circle(95, y + 35, 40, fill=1, stroke=1)
179
- c.setFont("Helvetica-Bold", 16)
180
- c.setFillColor(colors.HexColor("#0369a1"))
181
- c.drawCentredString(95, y + 30, "LOGO")
182
 
183
- # Modern title with enhanced typography
184
  c.setFont("Helvetica-Bold", 24)
185
- c.setFillColor(colors.HexColor("#1e293b"))
186
  c.drawCentredString(width / 2, y + 10, "Skin Lesion Diagnosis Report")
187
 
188
- # Elegant subtitle
189
  c.setFont("Helvetica", 12)
190
- c.setFillColor(colors.HexColor("#64748b"))
191
- c.drawCentredString(width / 2, y - 10, "AI-Powered Medical Analysis")
192
 
193
- # Modern accent line with gradient effect
194
- for i in range(3):
195
- c.setStrokeColor(colors.Color(0.0, 0.37, 0.54, 0.8 - i * 0.2))
196
- c.setLineWidth(3 - i)
197
- c.line(80, y - 25 - i, width - 80, y - 25 - i)
 
 
198
 
199
  y -= 80
200
 
201
- def modern_section_box(title, fields, extra_gap=25, accent_color="#0ea5e9"):
202
  nonlocal y
203
 
204
- # Calculate box height
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  box_height = len(fields) * 22 + 45
206
- box_top = y
207
- box_bottom = y - box_height
208
 
209
- # Modern card shadow
210
- for i in range(3):
211
- shadow_alpha = 0.05 - (i * 0.015)
212
- c.setFillColor(colors.Color(0, 0, 0, shadow_alpha))
213
- c.roundRect(52 + i, box_bottom - 2 - i, width - 104 - (i * 2), box_height, 12, fill=1, stroke=0)
214
 
215
- # Main card background with subtle gradient
216
- c.setFillColor(colors.white)
217
- c.roundRect(50, box_bottom, width - 100, box_height, 12, fill=1, stroke=0)
 
218
 
219
- # Subtle border
220
- c.setStrokeColor(colors.HexColor("#e2e8f0"))
221
- c.setLineWidth(1)
222
- c.roundRect(50, box_bottom, width - 100, box_height, 12, fill=0, stroke=1)
223
 
224
- # Modern section header with accent
225
- c.setFillColor(colors.HexColor(accent_color))
226
- c.roundRect(50, box_top - 35, width - 100, 35, 12, fill=1, stroke=0)
227
 
228
- # Section title
229
  c.setFont("Helvetica-Bold", 14)
230
  c.setFillColor(colors.white)
231
- c.drawString(70, box_top - 25, title)
232
 
233
- # Content area
234
- y -= 55
 
 
 
 
 
 
 
 
 
 
235
  c.setFont("Helvetica", 11)
 
236
 
237
- for label, val in fields.items():
238
- if label.startswith("•"):
239
- # Modern bullet points
240
- c.setFillColor(colors.HexColor(accent_color))
241
- c.circle(80, y + 4, 2, fill=1, stroke=0)
242
- c.setFillColor(colors.HexColor("#374151"))
243
- c.drawString(90, y, label[2:]) # Remove bullet from text
244
- else:
245
- # Label-value pairs with modern styling
246
- c.setFont("Helvetica-Bold", 11)
247
- c.setFillColor(colors.HexColor("#6b7280"))
248
- c.drawString(70, y, f"{label}:")
249
-
250
- c.setFont("Helvetica", 11)
251
- c.setFillColor(colors.HexColor("#111827"))
252
- c.drawString(70 + c.stringWidth(f"{label}: ", "Helvetica-Bold", 11), y, str(val))
253
  y -= 22
254
 
255
  y -= extra_gap
256
 
257
- # Enhanced sections with different accent colors
258
- modern_section_box("Patient Information", {
259
- "Name": report["name"],
260
- "Email": report["email"],
261
  "Gender": report["gender"],
262
- "Age": report["age"]
263
- }, accent_color="#3b82f6")
264
 
265
- modern_section_box("Clinical Diagnosis", {
266
- "Prediction": report["prediction"],
267
- "Confidence": report["confidence"],
268
- "Clinical Note": report["message"] if report["message"] else "No additional notes"
269
- }, accent_color="#10b981")
 
 
 
 
270
 
271
  disease = report["prediction"]
272
  treatment = recommendations.get(disease, recommendations["Unknown"])
273
 
274
- modern_section_box("Treatment Recommendations", {
275
- f" {line}": "" for line in treatment["solutions"]
276
- }, accent_color="#f59e0b")
277
 
278
- modern_section_box("Prescribed Medications", {
279
- f" {line}": "" for line in treatment["medications"]
280
- }, accent_color="#ef4444")
281
 
282
- # Modern disclaimer section
283
- y -= 20
284
-
285
- # Disclaimer background
286
- c.setFillColor(colors.HexColor("#fef3c7"))
287
- c.setStrokeColor(colors.HexColor("#f59e0b"))
288
- c.setLineWidth(1)
289
- c.roundRect(40, 35, width - 80, 55, 8, fill=1, stroke=1)
290
-
291
- # Warning icon (triangle)
292
- c.setFillColor(colors.HexColor("#f59e0b"))
293
- c.setStrokeColor(colors.HexColor("#f59e0b"))
294
  c.setLineWidth(2)
295
- triangle_points = [55, 75, 65, 75, 60, 85]
296
- c.polygon(triangle_points, fill=1, stroke=1)
297
- c.setFillColor(colors.white)
298
- c.setFont("Helvetica-Bold", 10)
299
- c.drawCentredString(60, 78, "!")
300
 
301
- # Disclaimer text with modern typography
302
- c.setFont("Helvetica-Bold", 10)
303
- c.setFillColor(colors.HexColor("#92400e"))
304
- c.drawString(75, 75, "IMPORTANT DISCLAIMER")
305
 
 
306
  c.setFont("Helvetica", 9)
307
- c.setFillColor(colors.HexColor("#78350f"))
308
  disclaimer_lines = [
309
- "This report is AI-generated using machine learning models and provides approximate insights only.",
310
- "This analysis is NOT a substitute for professional medical advice, diagnosis, or treatment.",
311
- "Always consult with qualified healthcare professionals for accurate medical assessment."
 
312
  ]
313
 
314
- line_y = 62
315
- for line in disclaimer_lines:
316
- c.drawString(75, line_y, line)
317
- line_y -= 10
318
-
319
- # Modern footer
320
- c.setFont("Helvetica", 8)
321
- c.setFillColor(colors.HexColor("#9ca3af"))
322
- c.drawCentredString(width / 2, 15, f"Generated on {datetime.now().strftime('%B %d, %Y at %I:%M %p')} | Confidential Medical Report")
323
 
324
  c.save()
 
325
  @app.route("/form")
326
  def form():
327
  return render_template("form.html", history_plot="/training_plot.png")
 
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
+
338
  @app.route("/form")
339
  def form():
340
  return render_template("form.html", history_plot="/training_plot.png")