Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -14,12 +14,8 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(
|
|
14 |
|
15 |
# Set up uploads folder
|
16 |
UPLOAD_FOLDER = "uploads"
|
17 |
-
|
18 |
-
|
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 |
|
@@ -31,15 +27,20 @@ def allowed_file(filename):
|
|
31 |
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
|
32 |
|
33 |
# Load trained model
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
39 |
model = None
|
40 |
|
41 |
def preprocess_image(img_path):
|
42 |
-
"""
|
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
|
@@ -49,6 +50,10 @@ def preprocess_image(img_path):
|
|
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."""
|
@@ -90,4 +95,4 @@ def predict():
|
|
90 |
return jsonify({"error": "Invalid file format"}), 400
|
91 |
|
92 |
if __name__ == "__main__":
|
93 |
-
app.run(host="0.0.0.0", port=7860, debug=True
|
|
|
14 |
|
15 |
# Set up uploads folder
|
16 |
UPLOAD_FOLDER = "uploads"
|
17 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True) # Ensure upload folder exists
|
18 |
+
os.chmod(UPLOAD_FOLDER, 0o755) # Set proper permissions
|
|
|
|
|
|
|
|
|
19 |
|
20 |
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
|
21 |
|
|
|
27 |
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
|
28 |
|
29 |
# Load trained model
|
30 |
+
model_path = "trained.h5"
|
31 |
+
if os.path.exists(model_path):
|
32 |
+
try:
|
33 |
+
model = tf.keras.models.load_model(model_path)
|
34 |
+
logging.info("β
Model loaded successfully.")
|
35 |
+
except Exception as e:
|
36 |
+
logging.error(f"β Error loading model: {e}")
|
37 |
+
model = None
|
38 |
+
else:
|
39 |
+
logging.error("β Model file not found! Ensure 'trained.h5' exists in the correct path.")
|
40 |
model = None
|
41 |
|
42 |
def preprocess_image(img_path):
|
43 |
+
"""Preprocess the image for model prediction."""
|
44 |
try:
|
45 |
img = image.load_img(img_path, target_size=(150, 150)) # Resize to model input size
|
46 |
img_array = image.img_to_array(img) / 255.0 # Normalize
|
|
|
50 |
logging.error(f"β Error in image preprocessing: {e}")
|
51 |
return None
|
52 |
|
53 |
+
@app.route("/", methods=["GET"])
|
54 |
+
def home():
|
55 |
+
return jsonify({"message": "Pneumonia Detection API is running!"})
|
56 |
+
|
57 |
@app.route("/predict", methods=["POST"])
|
58 |
def predict():
|
59 |
"""Handles image upload and model prediction."""
|
|
|
95 |
return jsonify({"error": "Invalid file format"}), 400
|
96 |
|
97 |
if __name__ == "__main__":
|
98 |
+
app.run(host="0.0.0.0", port=7860, debug=True) # Removed threaded=True for stability
|