isakovsh commited on
Commit
6eaf634
·
verified ·
1 Parent(s): 4a7e960

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import librosa
4
+ import tensorflow as tf
5
+ import random
6
+ import warnings
7
+ import joblib
8
+ warnings.filterwarnings("ignore")
9
+
10
+ # Load model and label encoder
11
+ model = tf.keras.models.load_model("final_model.h5")
12
+ label_encoder = joblib.load("le.pkl")
13
+
14
+ # Your feature extractor
15
+ def extract_features(y, sr):
16
+ try:
17
+ stft = np.abs(librosa.stft(y))
18
+ mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40)
19
+ mfcc_mean = np.mean(mfcc.T, axis=0)
20
+ chroma = librosa.feature.chroma_stft(S=stft, sr=sr)
21
+ chroma_mean = np.mean(chroma.T, axis=0)
22
+ contrast = librosa.feature.spectral_contrast(S=stft, sr=sr)
23
+ contrast_mean = np.mean(contrast.T, axis=0)
24
+ zcr = librosa.feature.zero_crossing_rate(y)
25
+ zcr_mean = np.mean(zcr)
26
+ rmse = librosa.feature.rms(y=y)
27
+ rmse_mean = np.mean(rmse)
28
+
29
+ return np.hstack([mfcc_mean, chroma_mean, contrast_mean, zcr_mean, rmse_mean])
30
+
31
+ except Exception as e:
32
+ print(f"Error extracting features: {e}")
33
+ return np.zeros(61)
34
+
35
+ # Prediction function
36
+ def predict_emotion(audio):
37
+ y, sr = librosa.load(audio, sr=None)
38
+ features = extract_features(y, sr)
39
+ features = features.reshape(1, -1) # Make it 2D
40
+ prediction = model.predict(features)
41
+ predicted_label = label_encoder.inverse_transform([np.argmax(prediction)])[0]
42
+ return predicted_label
43
+
44
+ examples = [["happy.wav"], ["sad.wav"], ["angry.wav"]]
45
+
46
+ # Gradio Interface
47
+ interface = gr.Interface(
48
+ fn=predict_emotion,
49
+ inputs=gr.Audio(type="filepath"),
50
+ outputs="label",
51
+ title="🎙️ Emotion Recognition from Audio",
52
+ description="Upload or record your voice to predict the emotion using a TensorFlow model trained on audio features.",
53
+ examples=examples
54
+ )
55
+
56
+ interface.launch()