codendushy/bbbbbbbbbbb
0
1import streamlit as st2import numpy as np3import librosa4from tensorflow.keras.models import load_model5import pickle6 7MODEL_PATH = 'model3.keras'8LABEL_ENCODER_PATH = 'label_encoder.pkl'9 10@st.cache_resource11def load_artifacts():12 model = load_model(MODEL_PATH)13 with open(LABEL_ENCODER_PATH, 'rb') as f:14 le = pickle.load(f)15 return model, le16 17model, le = load_artifacts()18 19def extract_mfcc_sequence(file_path, n_mfcc=40, max_len=200):20 y, sr = librosa.load(file_path, res_type='scipy')21 mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=n_mfcc)22 if mfcc.shape[1] < max_len:23 pad_width = max_len - mfcc.shape[1]24 mfcc = np.pad(mfcc, pad_width=((0,0),(0,pad_width)), mode='constant')25 else:26 mfcc = mfcc[:, :max_len]27 return mfcc.T28 29def predict_emotion(file_path):30 mfcc_seq = extract_mfcc_sequence(file_path)31 mfcc_seq = np.expand_dims(mfcc_seq, axis=0)32 pred = model.predict(mfcc_seq)33 predicted_class = np.argmax(pred)34 return le.classes_[predicted_class]35 36st.title("Speech Emotion Recognition Web App")37st.write("Upload a WAV or MP3 audio file to classify its emotion.")38 39uploaded_file = st.file_uploader("Choose an audio file", type=["wav", "mp3"])40 41if uploaded_file is not None:42 with open("temp.wav", "wb") as f:43 f.write(uploaded_file.read())44 st.audio("temp.wav")45 emotion = predict_emotion("temp.wav")46 st.success(f"Predicted Emotion: {emotion.capitalize()}")47 