DjPapzin commited on
Commit
c3d0bfd
·
1 Parent(s): 165caa4

fix: Correct file paths for loading models in Hugging Face Space

Browse files
Files changed (1) hide show
  1. frontend/app.py +7 -21
frontend/app.py CHANGED
@@ -57,13 +57,9 @@ st.title("Medi Scape Dashboard")
57
  # --- Session State Initialization ---
58
  if 'disease_model' not in st.session_state:
59
  try:
60
- # Check if running on Hugging Face Spaces
61
- if 'HF_SPACE_ID' in os.environ:
62
- model_path = 'FINAL_MODEL.h5' # Updated to .h5
63
- st.session_state.disease_model = tf.keras.models.load_model(model_path)
64
- else: # Assume running locally
65
- model_path = 'FINAL_MODEL.h5' # Updated to .h5
66
- st.session_state.disease_model = tf.keras.models.load_model(model_path)
67
  print("Disease model loaded successfully!")
68
  except FileNotFoundError:
69
  st.error("Disease classification model not found. Please ensure 'FINAL_MODEL.h5' is in the correct directory.")
@@ -76,8 +72,6 @@ if 'disease_model' not in st.session_state:
76
  if 'vectorizer' not in st.session_state:
77
  try:
78
  vectorizer_path = "vectorizer.pkl"
79
- print(f"Attempting to load vectorizer from: {vectorizer_path}")
80
- print(f"Vectorizer file exists: {os.path.exists(vectorizer_path)}")
81
  st.session_state.vectorizer = pd.read_pickle(vectorizer_path)
82
  print("Vectorizer loaded successfully!")
83
  except FileNotFoundError:
@@ -90,14 +84,11 @@ if 'vectorizer' not in st.session_state:
90
  # Load the logistic regression model using joblib
91
  if 'model_llm' not in st.session_state:
92
  try:
93
- llm_model_path = "logistic_regression_model.pkl" # Corrected path
94
- print(f"Attempting to load LLM model from: {llm_model_path}")
95
- print(f"LLM Model file exists: {os.path.exists(llm_model_path)}")
96
- # Load the model correctly using joblib
97
  st.session_state.model_llm = joblib.load(llm_model_path)
98
  print("LLM model loaded successfully!")
99
  except FileNotFoundError:
100
- st.error("LLM model file not found. Please ensure 'logistic_regression_model.pkl' is in the 'frontend' directory.")
101
  st.session_state.model_llm = None
102
  except Exception as e:
103
  st.error(f"An error occurred while loading the LLM model: {e}")
@@ -107,13 +98,8 @@ if 'model_llm' not in st.session_state:
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.h5' # Updated to .h5
113
- disease_model = tf.keras.models.load_model(model_path)
114
- else: # Assume running locally
115
- model_path = 'FINAL_MODEL.h5' # Updated to .h5
116
- disease_model = tf.keras.models.load_model(model_path)
117
  except FileNotFoundError:
118
  st.error("Disease classification model not found. Please ensure 'FINAL_MODEL.h5' is in the correct directory.")
119
  disease_model = None
 
57
  # --- Session State Initialization ---
58
  if 'disease_model' not in st.session_state:
59
  try:
60
+ # Assuming all models are in the root directory of your Hugging Face Space
61
+ model_path = 'FINAL_MODEL.h5'
62
+ st.session_state.disease_model = tf.keras.models.load_model(model_path)
 
 
 
 
63
  print("Disease model loaded successfully!")
64
  except FileNotFoundError:
65
  st.error("Disease classification model not found. Please ensure 'FINAL_MODEL.h5' is in the correct directory.")
 
72
  if 'vectorizer' not in st.session_state:
73
  try:
74
  vectorizer_path = "vectorizer.pkl"
 
 
75
  st.session_state.vectorizer = pd.read_pickle(vectorizer_path)
76
  print("Vectorizer loaded successfully!")
77
  except FileNotFoundError:
 
84
  # Load the logistic regression model using joblib
85
  if 'model_llm' not in st.session_state:
86
  try:
87
+ llm_model_path = "logistic_regression_model.pkl"
 
 
 
88
  st.session_state.model_llm = joblib.load(llm_model_path)
89
  print("LLM model loaded successfully!")
90
  except FileNotFoundError:
91
+ st.error("LLM model file not found. Please ensure 'logistic_regression_model.pkl' is in the correct directory.")
92
  st.session_state.model_llm = None
93
  except Exception as e:
94
  st.error(f"An error occurred while loading the LLM model: {e}")
 
98
 
99
  # Load the disease classification model (outside session state for this example)
100
  try:
101
+ model_path = 'FINAL_MODEL.h5'
102
+ disease_model = tf.keras.models.load_model(model_path)
 
 
 
 
 
103
  except FileNotFoundError:
104
  st.error("Disease classification model not found. Please ensure 'FINAL_MODEL.h5' is in the correct directory.")
105
  disease_model = None