Update app.py
Browse files
app.py
CHANGED
|
@@ -3,59 +3,71 @@ import tensorflow as tf
|
|
| 3 |
import librosa
|
| 4 |
import numpy as np
|
| 5 |
import warnings
|
|
|
|
|
|
|
| 6 |
warnings.filterwarnings('ignore')
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
from tensorflow import keras
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
print("
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# Layer 1-2
|
| 21 |
-
keras.layers.Dense(512, activation='relu', input_shape=(40,)),
|
| 22 |
-
keras.layers.Dropout(0.4),
|
| 23 |
-
|
| 24 |
-
# Layer 3-4
|
| 25 |
-
keras.layers.Dense(512, activation='relu'),
|
| 26 |
-
keras.layers.Dropout(0.4),
|
| 27 |
-
|
| 28 |
-
# Layer 5-6
|
| 29 |
-
keras.layers.Dense(256, activation='relu'),
|
| 30 |
-
keras.layers.Dropout(0.3),
|
| 31 |
|
| 32 |
-
|
| 33 |
-
keras.layers.Dense(256, activation='relu'),
|
| 34 |
-
keras.layers.Dropout(0.3),
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
])
|
| 48 |
|
|
|
|
|
|
|
| 49 |
print("Loading weights from best_model1_weights.h5...")
|
| 50 |
model.load_weights('best_model1_weights.h5')
|
| 51 |
-
print("
|
|
|
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
-
print(f"
|
| 55 |
import traceback
|
| 56 |
traceback.print_exc()
|
| 57 |
model = None
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
EMOTIONS = ['angry', 'disgust', 'fearful', 'happy', 'neutral', 'sad', 'surprised']
|
| 60 |
|
| 61 |
def preprocess_audio(audio_path):
|
|
@@ -99,7 +111,7 @@ def predict_emotion(audio_path):
|
|
| 99 |
return f"Error: {str(e)}", "N/A", {}
|
| 100 |
|
| 101 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 102 |
-
gr.Markdown("#
|
| 103 |
gr.Markdown("Record your voice or upload an audio file to detect emotions")
|
| 104 |
|
| 105 |
with gr.Row():
|
|
@@ -122,7 +134,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 122 |
outputs=[emotion_output, confidence_output, all_predictions]
|
| 123 |
)
|
| 124 |
|
| 125 |
-
gr.Markdown("###
|
| 126 |
gr.Markdown("- Speak clearly for 2-3 seconds")
|
| 127 |
gr.Markdown("- Avoid background noise")
|
| 128 |
gr.Markdown("- Try different emotions!")
|
|
|
|
| 3 |
import librosa
|
| 4 |
import numpy as np
|
| 5 |
import warnings
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
warnings.filterwarnings('ignore')
|
| 9 |
|
| 10 |
+
print("="*50)
|
| 11 |
+
print("STARTING MODEL LOAD")
|
| 12 |
+
print("Files in directory:", os.listdir('.'))
|
| 13 |
+
print("="*50)
|
| 14 |
+
|
| 15 |
+
model = None
|
| 16 |
+
|
| 17 |
try:
|
| 18 |
from tensorflow import keras
|
| 19 |
|
| 20 |
+
if not os.path.exists('best_model1_weights.h5'):
|
| 21 |
+
print("ERROR: best_model1_weights.h5 NOT FOUND!")
|
| 22 |
+
print("Available files:", os.listdir('.'))
|
| 23 |
+
else:
|
| 24 |
+
print("Weight file found!")
|
| 25 |
+
file_size = os.path.getsize('best_model1_weights.h5') / (1024*1024)
|
| 26 |
+
print(f"Weight file size: {file_size:.2f} MB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
print("Building model architecture with 13 layers...")
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
model = keras.Sequential([
|
| 31 |
+
keras.layers.Dense(512, activation='relu', input_shape=(40,)),
|
| 32 |
+
keras.layers.Dropout(0.4),
|
| 33 |
+
keras.layers.Dense(512, activation='relu'),
|
| 34 |
+
keras.layers.Dropout(0.4),
|
| 35 |
+
keras.layers.Dense(256, activation='relu'),
|
| 36 |
+
keras.layers.Dropout(0.3),
|
| 37 |
+
keras.layers.Dense(256, activation='relu'),
|
| 38 |
+
keras.layers.Dropout(0.3),
|
| 39 |
+
keras.layers.Dense(128, activation='relu'),
|
| 40 |
+
keras.layers.Dropout(0.3),
|
| 41 |
+
keras.layers.Dense(64, activation='relu'),
|
| 42 |
+
keras.layers.Dropout(0.2),
|
| 43 |
+
keras.layers.Dense(7, activation='softmax')
|
| 44 |
])
|
| 45 |
|
| 46 |
+
print("Architecture built successfully!")
|
| 47 |
+
|
| 48 |
print("Loading weights from best_model1_weights.h5...")
|
| 49 |
model.load_weights('best_model1_weights.h5')
|
| 50 |
+
print("WEIGHTS LOADED SUCCESSFULLY!")
|
| 51 |
+
print("="*50)
|
| 52 |
|
| 53 |
+
except FileNotFoundError as e:
|
| 54 |
+
print(f"FILE NOT FOUND ERROR: {e}")
|
| 55 |
+
model = None
|
| 56 |
+
except ValueError as e:
|
| 57 |
+
print(f"SHAPE MISMATCH ERROR: {e}")
|
| 58 |
+
print("Architecture does not match the weights!")
|
| 59 |
+
model = None
|
| 60 |
except Exception as e:
|
| 61 |
+
print(f"ERROR: {type(e).__name__}: {e}")
|
| 62 |
import traceback
|
| 63 |
traceback.print_exc()
|
| 64 |
model = None
|
| 65 |
|
| 66 |
+
if model is None:
|
| 67 |
+
print("MODEL FAILED TO LOAD!")
|
| 68 |
+
else:
|
| 69 |
+
print("MODEL READY FOR PREDICTIONS!")
|
| 70 |
+
|
| 71 |
EMOTIONS = ['angry', 'disgust', 'fearful', 'happy', 'neutral', 'sad', 'surprised']
|
| 72 |
|
| 73 |
def preprocess_audio(audio_path):
|
|
|
|
| 111 |
return f"Error: {str(e)}", "N/A", {}
|
| 112 |
|
| 113 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 114 |
+
gr.Markdown("# Emotion Analysis from Speech")
|
| 115 |
gr.Markdown("Record your voice or upload an audio file to detect emotions")
|
| 116 |
|
| 117 |
with gr.Row():
|
|
|
|
| 134 |
outputs=[emotion_output, confidence_output, all_predictions]
|
| 135 |
)
|
| 136 |
|
| 137 |
+
gr.Markdown("### Tips:")
|
| 138 |
gr.Markdown("- Speak clearly for 2-3 seconds")
|
| 139 |
gr.Markdown("- Avoid background noise")
|
| 140 |
gr.Markdown("- Try different emotions!")
|