ujan2003 commited on
Commit
849030e
Β·
verified Β·
1 Parent(s): 160f6b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
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
- 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
 
@@ -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
- 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
@@ -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, threaded=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