Spaces:
Paused
Paused
Yash goyal commited on
Update app.py
Browse files
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 |
-
|
|
|
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
# Title
|
| 99 |
c.setFillColor(colors.HexColor("#007ACC"))
|
| 100 |
c.setFont("Helvetica-Bold", 20)
|
| 101 |
-
c.drawCentredString(width / 2,
|
| 102 |
-
c.
|
| 103 |
-
c.
|
| 104 |
-
c.line(margin,
|
| 105 |
|
| 106 |
-
|
| 107 |
|
| 108 |
-
def draw_box(title,
|
| 109 |
-
nonlocal
|
| 110 |
-
box_height = 20 * len(
|
| 111 |
-
c.setFillColor(
|
| 112 |
-
c.roundRect(margin,
|
| 113 |
|
| 114 |
c.setFillColor(colors.HexColor("#007ACC"))
|
| 115 |
c.setFont("Helvetica-Bold", 14)
|
| 116 |
-
c.drawString(margin +
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
c.
|
| 122 |
-
c.drawString(margin +
|
| 123 |
-
c.setFont("Helvetica",
|
| 124 |
-
c.drawString(margin +
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
| 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",
|
| 134 |
])
|
| 135 |
|
| 136 |
-
# Diagnosis
|
| 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
|
| 143 |
draw_box("Treatment Recommendations", [
|
| 144 |
("Suggested Solutions", report_data.get("solution", "N/A")),
|
| 145 |
-
("Recommended Medicines", report_data.get("
|
| 146 |
])
|
| 147 |
|
| 148 |
-
#
|
| 149 |
disclaimer_text = (
|
| 150 |
-
"⚠ Disclaimer: This report is AI-generated using a machine learning model and is intended
|
| 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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
# Timestamp
|
| 159 |
c.setFont("Helvetica", 8)
|
| 160 |
c.setFillColor(colors.grey)
|
| 161 |
-
c.
|
| 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 |
|