Upload app.py
Browse files
app.py
CHANGED
|
@@ -215,36 +215,49 @@ def load_translation_model():
|
|
| 215 |
st.warning("Keras or HuggingFace Hub not available. Model loading skipped.")
|
| 216 |
return None
|
| 217 |
|
| 218 |
-
model
|
| 219 |
-
model.add(Input(shape=((20, 156))))
|
| 220 |
-
model.add(keras.layers.Masking(mask_value=0.))
|
| 221 |
-
model.add(BatchNormalization())
|
| 222 |
-
model.add(Bidirectional(LSTM(32, recurrent_dropout=0.2, return_sequences=True)))
|
| 223 |
-
|
| 224 |
-
model.add(Dropout(0.2))
|
| 225 |
-
model.add(Bidirectional(LSTM(32, recurrent_dropout=0.2)))
|
| 226 |
-
|
| 227 |
-
model.add(keras.layers.Activation('elu'))
|
| 228 |
-
model.add(Dense(32, use_bias=False, kernel_initializer='he_normal'))
|
| 229 |
-
|
| 230 |
-
model.add(BatchNormalization())
|
| 231 |
-
model.add(Dropout(0.2))
|
| 232 |
-
model.add(keras.layers.Activation('elu'))
|
| 233 |
-
model.add(Dense(32, kernel_initializer='he_normal', use_bias=False))
|
| 234 |
-
|
| 235 |
-
model.add(BatchNormalization())
|
| 236 |
-
model.add(keras.layers.Activation('elu'))
|
| 237 |
-
model.add(Dropout(0.2))
|
| 238 |
-
model.add(Dense(len(list(expression_mapping.keys())), activation='softmax'))
|
| 239 |
-
|
| 240 |
-
# Download pre-trained model weights
|
| 241 |
model_file = hf_hub_download(
|
| 242 |
repo_id="sunilsarolkar/isl-translation-model",
|
| 243 |
filename="isl_model_final.keras"
|
| 244 |
)
|
| 245 |
-
model.load_weights(model_file)
|
| 246 |
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
except Exception as e:
|
| 249 |
st.error(f"Failed to load translation model: {e}")
|
| 250 |
return None
|
|
@@ -795,32 +808,47 @@ elif app_mode == 'Test Video Translation':
|
|
| 795 |
# Load translation model
|
| 796 |
translation_model = load_translation_model()
|
| 797 |
|
| 798 |
-
#
|
| 799 |
-
sequence_idx = frame_idx - 20
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 804 |
)
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 813 |
|
| 814 |
# Update frame-wise predictions for weighted average
|
| 815 |
for sign, prob in zip(top_3_signs, top_3_probabilities):
|
| 816 |
if sign not in frame_predictions:
|
| 817 |
frame_predictions[sign] = []
|
| 818 |
frame_predictions[sign].append(prob)
|
| 819 |
-
|
| 820 |
-
# Current frame predictions
|
| 821 |
-
current_predictions = {}
|
| 822 |
-
for sign, prob in zip(top_3_signs, top_3_probabilities):
|
| 823 |
-
current_predictions[sign] = prob
|
| 824 |
|
| 825 |
# Calculate weighted averages
|
| 826 |
for sign in frame_predictions:
|
|
@@ -868,8 +896,12 @@ elif app_mode == 'Test Video Translation':
|
|
| 868 |
video_writer.write_frame(canvas_with_predictions)
|
| 869 |
|
| 870 |
# Get best prediction for display
|
| 871 |
-
|
| 872 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 873 |
|
| 874 |
# Update progress display
|
| 875 |
with progress_container.container():
|
|
|
|
| 215 |
st.warning("Keras or HuggingFace Hub not available. Model loading skipped.")
|
| 216 |
return None
|
| 217 |
|
| 218 |
+
# Download pre-trained model file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
model_file = hf_hub_download(
|
| 220 |
repo_id="sunilsarolkar/isl-translation-model",
|
| 221 |
filename="isl_model_final.keras"
|
| 222 |
)
|
|
|
|
| 223 |
|
| 224 |
+
# Try to load the complete model first
|
| 225 |
+
try:
|
| 226 |
+
model = keras.models.load_model(model_file)
|
| 227 |
+
st.success("✅ Model loaded successfully from saved file")
|
| 228 |
+
return model
|
| 229 |
+
except Exception as load_error:
|
| 230 |
+
st.warning(f"Failed to load complete model: {load_error}")
|
| 231 |
+
st.info("Attempting to build model architecture and load weights...")
|
| 232 |
+
|
| 233 |
+
# Fallback: Build model architecture and load weights
|
| 234 |
+
model = Sequential()
|
| 235 |
+
model.add(Input(shape=((20, 156))))
|
| 236 |
+
model.add(keras.layers.Masking(mask_value=0.))
|
| 237 |
+
model.add(BatchNormalization())
|
| 238 |
+
model.add(Bidirectional(LSTM(32, recurrent_dropout=0.2, return_sequences=True)))
|
| 239 |
+
|
| 240 |
+
model.add(Dropout(0.2))
|
| 241 |
+
model.add(Bidirectional(LSTM(32, recurrent_dropout=0.2)))
|
| 242 |
+
|
| 243 |
+
model.add(keras.layers.Activation('elu'))
|
| 244 |
+
model.add(Dense(32, use_bias=False, kernel_initializer='he_normal'))
|
| 245 |
+
|
| 246 |
+
model.add(BatchNormalization())
|
| 247 |
+
model.add(Dropout(0.2))
|
| 248 |
+
model.add(keras.layers.Activation('elu'))
|
| 249 |
+
model.add(Dense(32, kernel_initializer='he_normal', use_bias=False))
|
| 250 |
+
|
| 251 |
+
model.add(BatchNormalization())
|
| 252 |
+
model.add(keras.layers.Activation('elu'))
|
| 253 |
+
model.add(Dropout(0.2))
|
| 254 |
+
model.add(Dense(len(list(expression_mapping.keys())), activation='softmax'))
|
| 255 |
+
|
| 256 |
+
# Try to load weights
|
| 257 |
+
model.load_weights(model_file)
|
| 258 |
+
st.success("✅ Model architecture built and weights loaded successfully")
|
| 259 |
+
return model
|
| 260 |
+
|
| 261 |
except Exception as e:
|
| 262 |
st.error(f"Failed to load translation model: {e}")
|
| 263 |
return None
|
|
|
|
| 808 |
# Load translation model
|
| 809 |
translation_model = load_translation_model()
|
| 810 |
|
| 811 |
+
# Check if model loaded successfully
|
| 812 |
+
sequence_idx = frame_idx - 20 # Define sequence_idx for both cases
|
| 813 |
+
if translation_model is None:
|
| 814 |
+
st.error("❌ Translation model failed to load. Cannot make predictions.")
|
| 815 |
+
# Use dummy predictions to keep the visualization working
|
| 816 |
+
current_predictions = {"model_not_available": 0.0}
|
| 817 |
+
top_3_signs = ["model_not_available"]
|
| 818 |
+
top_3_probabilities = [0.0]
|
| 819 |
+
else:
|
| 820 |
+
# Make prediction on current window
|
| 821 |
+
prediction_output = translation_model(
|
| 822 |
+
X_test_processed[sequence_idx].reshape(
|
| 823 |
+
1, X_test_processed[sequence_idx].shape[0],
|
| 824 |
+
X_test_processed[sequence_idx].shape[1]
|
| 825 |
+
)
|
| 826 |
)
|
| 827 |
+
|
| 828 |
+
# Handle both PyTorch and Keras/TensorFlow models
|
| 829 |
+
try:
|
| 830 |
+
# Try PyTorch tensor operations first
|
| 831 |
+
prediction_output = prediction_output[0].cpu().detach().numpy()
|
| 832 |
+
except AttributeError:
|
| 833 |
+
# If it's a Keras model, it already returns NumPy arrays
|
| 834 |
+
prediction_output = prediction_output[0]
|
| 835 |
+
|
| 836 |
+
# Get top predictions
|
| 837 |
+
top_prediction_idx = np.argmax(prediction_output)
|
| 838 |
+
top_3_indices = prediction_output.argsort()[-3:][::-1]
|
| 839 |
+
top_3_signs = [expression_mapping[i] for i in top_3_indices]
|
| 840 |
+
top_3_probabilities = prediction_output[top_3_indices]
|
| 841 |
+
|
| 842 |
+
# Current frame predictions
|
| 843 |
+
current_predictions = {}
|
| 844 |
+
for sign, prob in zip(top_3_signs, top_3_probabilities):
|
| 845 |
+
current_predictions[sign] = prob
|
| 846 |
|
| 847 |
# Update frame-wise predictions for weighted average
|
| 848 |
for sign, prob in zip(top_3_signs, top_3_probabilities):
|
| 849 |
if sign not in frame_predictions:
|
| 850 |
frame_predictions[sign] = []
|
| 851 |
frame_predictions[sign].append(prob)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 852 |
|
| 853 |
# Calculate weighted averages
|
| 854 |
for sign in frame_predictions:
|
|
|
|
| 896 |
video_writer.write_frame(canvas_with_predictions)
|
| 897 |
|
| 898 |
# Get best prediction for display
|
| 899 |
+
if weighted_predictions:
|
| 900 |
+
best_sign = max(weighted_predictions, key=weighted_predictions.get)
|
| 901 |
+
best_confidence = weighted_predictions[best_sign]
|
| 902 |
+
else:
|
| 903 |
+
best_sign = "no_predictions"
|
| 904 |
+
best_confidence = 0.0
|
| 905 |
|
| 906 |
# Update progress display
|
| 907 |
with progress_container.container():
|