final config
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
|
|
|
| 3 |
import keras
|
| 4 |
import numpy as np
|
| 5 |
import pandas as pd
|
|
@@ -15,52 +16,77 @@ encoder_model = None
|
|
| 15 |
load_error = None
|
| 16 |
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def load_resources():
|
| 27 |
global model, encoder_model, load_error
|
| 28 |
load_error = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if os.path.exists(MODEL_PATH):
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
MODEL_PATH,
|
| 34 |
-
compile=False,
|
| 35 |
-
safe_mode=False,
|
| 36 |
-
custom_objects={
|
| 37 |
-
"Dense": DenseCompat,
|
| 38 |
-
"keras.layers.Dense": DenseCompat,
|
| 39 |
-
},
|
| 40 |
-
)
|
| 41 |
-
except Exception:
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
MODEL_PATH,
|
| 45 |
compile=False,
|
| 46 |
-
|
| 47 |
-
"Dense": DenseCompat,
|
| 48 |
-
"keras.layers.Dense": DenseCompat,
|
| 49 |
-
},
|
| 50 |
)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
else:
|
| 63 |
-
load_error = f"Model file not found at {MODEL_PATH}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
# Initial load
|
| 66 |
load_resources()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
| 3 |
+
from tensorflow.keras import layers, Model
|
| 4 |
import keras
|
| 5 |
import numpy as np
|
| 6 |
import pandas as pd
|
|
|
|
| 16 |
load_error = None
|
| 17 |
|
| 18 |
|
| 19 |
+
def build_model(input_dim=18241, latent_dim=32):
|
| 20 |
+
"""
|
| 21 |
+
Rebuild the model architecture from scratch to avoid deserialization issues.
|
| 22 |
+
Architecture matches the training notebook exactly.
|
| 23 |
+
"""
|
| 24 |
+
inputs = layers.Input(shape=(input_dim,))
|
| 25 |
+
|
| 26 |
+
# Encoder
|
| 27 |
+
x = layers.Dense(512, activation="relu")(inputs)
|
| 28 |
+
x = layers.BatchNormalization()(x)
|
| 29 |
+
x = layers.Dropout(0.3)(x)
|
| 30 |
+
x = layers.Dense(128, activation="relu")(x)
|
| 31 |
+
latent = layers.Dense(latent_dim, name="latent")(x)
|
| 32 |
+
|
| 33 |
+
# Decoder
|
| 34 |
+
x = layers.Dense(128, activation="relu")(latent)
|
| 35 |
+
x = layers.Dense(512, activation="relu")(x)
|
| 36 |
+
reconstruction = layers.Dense(input_dim, name="reconstruction")(x)
|
| 37 |
+
|
| 38 |
+
# Age prediction head
|
| 39 |
+
age_pred = layers.Dense(1, name="age")(latent)
|
| 40 |
+
|
| 41 |
+
model = Model(inputs=inputs, outputs=[reconstruction, age_pred])
|
| 42 |
+
return model
|
| 43 |
+
|
| 44 |
|
| 45 |
def load_resources():
|
| 46 |
global model, encoder_model, load_error
|
| 47 |
load_error = None
|
| 48 |
+
|
| 49 |
+
# Build model from architecture
|
| 50 |
+
model = build_model()
|
| 51 |
+
|
| 52 |
+
# Try to load weights from saved file
|
| 53 |
if os.path.exists(MODEL_PATH):
|
| 54 |
try:
|
| 55 |
+
print(f"Loading weights from {MODEL_PATH}...")
|
| 56 |
+
# Try to load as keras model first to extract weights
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
try:
|
| 58 |
+
saved_model = keras.saving.load_model(
|
| 59 |
MODEL_PATH,
|
| 60 |
compile=False,
|
| 61 |
+
safe_mode=False,
|
|
|
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
+
model.set_weights(saved_model.get_weights())
|
| 64 |
+
print("Weights loaded successfully from .keras file.")
|
| 65 |
+
except Exception:
|
| 66 |
+
# Fallback: try loading as h5 or other format
|
| 67 |
+
try:
|
| 68 |
+
model.load_weights(MODEL_PATH)
|
| 69 |
+
print("Weights loaded successfully.")
|
| 70 |
+
except Exception as e:
|
| 71 |
+
load_error = f"Could not load weights: {e}"
|
| 72 |
+
print(f"Warning: {load_error}")
|
| 73 |
+
print("Model will run with random weights.")
|
| 74 |
+
except Exception as e:
|
| 75 |
+
load_error = f"Error loading weights: {e}"
|
| 76 |
+
print(f"Warning: {load_error}")
|
| 77 |
+
print("Model will run with random weights.")
|
| 78 |
else:
|
| 79 |
+
load_error = f"Model file not found at {MODEL_PATH}. Model will run with random weights."
|
| 80 |
+
print(load_error)
|
| 81 |
+
|
| 82 |
+
# Create encoder model
|
| 83 |
+
try:
|
| 84 |
+
latent_layer = model.get_layer("latent")
|
| 85 |
+
encoder_model = Model(inputs=model.input, outputs=latent_layer.output)
|
| 86 |
+
print("Encoder model created successfully.")
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print(f"Warning: Could not create encoder model: {e}")
|
| 89 |
+
|
| 90 |
|
| 91 |
# Initial load
|
| 92 |
load_resources()
|