Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ import io
|
|
| 10 |
import logging
|
| 11 |
import time
|
| 12 |
import os
|
|
|
|
| 13 |
from urllib.parse import urlparse
|
| 14 |
|
| 15 |
# Set up logging
|
|
@@ -139,10 +140,24 @@ def load_model(model_name):
|
|
| 139 |
if error:
|
| 140 |
return None, error
|
| 141 |
|
| 142 |
-
# Try to load the model
|
| 143 |
try:
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
# Cache the model
|
| 148 |
model_cache[model_name] = model
|
|
@@ -151,6 +166,12 @@ def load_model(model_name):
|
|
| 151 |
|
| 152 |
except Exception as load_error:
|
| 153 |
logger.error(f"Model loading error for {model_name}: {str(load_error)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
return None, f"Model loading failed: {str(load_error)}"
|
| 155 |
|
| 156 |
except Exception as e:
|
|
|
|
| 10 |
import logging
|
| 11 |
import time
|
| 12 |
import os
|
| 13 |
+
import tempfile
|
| 14 |
from urllib.parse import urlparse
|
| 15 |
|
| 16 |
# Set up logging
|
|
|
|
| 140 |
if error:
|
| 141 |
return None, error
|
| 142 |
|
| 143 |
+
# Try to load the model using temporary file
|
| 144 |
try:
|
| 145 |
+
import tempfile
|
| 146 |
+
|
| 147 |
+
# Create temporary file
|
| 148 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.keras') as tmp_file:
|
| 149 |
+
model_bytes.seek(0)
|
| 150 |
+
tmp_file.write(model_bytes.read())
|
| 151 |
+
tmp_file_path = tmp_file.name
|
| 152 |
+
|
| 153 |
+
# Load model from temporary file
|
| 154 |
+
model = tf.keras.models.load_model(tmp_file_path, compile=False)
|
| 155 |
+
|
| 156 |
+
# Clean up temporary file
|
| 157 |
+
try:
|
| 158 |
+
os.unlink(tmp_file_path)
|
| 159 |
+
except:
|
| 160 |
+
pass # Ignore cleanup errors
|
| 161 |
|
| 162 |
# Cache the model
|
| 163 |
model_cache[model_name] = model
|
|
|
|
| 166 |
|
| 167 |
except Exception as load_error:
|
| 168 |
logger.error(f"Model loading error for {model_name}: {str(load_error)}")
|
| 169 |
+
# Try to cleanup temp file if it exists
|
| 170 |
+
try:
|
| 171 |
+
if 'tmp_file_path' in locals():
|
| 172 |
+
os.unlink(tmp_file_path)
|
| 173 |
+
except:
|
| 174 |
+
pass
|
| 175 |
return None, f"Model loading failed: {str(load_error)}"
|
| 176 |
|
| 177 |
except Exception as e:
|