Hardik commited on
Commit
d939d85
·
1 Parent(s): 525fd77

LSTM: fallback to rebuild model arch and load weights

Browse files
ml_service/app/services/model_loader.py CHANGED
@@ -99,12 +99,6 @@ class ModelManager:
99
  cfg_path = os.path.join(settings.DATA_DIR, "config.pkl")
100
 
101
  if os.path.exists(model_path) and os.path.exists(tok_path) and os.path.exists(cfg_path):
102
- with open(model_path, "rb") as f:
103
- header = f.read(8)
104
- is_h5 = header[0:2] == b"\x89H"
105
- is_zip = header[0:2] == b"PK"
106
-
107
- loaded = False
108
  last_lstm_error = ""
109
 
110
  for loader_fn, name in [
@@ -114,14 +108,33 @@ class ModelManager:
114
  ]:
115
  try:
116
  self.lstm_model = loader_fn(model_path)
117
- loaded = True
118
  break
119
  except Exception as e:
120
  last_lstm_error = f"{name}: {str(e)}"
121
  continue
122
 
123
- if not loaded:
124
- raise RuntimeError(f"Could not load LSTM model ({'h5' if is_h5 else 'zip'}): {last_lstm_error}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  if hasattr(self.lstm_model, "signatures") and "serving_default" in self.lstm_model.signatures:
126
  self.lstm_fast_predict = self.lstm_model.signatures["serving_default"]
127
  else:
@@ -134,7 +147,6 @@ class ModelManager:
134
  self.lstm_tokenizer = pickle.load(f)
135
  with open(cfg_path, "rb") as f:
136
  self.lstm_config = pickle.load(f)
137
- self.models_loaded["lstm"] = True
138
  except Exception as e:
139
  self.load_errors["lstm"] = str(e)
140
  print(f"Failed to load LSTM: {e}")
 
99
  cfg_path = os.path.join(settings.DATA_DIR, "config.pkl")
100
 
101
  if os.path.exists(model_path) and os.path.exists(tok_path) and os.path.exists(cfg_path):
 
 
 
 
 
 
102
  last_lstm_error = ""
103
 
104
  for loader_fn, name in [
 
108
  ]:
109
  try:
110
  self.lstm_model = loader_fn(model_path)
111
+ self.models_loaded["lstm"] = True
112
  break
113
  except Exception as e:
114
  last_lstm_error = f"{name}: {str(e)}"
115
  continue
116
 
117
+ if not self.models_loaded["lstm"]:
118
+ try:
119
+ import keras.saving
120
+ rebuilt = keras.models.Sequential([
121
+ keras.layers.InputLayer(batch_shape=[None, 300]),
122
+ keras.layers.Embedding(50000, 128, mask_zero=True),
123
+ keras.layers.SpatialDropout1D(0.2),
124
+ keras.layers.Bidirectional(keras.layers.LSTM(64)),
125
+ keras.layers.Dropout(0.5),
126
+ keras.layers.Dense(1, activation="sigmoid"),
127
+ ])
128
+ rebuilt.load_weights(model_path)
129
+ self.lstm_model = rebuilt
130
+ self.models_loaded["lstm"] = True
131
+ last_lstm_error = ""
132
+ except Exception as e:
133
+ last_lstm_error = f"rebuild: {str(e)}"
134
+
135
+ if not self.models_loaded["lstm"]:
136
+ raise RuntimeError(f"Could not load LSTM model: {last_lstm_error}")
137
+
138
  if hasattr(self.lstm_model, "signatures") and "serving_default" in self.lstm_model.signatures:
139
  self.lstm_fast_predict = self.lstm_model.signatures["serving_default"]
140
  else:
 
147
  self.lstm_tokenizer = pickle.load(f)
148
  with open(cfg_path, "rb") as f:
149
  self.lstm_config = pickle.load(f)
 
150
  except Exception as e:
151
  self.load_errors["lstm"] = str(e)
152
  print(f"Failed to load LSTM: {e}")