Yash goyal commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,8 @@ 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"
|
|
@@ -22,6 +24,13 @@ PLOT_PATH = "/tmp/static/training_plot.png"
|
|
| 22 |
LOGO_PATH = "static/logo.jpg"
|
| 23 |
IMG_SIZE = (224, 224)
|
| 24 |
CONFIDENCE_THRESHOLD = 0.30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
label_map = {
|
| 27 |
0: "Melanoma",
|
|
@@ -290,6 +299,75 @@ def form():
|
|
| 290 |
def training_plot():
|
| 291 |
return send_file(PLOT_PATH, mimetype="image/png")
|
| 292 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
@app.route("/predict", methods=["POST"])
|
| 294 |
def predict():
|
| 295 |
try:
|
|
|
|
| 12 |
from reportlab.lib.units import inch
|
| 13 |
from datetime import datetime
|
| 14 |
import logging
|
| 15 |
+
from flask_mail import Mail, Message
|
| 16 |
+
from flask import jsonify, url_for
|
| 17 |
|
| 18 |
app = Flask(__name__)
|
| 19 |
app.secret_key = "e3f6f40bb8b2471b9f07c4025d845be9"
|
|
|
|
| 24 |
LOGO_PATH = "static/logo.jpg"
|
| 25 |
IMG_SIZE = (224, 224)
|
| 26 |
CONFIDENCE_THRESHOLD = 0.30
|
| 27 |
+
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
|
| 28 |
+
app.config['MAIL_PORT'] = 465
|
| 29 |
+
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME') # Your Gmail address
|
| 30 |
+
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD') # Your Gmail App Password
|
| 31 |
+
app.config['MAIL_USE_TLS'] = False
|
| 32 |
+
app.config['MAIL_USE_SSL'] = True
|
| 33 |
+
mail = Mail(app)
|
| 34 |
|
| 35 |
label_map = {
|
| 36 |
0: "Melanoma",
|
|
|
|
| 299 |
def training_plot():
|
| 300 |
return send_file(PLOT_PATH, mimetype="image/png")
|
| 301 |
|
| 302 |
+
@app.route("/api/history")
|
| 303 |
+
def api_history():
|
| 304 |
+
user_email = request.args.get('email')
|
| 305 |
+
if not user_email:
|
| 306 |
+
return jsonify({"error": "Email parameter is required"}), 400
|
| 307 |
+
|
| 308 |
+
user = User.query.filter_by(email=user_email).first()
|
| 309 |
+
if not user:
|
| 310 |
+
return jsonify([]) # Return empty list if user not found
|
| 311 |
+
|
| 312 |
+
scans = Scan.query.filter_by(user_id=user.id).order_by(Scan.timestamp.desc()).all()
|
| 313 |
+
|
| 314 |
+
history_data = []
|
| 315 |
+
for scan in scans:
|
| 316 |
+
# We need to provide the full URL for the image
|
| 317 |
+
# In a real deployment, you might need to replace 'localhost' with your actual domain
|
| 318 |
+
image_url = url_for('uploaded_file', filename=scan.image_filename, _external=True)
|
| 319 |
+
|
| 320 |
+
history_data.append({
|
| 321 |
+
"id": scan.id,
|
| 322 |
+
"prediction": scan.prediction,
|
| 323 |
+
"confidence": scan.confidence,
|
| 324 |
+
"timestamp": scan.timestamp.strftime("%B %d, %Y at %I:%M %p"),
|
| 325 |
+
"patient_name": scan.patient_name,
|
| 326 |
+
"image_url": image_url
|
| 327 |
+
})
|
| 328 |
+
|
| 329 |
+
return jsonify(history_data)
|
| 330 |
+
|
| 331 |
+
# --- New API Endpoint to Email a Report ---
|
| 332 |
+
@app.route("/api/email-report/<int:scan_id>")
|
| 333 |
+
def email_report(scan_id):
|
| 334 |
+
scan = Scan.query.get(scan_id)
|
| 335 |
+
if not scan:
|
| 336 |
+
return jsonify({"error": "Report not found"}), 404
|
| 337 |
+
|
| 338 |
+
try:
|
| 339 |
+
# Generate the PDF report
|
| 340 |
+
report_data = {
|
| 341 |
+
"name": scan.user.name, "email": scan.user.email,
|
| 342 |
+
"gender": scan.patient_gender, "age": scan.patient_age,
|
| 343 |
+
"prediction": scan.prediction, "confidence": scan.confidence,
|
| 344 |
+
"message": ""
|
| 345 |
+
}
|
| 346 |
+
pdf_path = f"/tmp/report_{scan_id}.pdf"
|
| 347 |
+
generate_pdf(report_data, pdf_path)
|
| 348 |
+
|
| 349 |
+
# Create and send the email
|
| 350 |
+
msg = Message(
|
| 351 |
+
'Your SnapSkin Diagnostic Report',
|
| 352 |
+
sender=app.config['MAIL_USERNAME'],
|
| 353 |
+
recipients=[scan.user.email]
|
| 354 |
+
)
|
| 355 |
+
msg.body = f"Dear {scan.user.name},\n\nPlease find your requested diagnostic report attached.\n\nThank you for using SnapSkin."
|
| 356 |
+
with app.open_resource(pdf_path) as fp:
|
| 357 |
+
msg.attach(f"SnapSkin_Report_{scan_id}.pdf", "application/pdf", fp.read())
|
| 358 |
+
|
| 359 |
+
mail.send(msg)
|
| 360 |
+
|
| 361 |
+
# Clean up the temporary PDF file
|
| 362 |
+
os.remove(pdf_path)
|
| 363 |
+
|
| 364 |
+
return jsonify({"success": True, "message": f"Report sent to {scan.user.email}"})
|
| 365 |
+
|
| 366 |
+
except Exception as e:
|
| 367 |
+
logger.error(f"Failed to send email for scan {scan_id}: {e}")
|
| 368 |
+
return jsonify({"success": False, "message": "Failed to send email."}), 500
|
| 369 |
+
|
| 370 |
+
|
| 371 |
@app.route("/predict", methods=["POST"])
|
| 372 |
def predict():
|
| 373 |
try:
|