Yash goyal commited on
Commit
55426c6
·
verified ·
1 Parent(s): 00c0bbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -42
app.py CHANGED
@@ -80,85 +80,94 @@ def preprocess_image(image_bytes):
80
  raise
81
 
82
  def generate_pdf(report_data, filepath):
83
- from reportlab.lib.pagesizes import A4
84
- from reportlab.pdfgen import canvas
85
- from reportlab.lib.units import inch
86
- from reportlab.lib import colors
87
- from datetime import datetime
88
-
89
  c = canvas.Canvas(filepath, pagesize=A4)
90
  width, height = A4
 
 
91
  margin = 50
92
- y = height - margin
 
93
 
94
- # Logo
95
- if os.path.exists(LOGO_PATH):
96
- c.drawImage(LOGO_PATH, margin, y - 40, width=60, preserveAspectRatio=True, mask='auto')
 
 
 
97
 
98
  # Title
99
  c.setFillColor(colors.HexColor("#007ACC"))
100
  c.setFont("Helvetica-Bold", 20)
101
- c.drawCentredString(width / 2, y - 20, "Skin Lesion Diagnosis Report")
102
- c.setLineWidth(1.5)
103
- c.setStrokeColor(colors.HexColor("#007ACC"))
104
- c.line(margin, y - 30, width - margin, y - 30)
105
 
106
- y -= 60
107
 
108
- def draw_box(title, lines, box_color=colors.lightgrey, text_color=colors.black):
109
- nonlocal y
110
- box_height = 20 * len(lines) + 30
111
- c.setFillColor(box_color)
112
- c.roundRect(margin, y - box_height, width - 2 * margin, box_height, 10, fill=1, stroke=0)
113
 
114
  c.setFillColor(colors.HexColor("#007ACC"))
115
  c.setFont("Helvetica-Bold", 14)
116
- c.drawString(margin + 15, y - 25, title)
117
-
118
- c.setFillColor(text_color)
119
- y -= 40
120
- for label, value in lines:
121
- c.setFont("Helvetica-Bold", 12)
122
- c.drawString(margin + 20, y, f"{label}:")
123
- c.setFont("Helvetica", 12)
124
- c.drawString(margin + 120, y, value)
125
- y -= 20
126
- y -= 10
127
-
128
- # Patient Info
 
129
  draw_box("Patient Details", [
130
  ("Full Name", report_data.get("name", "N/A")),
131
  ("Email", report_data.get("email", "N/A")),
132
  ("Gender", report_data.get("gender", "N/A")),
133
- ("Age", str(report_data.get("age", "N/A")))
134
  ])
135
 
136
- # Diagnosis Info
137
  draw_box("AI Diagnosis Result", [
138
  ("Prediction", report_data.get("prediction", "N/A")),
139
  ("Confidence", report_data.get("confidence", "N/A"))
140
  ])
141
 
142
- # Treatment Info
143
  draw_box("Treatment Recommendations", [
144
  ("Suggested Solutions", report_data.get("solution", "N/A")),
145
- ("Recommended Medicines", report_data.get("medicines", "N/A"))
146
  ])
147
 
148
- # Footer Disclaimer
149
  disclaimer_text = (
150
- "⚠ Disclaimer: This report is AI-generated using a machine learning model and is intended only "
151
- "to provide an approximate overview. It is not a substitute for professional medical advice. "
152
  "Please consult a certified doctor for accurate diagnosis and treatment."
153
  )
 
154
  c.setFont("Helvetica", 9)
155
  c.setFillColor(colors.red)
156
- c.drawCentredString(width / 2, 50, disclaimer_text)
 
 
 
 
 
 
 
157
 
158
  # Timestamp
159
  c.setFont("Helvetica", 8)
160
  c.setFillColor(colors.grey)
161
- c.drawRightString(width - margin, 35, f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
162
 
163
  c.save()
164
 
 
80
  raise
81
 
82
  def generate_pdf(report_data, filepath):
 
 
 
 
 
 
83
  c = canvas.Canvas(filepath, pagesize=A4)
84
  width, height = A4
85
+
86
+ # Constants
87
  margin = 50
88
+ box_padding = 10
89
+ current_y = height - margin
90
 
91
+ # Draw logo (top-right)
92
+ try:
93
+ if os.path.exists(LOGO_PATH):
94
+ c.drawImage(LOGO_PATH, width - 100, current_y - 50, width=50, height=50, preserveAspectRatio=True, mask='auto')
95
+ except Exception as e:
96
+ logger.warning("Could not load logo: %s", str(e))
97
 
98
  # Title
99
  c.setFillColor(colors.HexColor("#007ACC"))
100
  c.setFont("Helvetica-Bold", 20)
101
+ c.drawCentredString(width / 2, current_y, "Skin Lesion Diagnosis Report")
102
+ c.setStrokeColor(colors.HexColor("#000000"))
103
+ c.setLineWidth(1)
104
+ c.line(margin, current_y - 10, width - margin, current_y - 10)
105
 
106
+ current_y -= 40
107
 
108
+ def draw_box(title, fields, bg_color=colors.lightgrey):
109
+ nonlocal current_y
110
+ box_height = 20 * len(fields) + 30
111
+ c.setFillColor(bg_color)
112
+ c.roundRect(margin, current_y - box_height, width - 2 * margin, box_height, radius=8, fill=1)
113
 
114
  c.setFillColor(colors.HexColor("#007ACC"))
115
  c.setFont("Helvetica-Bold", 14)
116
+ c.drawString(margin + box_padding, current_y - 20, title)
117
+
118
+ y_pos = current_y - 40
119
+ for label, value in fields:
120
+ c.setFont("Helvetica-Bold", 11)
121
+ c.setFillColor(colors.black)
122
+ c.drawString(margin + box_padding, y_pos, f"{label}:")
123
+ c.setFont("Helvetica", 11)
124
+ c.drawString(margin + box_padding + 100, y_pos, str(value))
125
+ y_pos -= 20
126
+
127
+ current_y -= box_height + 15
128
+
129
+ # Patient details
130
  draw_box("Patient Details", [
131
  ("Full Name", report_data.get("name", "N/A")),
132
  ("Email", report_data.get("email", "N/A")),
133
  ("Gender", report_data.get("gender", "N/A")),
134
+ ("Age", report_data.get("age", "N/A")),
135
  ])
136
 
137
+ # Diagnosis
138
  draw_box("AI Diagnosis Result", [
139
  ("Prediction", report_data.get("prediction", "N/A")),
140
  ("Confidence", report_data.get("confidence", "N/A"))
141
  ])
142
 
143
+ # Treatment
144
  draw_box("Treatment Recommendations", [
145
  ("Suggested Solutions", report_data.get("solution", "N/A")),
146
+ ("Recommended Medicines", report_data.get("medication", "N/A")),
147
  ])
148
 
149
+ # Disclaimer (Footer)
150
  disclaimer_text = (
151
+ "⚠ Disclaimer: This report is AI-generated using a machine learning model and is intended "
152
+ "only to provide an approximate overview. It is not a substitute for professional medical advice. "
153
  "Please consult a certified doctor for accurate diagnosis and treatment."
154
  )
155
+
156
  c.setFont("Helvetica", 9)
157
  c.setFillColor(colors.red)
158
+ c.setStrokeColor(colors.red)
159
+ c.roundRect(margin, margin + 10, width - 2 * margin, 45, radius=6, fill=0)
160
+ text = c.beginText(margin + 10, margin + 45)
161
+ text.setFont("Helvetica", 9)
162
+ text.setFillColor(colors.red)
163
+ for line in disclaimer_text.split('. '):
164
+ text.textLine("■ " + line.strip() if line.startswith("⚠") else line.strip())
165
+ c.drawText(text)
166
 
167
  # Timestamp
168
  c.setFont("Helvetica", 8)
169
  c.setFillColor(colors.grey)
170
+ c.drawString(margin, margin, f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
171
 
172
  c.save()
173