ViannyCruz commited on
Commit
d3f142b
·
verified ·
1 Parent(s): da48ebe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -5
app.py CHANGED
@@ -104,14 +104,25 @@ def admin_required(f):
104
  # ------------------------------------------------------------------ #
105
  def load_model():
106
  global model
107
- # Usar ruta absoluta para evitar problemas con directorio de trabajo
108
  app_dir = os.path.dirname(os.path.abspath(__file__))
109
  model_files = [f for f in os.listdir(app_dir) if f.endswith('.h5')]
110
  if not model_files:
111
  print(f"ERROR: No hay archivos .h5 en {app_dir}")
112
  return False
113
  model_path = os.path.join(app_dir, model_files[0])
114
- print(f"Cargando modelo: {model_path}")
 
 
 
 
 
 
 
 
 
 
 
 
115
  try:
116
  from tensorflow.keras.applications import EfficientNetB0
117
  from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout, BatchNormalization
@@ -134,10 +145,11 @@ def load_model():
134
 
135
  test = np.random.random((1, 224, 224, 3)).astype(np.float32) * 255
136
  model.predict(test, verbose=0)
137
- print(f"Modelo cargado exitosamente: {model_path}")
138
  return True
139
- except Exception as e:
140
- print(f"Error cargando modelo: {e}")
 
141
  return False
142
 
143
  def preprocess_image(image_bytes) -> Optional[np.ndarray]:
 
104
  # ------------------------------------------------------------------ #
105
  def load_model():
106
  global model
 
107
  app_dir = os.path.dirname(os.path.abspath(__file__))
108
  model_files = [f for f in os.listdir(app_dir) if f.endswith('.h5')]
109
  if not model_files:
110
  print(f"ERROR: No hay archivos .h5 en {app_dir}")
111
  return False
112
  model_path = os.path.join(app_dir, model_files[0])
113
+ print(f"Intentando cargar modelo: {model_path}")
114
+
115
+ # Intento 1: carga directa del archivo completo
116
+ try:
117
+ model = tf.keras.models.load_model(model_path, compile=False)
118
+ test = np.random.random((1, 224, 224, 3)).astype(np.float32) * 255
119
+ model.predict(test, verbose=0)
120
+ print(f"Modelo cargado con load_model(): {model_path}")
121
+ return True
122
+ except Exception as e1:
123
+ print(f"load_model() falló: {e1}")
124
+
125
+ # Intento 2: reconstruir arquitectura y cargar pesos
126
  try:
127
  from tensorflow.keras.applications import EfficientNetB0
128
  from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout, BatchNormalization
 
145
 
146
  test = np.random.random((1, 224, 224, 3)).astype(np.float32) * 255
147
  model.predict(test, verbose=0)
148
+ print(f"Modelo cargado con load_weights(): {model_path}")
149
  return True
150
+ except Exception as e2:
151
+ print(f"load_weights() falló: {e2}")
152
+ model = None
153
  return False
154
 
155
  def preprocess_image(image_bytes) -> Optional[np.ndarray]: