Yash goyal commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -154,20 +154,23 @@ class Scan(db.Model):
|
|
| 154 |
|
| 155 |
# Load Model
|
| 156 |
model = None
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
| 161 |
model = tf.keras.models.load_model(MODEL_PATH, compile=False)
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
|
|
|
| 171 |
|
| 172 |
# Plot training history
|
| 173 |
if os.path.exists(HISTORY_PATH):
|
|
@@ -351,6 +354,14 @@ def form():
|
|
| 351 |
# Check for required environment variables
|
| 352 |
if not app.config['MAIL_USERNAME'] or not app.config['MAIL_PASSWORD']:
|
| 353 |
logger.warning("Mail configuration missing, email functionality may fail")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
return render_template("form.html", history_plot="/training_plot.png")
|
| 355 |
except Exception as e:
|
| 356 |
logger.error("Error rendering form: %s", str(e))
|
|
@@ -442,8 +453,8 @@ def email_report(scan_id):
|
|
| 442 |
@app.route("/predict", methods=["POST"])
|
| 443 |
def predict():
|
| 444 |
try:
|
| 445 |
-
if not model:
|
| 446 |
-
raise ValueError("Model not loaded
|
| 447 |
if "image" not in request.files:
|
| 448 |
raise ValueError("No image uploaded.")
|
| 449 |
image = request.files["image"]
|
|
@@ -530,7 +541,7 @@ def predict():
|
|
| 530 |
def result():
|
| 531 |
try:
|
| 532 |
report = session.get("report", {})
|
| 533 |
-
return render_template("
|
| 534 |
except Exception as e:
|
| 535 |
logger.error("Error rendering result: %s", str(e))
|
| 536 |
return render_template("form.html", history_plot="/training_plot.png", result={
|
|
|
|
| 154 |
|
| 155 |
# Load Model
|
| 156 |
model = None
|
| 157 |
+
model_load_error = None
|
| 158 |
+
def load_model():
|
| 159 |
+
global model, model_load_error
|
| 160 |
+
try:
|
| 161 |
+
if os.path.exists(MODEL_PATH):
|
| 162 |
+
logger.info("Loading model from %s", MODEL_PATH)
|
| 163 |
model = tf.keras.models.load_model(MODEL_PATH, compile=False)
|
| 164 |
+
logger.info("Model loaded successfully")
|
| 165 |
+
else:
|
| 166 |
+
logger.error("Model file %s not found", MODEL_PATH)
|
| 167 |
+
model_load_error = f"Model file {MODEL_PATH} not found"
|
| 168 |
+
except Exception as e:
|
| 169 |
+
logger.error("Failed to load model: %s", str(e))
|
| 170 |
+
model_load_error = f"Model deserialization error: {str(e)}. Please ensure the model is compatible with TensorFlow 2.15.0 or re-save it."
|
| 171 |
+
|
| 172 |
+
# Attempt to load model at startup
|
| 173 |
+
load_model()
|
| 174 |
|
| 175 |
# Plot training history
|
| 176 |
if os.path.exists(HISTORY_PATH):
|
|
|
|
| 354 |
# Check for required environment variables
|
| 355 |
if not app.config['MAIL_USERNAME'] or not app.config['MAIL_PASSWORD']:
|
| 356 |
logger.warning("Mail configuration missing, email functionality may fail")
|
| 357 |
+
# Check if model loaded successfully
|
| 358 |
+
if model_load_error:
|
| 359 |
+
return render_template("form.html", history_plot="/training_plot.png", result={
|
| 360 |
+
"prediction": "Error",
|
| 361 |
+
"confidence": "N/A",
|
| 362 |
+
"message": f"Model loading failed: {model_load_error}",
|
| 363 |
+
"email_status": "N/A"
|
| 364 |
+
})
|
| 365 |
return render_template("form.html", history_plot="/training_plot.png")
|
| 366 |
except Exception as e:
|
| 367 |
logger.error("Error rendering form: %s", str(e))
|
|
|
|
| 453 |
@app.route("/predict", methods=["POST"])
|
| 454 |
def predict():
|
| 455 |
try:
|
| 456 |
+
if model_load_error or not model:
|
| 457 |
+
raise ValueError(f"Model not loaded: {model_load_error}")
|
| 458 |
if "image" not in request.files:
|
| 459 |
raise ValueError("No image uploaded.")
|
| 460 |
image = request.files["image"]
|
|
|
|
| 541 |
def result():
|
| 542 |
try:
|
| 543 |
report = session.get("report", {})
|
| 544 |
+
return render_template("form.html", **report)
|
| 545 |
except Exception as e:
|
| 546 |
logger.error("Error rendering result: %s", str(e))
|
| 547 |
return render_template("form.html", history_plot="/training_plot.png", result={
|