Spaces:
Paused
Paused
Yash goyal commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,25 +6,24 @@ import pickle
|
|
| 6 |
import io
|
| 7 |
import os
|
| 8 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 9 |
from reportlab.pdfgen import canvas
|
|
|
|
| 10 |
from datetime import datetime
|
| 11 |
import logging
|
| 12 |
|
| 13 |
app = Flask(__name__)
|
| 14 |
-
app.secret_key = "
|
| 15 |
-
|
| 16 |
-
# Logging setup
|
| 17 |
-
logging.basicConfig(level=logging.INFO)
|
| 18 |
-
logger = logging.getLogger(__name__)
|
| 19 |
|
| 20 |
# Paths
|
| 21 |
MODEL_PATH = "skin_lesion_model.h5"
|
| 22 |
HISTORY_PATH = "training_history.pkl"
|
| 23 |
PLOT_PATH = "/tmp/static/training_plot.png"
|
|
|
|
| 24 |
IMG_SIZE = (224, 224)
|
| 25 |
CONFIDENCE_THRESHOLD = 0.30
|
| 26 |
|
| 27 |
-
# Label map
|
| 28 |
label_map = {
|
| 29 |
0: "Melanoma",
|
| 30 |
1: "Melanocytic nevus",
|
|
@@ -36,6 +35,10 @@ label_map = {
|
|
| 36 |
7: "Squamous cell carcinoma"
|
| 37 |
}
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# Load model
|
| 40 |
try:
|
| 41 |
logger.info("Loading model from %s", MODEL_PATH)
|
|
@@ -44,7 +47,7 @@ except Exception as e:
|
|
| 44 |
logger.error("Failed to load model: %s", str(e))
|
| 45 |
raise
|
| 46 |
|
| 47 |
-
# Load
|
| 48 |
history_dict = {}
|
| 49 |
if os.path.exists(HISTORY_PATH):
|
| 50 |
try:
|
|
@@ -63,9 +66,8 @@ if os.path.exists(HISTORY_PATH):
|
|
| 63 |
plt.close()
|
| 64 |
logger.info("Training plot saved at %s", PLOT_PATH)
|
| 65 |
except Exception as e:
|
| 66 |
-
logger.error("Failed to
|
| 67 |
|
| 68 |
-
# Preprocess uploaded image
|
| 69 |
def preprocess_image(image_bytes):
|
| 70 |
try:
|
| 71 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
|
@@ -77,17 +79,72 @@ def preprocess_image(image_bytes):
|
|
| 77 |
logger.error("Image preprocessing failed: %s", str(e))
|
| 78 |
raise
|
| 79 |
|
| 80 |
-
# Generate PDF report
|
| 81 |
def generate_pdf(report_data, filepath):
|
| 82 |
-
c = canvas.Canvas(filepath)
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
c.save()
|
| 92 |
|
| 93 |
@app.route("/form", methods=["GET"])
|
|
|
|
| 6 |
import io
|
| 7 |
import os
|
| 8 |
import matplotlib.pyplot as plt
|
| 9 |
+
from reportlab.lib.pagesizes import A4
|
| 10 |
+
from reportlab.lib import colors
|
| 11 |
from reportlab.pdfgen import canvas
|
| 12 |
+
from reportlab.lib.units import inch
|
| 13 |
from datetime import datetime
|
| 14 |
import logging
|
| 15 |
|
| 16 |
app = Flask(__name__)
|
| 17 |
+
app.secret_key = "e3f6f40bb8b2471b9f07c4025d845be9" # Replace with secure key if needed
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# Paths
|
| 20 |
MODEL_PATH = "skin_lesion_model.h5"
|
| 21 |
HISTORY_PATH = "training_history.pkl"
|
| 22 |
PLOT_PATH = "/tmp/static/training_plot.png"
|
| 23 |
+
LOGO_PATH = "static/logo.jpg" # Logo in static folder
|
| 24 |
IMG_SIZE = (224, 224)
|
| 25 |
CONFIDENCE_THRESHOLD = 0.30
|
| 26 |
|
|
|
|
| 27 |
label_map = {
|
| 28 |
0: "Melanoma",
|
| 29 |
1: "Melanocytic nevus",
|
|
|
|
| 35 |
7: "Squamous cell carcinoma"
|
| 36 |
}
|
| 37 |
|
| 38 |
+
# Logging setup
|
| 39 |
+
logging.basicConfig(level=logging.INFO)
|
| 40 |
+
logger = logging.getLogger(__name__)
|
| 41 |
+
|
| 42 |
# Load model
|
| 43 |
try:
|
| 44 |
logger.info("Loading model from %s", MODEL_PATH)
|
|
|
|
| 47 |
logger.error("Failed to load model: %s", str(e))
|
| 48 |
raise
|
| 49 |
|
| 50 |
+
# Load training history and generate plot
|
| 51 |
history_dict = {}
|
| 52 |
if os.path.exists(HISTORY_PATH):
|
| 53 |
try:
|
|
|
|
| 66 |
plt.close()
|
| 67 |
logger.info("Training plot saved at %s", PLOT_PATH)
|
| 68 |
except Exception as e:
|
| 69 |
+
logger.error("Failed to process training history: %s", str(e))
|
| 70 |
|
|
|
|
| 71 |
def preprocess_image(image_bytes):
|
| 72 |
try:
|
| 73 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
|
|
|
| 79 |
logger.error("Image preprocessing failed: %s", str(e))
|
| 80 |
raise
|
| 81 |
|
|
|
|
| 82 |
def generate_pdf(report_data, filepath):
|
| 83 |
+
c = canvas.Canvas(filepath, pagesize=A4)
|
| 84 |
+
width, height = A4
|
| 85 |
+
|
| 86 |
+
# Add logo if exists
|
| 87 |
+
try:
|
| 88 |
+
if os.path.exists(LOGO_PATH):
|
| 89 |
+
c.drawImage(LOGO_PATH, 50, height - 100, width=80, preserveAspectRatio=True, mask='auto')
|
| 90 |
+
except Exception as e:
|
| 91 |
+
logger.warning("Could not load logo: %s", str(e))
|
| 92 |
+
|
| 93 |
+
# Title
|
| 94 |
+
c.setFillColor(colors.HexColor("#007ACC"))
|
| 95 |
+
c.setFont("Helvetica-Bold", 20)
|
| 96 |
+
c.drawCentredString(width / 2, height - 80, "Skin Lesion Diagnosis Report")
|
| 97 |
+
c.setStrokeColor(colors.HexColor("#007ACC"))
|
| 98 |
+
c.setLineWidth(2)
|
| 99 |
+
c.line(60, height - 90, width - 60, height - 90)
|
| 100 |
+
|
| 101 |
+
# Info box background
|
| 102 |
+
c.setFillColor(colors.lightgrey)
|
| 103 |
+
c.rect(50, height - 250, width - 100, 140, fill=1, stroke=0)
|
| 104 |
+
|
| 105 |
+
# Patient Info
|
| 106 |
+
c.setFillColor(colors.black)
|
| 107 |
+
c.setFont("Helvetica-Bold", 12)
|
| 108 |
+
y = height - 120
|
| 109 |
+
spacing = 20
|
| 110 |
+
|
| 111 |
+
def draw_field(label, value):
|
| 112 |
+
nonlocal y
|
| 113 |
+
c.setFont("Helvetica-Bold", 12)
|
| 114 |
+
c.drawString(70, y, f"{label}:")
|
| 115 |
+
c.setFont("Helvetica", 12)
|
| 116 |
+
c.drawString(180, y, value)
|
| 117 |
+
y -= spacing
|
| 118 |
+
|
| 119 |
+
draw_field("Full Name", report_data.get("name", "N/A"))
|
| 120 |
+
draw_field("Email", report_data.get("email", "N/A"))
|
| 121 |
+
draw_field("Gender", report_data.get("gender", "N/A"))
|
| 122 |
+
draw_field("Age", str(report_data.get("age", "N/A")))
|
| 123 |
+
|
| 124 |
+
# Prediction
|
| 125 |
+
y -= 20
|
| 126 |
+
c.setFont("Helvetica-Bold", 14)
|
| 127 |
+
c.setFillColor(colors.HexColor("#007ACC"))
|
| 128 |
+
c.drawString(50, y, "AI Diagnosis Result")
|
| 129 |
+
c.setFillColor(colors.black)
|
| 130 |
+
y -= spacing
|
| 131 |
+
draw_field("Prediction", report_data.get("prediction", "N/A"))
|
| 132 |
+
draw_field("Confidence", report_data.get("confidence", "N/A"))
|
| 133 |
+
|
| 134 |
+
# Optional message
|
| 135 |
+
message = report_data.get("message", "")
|
| 136 |
+
if message:
|
| 137 |
+
y -= 10
|
| 138 |
+
c.setFont("Helvetica-Oblique", 11)
|
| 139 |
+
c.setFillColor(colors.red)
|
| 140 |
+
c.drawString(70, y, message)
|
| 141 |
+
|
| 142 |
+
# Timestamp
|
| 143 |
+
y -= 40
|
| 144 |
+
c.setFont("Helvetica", 10)
|
| 145 |
+
c.setFillColor(colors.grey)
|
| 146 |
+
c.drawString(50, y, f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
| 147 |
+
|
| 148 |
c.save()
|
| 149 |
|
| 150 |
@app.route("/form", methods=["GET"])
|