DjPapzin commited on
Commit
f5b6e9f
·
1 Parent(s): 398c97f

fix: Update model loading logic for Hugging Face Spaces

Browse files
Files changed (1) hide show
  1. frontend/app.py +23 -15
frontend/app.py CHANGED
@@ -56,16 +56,19 @@ st.title("Medi Scape Dashboard")
56
  # --- Session State Initialization ---
57
  if 'disease_model' not in st.session_state:
58
  try:
59
- model_path = 'FINAL_MODEL.zip' # Updated path to zip file
60
- print(f"Attempting to load disease model from: {model_path}")
61
- print(f"Model file exists: {os.path.exists(model_path)}")
62
- with tf.keras.utils.get_file('FINAL_MODEL.keras', model_path, extract=True) as extracted_model_path:
63
- model_dir = os.path.dirname(extracted_model_path)
64
- model_path = os.path.join(model_dir, 'FINAL_MODEL.keras')
 
 
 
65
  st.session_state.disease_model = tf.keras.models.load_model(model_path)
66
  print("Disease model loaded successfully!")
67
  except FileNotFoundError:
68
- st.error("Disease classification model not found. Please ensure 'FINAL_MODEL.zip' is in the same directory as this app.")
69
  st.session_state.disease_model = None
70
  except PermissionError:
71
  st.error("Permission error accessing 'model.weights.h5'. Please ensure the file is not being used by another process.")
@@ -102,15 +105,20 @@ if 'model_llm' not in st.session_state:
102
 
103
  # --- End of Session State Initialization ---
104
 
105
- # Load the disease classification model
106
  try:
107
- model_path = 'FINAL_MODEL.zip' # Updated path to zip file
108
- with tf.keras.utils.get_file('FINAL_MODEL.keras', model_path, extract=True) as extracted_model_path:
109
- model_dir = os.path.dirname(extracted_model_path)
110
- model_path = os.path.join(model_dir, 'FINAL_MODEL.keras')
 
 
 
 
 
111
  disease_model = tf.keras.models.load_model(model_path)
112
  except FileNotFoundError:
113
- st.error("Disease classification model not found. Please ensure 'FINAL_MODEL.zip' is in the same directory as this app.")
114
  disease_model = None
115
  except PermissionError:
116
  st.error("Permission error accessing 'model.weights.h5'. Please ensure the file is not being used by another process.")
@@ -401,7 +409,7 @@ else:
401
  st.write("Upload a chest X-ray image for disease detection.")
402
  uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
403
 
404
- if uploaded_image is not None and st.session_state.disease_model is not None:
405
  # Display the image
406
  img_opened = Image.open(uploaded_image).convert('RGB')
407
  image_pred = np.array(img_opened)
@@ -417,7 +425,7 @@ else:
417
  image_pred = np.expand_dims(image_pred, axis=0)
418
 
419
  # Predict using the model
420
- prediction = st.session_state.disease_model.predict(image_pred)
421
 
422
  # Get the predicted class
423
  predicted_ = np.argmax(prediction)
 
56
  # --- Session State Initialization ---
57
  if 'disease_model' not in st.session_state:
58
  try:
59
+ # Check if running on Hugging Face Spaces
60
+ if 'HF_SPACE_ID' in os.environ:
61
+ model_path = 'FINAL_MODEL.zip'
62
+ with tf.keras.utils.get_file('FINAL_MODEL.keras', model_path, extract=True) as extracted_model_path:
63
+ model_dir = os.path.dirname(extracted_model_path)
64
+ model_path = os.path.join(model_dir, 'FINAL_MODEL.keras')
65
+ st.session_state.disease_model = tf.keras.models.load_model(model_path)
66
+ else: # Assume running locally
67
+ model_path = 'FINAL_MODEL.keras'
68
  st.session_state.disease_model = tf.keras.models.load_model(model_path)
69
  print("Disease model loaded successfully!")
70
  except FileNotFoundError:
71
+ st.error("Disease classification model not found. Please ensure 'FINAL_MODEL.zip' (for Hugging Face) or 'FINAL_MODEL.keras' (for local) is in the correct directory.")
72
  st.session_state.disease_model = None
73
  except PermissionError:
74
  st.error("Permission error accessing 'model.weights.h5'. Please ensure the file is not being used by another process.")
 
105
 
106
  # --- End of Session State Initialization ---
107
 
108
+ # Load the disease classification model (outside session state for this example)
109
  try:
110
+ # Check if running on Hugging Face Spaces
111
+ if 'HF_SPACE_ID' in os.environ:
112
+ model_path = 'FINAL_MODEL.zip'
113
+ with tf.keras.utils.get_file('FINAL_MODEL.keras', model_path, extract=True) as extracted_model_path:
114
+ model_dir = os.path.dirname(extracted_model_path)
115
+ model_path = os.path.join(model_dir, 'FINAL_MODEL.keras')
116
+ disease_model = tf.keras.models.load_model(model_path)
117
+ else: # Assume running locally
118
+ model_path = 'FINAL_MODEL.keras'
119
  disease_model = tf.keras.models.load_model(model_path)
120
  except FileNotFoundError:
121
+ st.error("Disease classification model not found. Please ensure 'FINAL_MODEL.zip' (for Hugging Face) or 'FINAL_MODEL.keras' (for local) is in the correct directory.")
122
  disease_model = None
123
  except PermissionError:
124
  st.error("Permission error accessing 'model.weights.h5'. Please ensure the file is not being used by another process.")
 
409
  st.write("Upload a chest X-ray image for disease detection.")
410
  uploaded_image = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
411
 
412
+ if uploaded_image is not None and disease_model is not None: # Use disease_model directly
413
  # Display the image
414
  img_opened = Image.open(uploaded_image).convert('RGB')
415
  image_pred = np.array(img_opened)
 
425
  image_pred = np.expand_dims(image_pred, axis=0)
426
 
427
  # Predict using the model
428
+ prediction = disease_model.predict(image_pred) # Use disease_model directly
429
 
430
  # Get the predicted class
431
  predicted_ = np.argmax(prediction)