Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,45 +3,57 @@ import tensorflow as tf
|
|
3 |
import numpy as np
|
4 |
from tensorflow.keras.preprocessing import image
|
5 |
import os
|
|
|
6 |
from werkzeug.utils import secure_filename
|
7 |
|
|
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
-
#
|
|
|
|
|
|
|
11 |
UPLOAD_FOLDER = "uploads"
|
12 |
-
os.
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
|
14 |
|
15 |
# Allowed file types
|
16 |
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg"}
|
17 |
|
18 |
def allowed_file(filename):
|
|
|
19 |
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
|
20 |
|
21 |
# Load trained model
|
22 |
try:
|
23 |
model = tf.keras.models.load_model("trained.h5")
|
24 |
-
|
25 |
except Exception as e:
|
26 |
-
|
27 |
model = None
|
28 |
|
29 |
def preprocess_image(img_path):
|
30 |
-
"""
|
31 |
try:
|
32 |
-
img = image.load_img(img_path, target_size=(150, 150)) # Resize
|
33 |
img_array = image.img_to_array(img) / 255.0 # Normalize
|
34 |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
35 |
return img_array
|
36 |
except Exception as e:
|
37 |
-
|
38 |
return None
|
39 |
|
40 |
@app.route("/predict", methods=["POST"])
|
41 |
def predict():
|
42 |
-
"""
|
43 |
if model is None:
|
44 |
-
return jsonify({"error": "Model not loaded. Check logs for details."}), 500
|
45 |
|
46 |
if "file" not in request.files:
|
47 |
return jsonify({"error": "No file uploaded"}), 400
|
@@ -65,7 +77,7 @@ def predict():
|
|
65 |
prediction = model.predict(img_array)[0][0]
|
66 |
except Exception as e:
|
67 |
os.remove(file_path)
|
68 |
-
|
69 |
return jsonify({"error": "Error making prediction"}), 500
|
70 |
|
71 |
os.remove(file_path) # Cleanup
|
|
|
3 |
import numpy as np
|
4 |
from tensorflow.keras.preprocessing import image
|
5 |
import os
|
6 |
+
import logging
|
7 |
from werkzeug.utils import secure_filename
|
8 |
|
9 |
+
# Initialize Flask app
|
10 |
app = Flask(__name__)
|
11 |
|
12 |
+
# Configure logging
|
13 |
+
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
14 |
+
|
15 |
+
# Set up uploads folder
|
16 |
UPLOAD_FOLDER = "uploads"
|
17 |
+
if not os.path.exists(UPLOAD_FOLDER):
|
18 |
+
try:
|
19 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
20 |
+
os.chmod(UPLOAD_FOLDER, 0o755) # Set proper permissions
|
21 |
+
except Exception as e:
|
22 |
+
logging.error(f"β Error creating uploads directory: {e}")
|
23 |
+
|
24 |
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
|
25 |
|
26 |
# Allowed file types
|
27 |
ALLOWED_EXTENSIONS = {"png", "jpg", "jpeg"}
|
28 |
|
29 |
def allowed_file(filename):
|
30 |
+
"""Check if file has an allowed extension."""
|
31 |
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
|
32 |
|
33 |
# Load trained model
|
34 |
try:
|
35 |
model = tf.keras.models.load_model("trained.h5")
|
36 |
+
logging.info("β
Model loaded successfully.")
|
37 |
except Exception as e:
|
38 |
+
logging.error(f"β Error loading model: {e}")
|
39 |
model = None
|
40 |
|
41 |
def preprocess_image(img_path):
|
42 |
+
"""Preprocesses the image for model prediction."""
|
43 |
try:
|
44 |
+
img = image.load_img(img_path, target_size=(150, 150)) # Resize to model input size
|
45 |
img_array = image.img_to_array(img) / 255.0 # Normalize
|
46 |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
47 |
return img_array
|
48 |
except Exception as e:
|
49 |
+
logging.error(f"β Error in image preprocessing: {e}")
|
50 |
return None
|
51 |
|
52 |
@app.route("/predict", methods=["POST"])
|
53 |
def predict():
|
54 |
+
"""Handles image upload and model prediction."""
|
55 |
if model is None:
|
56 |
+
return jsonify({"error": "Model not loaded. Check server logs for details."}), 500
|
57 |
|
58 |
if "file" not in request.files:
|
59 |
return jsonify({"error": "No file uploaded"}), 400
|
|
|
77 |
prediction = model.predict(img_array)[0][0]
|
78 |
except Exception as e:
|
79 |
os.remove(file_path)
|
80 |
+
logging.error(f"β Prediction Error: {e}")
|
81 |
return jsonify({"error": "Error making prediction"}), 500
|
82 |
|
83 |
os.remove(file_path) # Cleanup
|