NishantFOT commited on
Commit
d5b4c96
·
verified ·
1 Parent(s): 4773742

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -31
app.py CHANGED
@@ -337,17 +337,20 @@ def analyze_image():
337
  if request.method == "OPTIONS":
338
  return "", 204
339
 
340
- if "user_id" not in session:
341
- return jsonify({"success": False, "message": "Not logged in"}), 401
342
-
343
- user_id = session["user_id"]
344
- user = db.session.get(User, user_id)
345
-
346
- if not user:
347
- session.clear()
348
- return jsonify({"success": False, "message": "User not found"}), 401
 
 
 
349
 
350
- print(f"[INFO] Analyze request from user: {user.username}")
351
 
352
  if "image" not in request.files:
353
  return jsonify({"success": False, "message": "Image file missing"}), 400
@@ -390,25 +393,34 @@ def analyze_image():
390
  gradcam_bytes = apply_gradcam_overlay(image, heatmap)
391
  inference_time = time.time() - start_time
392
 
393
- encrypted_img, salt, iv = ImageEncryption.encrypt_image(image_bytes, user_id)
394
- encrypted_gradcam, gradcam_salt, gradcam_iv = ImageEncryption.encrypt_image(gradcam_bytes, user_id)
395
-
396
- record = PatientRecord(
397
- user_code=str(user_id),
398
- diagnosis=top_predictions[0]["condition"],
399
- confidence=top_predictions[0]["confidence"],
400
- recommendation="AI-assisted result. Consult a dermatologist.",
401
- top_predictions=json.dumps(top_predictions),
402
- encrypted_image=encrypted_img,
403
- encryption_salt=salt,
404
- encryption_iv=iv,
405
- encrypted_gradcam=encrypted_gradcam,
406
- gradcam_iv=gradcam_iv,
407
- inference_time=inference_time
408
- )
409
-
410
- db.session.add(record)
411
- db.session.commit()
 
 
 
 
 
 
 
 
 
412
 
413
  gradcam_b64 = f"data:image/png;base64,{base64.b64encode(gradcam_bytes).decode()}"
414
 
@@ -420,8 +432,8 @@ def analyze_image():
420
  "gradcam": gradcam_b64,
421
  "explanation": f"The model identified {top_predictions[0]['condition']} with {top_predictions[0]['confidence']} confidence.",
422
  "recommendation": "AI-assisted result. Consult a dermatologist.",
423
- "record_id": record.id,
424
- "encryption_status": "✓ Encrypted with AES-256",
425
  "inference_time": f"{inference_time:.2f}s"
426
  })
427
 
@@ -431,6 +443,7 @@ def analyze_image():
431
  traceback.print_exc()
432
  return jsonify({"success": False, "message": f"Analysis failed: {str(e)}"}), 500
433
 
 
434
  @app.route("/api/history", methods=["GET"])
435
  def get_history():
436
  if "user_id" not in session:
 
337
  if request.method == "OPTIONS":
338
  return "", 204
339
 
340
+ # CHANGED: Make authentication optional - allow guest access
341
+ if "user_id" in session:
342
+ user_id = session["user_id"]
343
+ user = db.session.get(User, user_id)
344
+ if not user:
345
+ user_id = 0 # Guest mode
346
+ username = "Guest"
347
+ else:
348
+ username = user.username
349
+ else:
350
+ user_id = 0 # Guest mode - no login required
351
+ username = "Guest"
352
 
353
+ print(f"[INFO] Analyze request from user: {username} (ID: {user_id})")
354
 
355
  if "image" not in request.files:
356
  return jsonify({"success": False, "message": "Image file missing"}), 400
 
393
  gradcam_bytes = apply_gradcam_overlay(image, heatmap)
394
  inference_time = time.time() - start_time
395
 
396
+ # CHANGED: Only encrypt and save to database if user is logged in
397
+ record_id = None
398
+ if user_id > 0:
399
+ encrypted_img, salt, iv = ImageEncryption.encrypt_image(image_bytes, user_id)
400
+ encrypted_gradcam, gradcam_salt, gradcam_iv = ImageEncryption.encrypt_image(gradcam_bytes, user_id)
401
+
402
+ record = PatientRecord(
403
+ user_code=str(user_id),
404
+ diagnosis=top_predictions[0]["condition"],
405
+ confidence=top_predictions[0]["confidence"],
406
+ recommendation="AI-assisted result. Consult a dermatologist.",
407
+ top_predictions=json.dumps(top_predictions),
408
+ encrypted_image=encrypted_img,
409
+ encryption_salt=salt,
410
+ encryption_iv=iv,
411
+ encrypted_gradcam=encrypted_gradcam,
412
+ gradcam_iv=gradcam_iv,
413
+ inference_time=inference_time
414
+ )
415
+
416
+ db.session.add(record)
417
+ db.session.commit()
418
+ record_id = record.id
419
+ encryption_status = "✓ Encrypted with AES-256"
420
+ print(f"[INFO] Record saved to database (ID: {record_id})")
421
+ else:
422
+ encryption_status = "Guest mode - not saved"
423
+ print("[INFO] Guest analysis - not saved to database")
424
 
425
  gradcam_b64 = f"data:image/png;base64,{base64.b64encode(gradcam_bytes).decode()}"
426
 
 
432
  "gradcam": gradcam_b64,
433
  "explanation": f"The model identified {top_predictions[0]['condition']} with {top_predictions[0]['confidence']} confidence.",
434
  "recommendation": "AI-assisted result. Consult a dermatologist.",
435
+ "record_id": record_id,
436
+ "encryption_status": encryption_status,
437
  "inference_time": f"{inference_time:.2f}s"
438
  })
439
 
 
443
  traceback.print_exc()
444
  return jsonify({"success": False, "message": f"Analysis failed: {str(e)}"}), 500
445
 
446
+
447
  @app.route("/api/history", methods=["GET"])
448
  def get_history():
449
  if "user_id" not in session: