-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
77 lines (57 loc) · 2 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import streamlit as st
import os
import time
from preprocessing import predict_emotion
from modelutils import load_model
import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np
# Set page configuration
st.set_page_config(page_title="Audio Emotion Detector", layout="wide")
# Load model
@st.cache_resource
def get_model():
return load_model()
model = get_model()
# Title and description
st.title("🎭 Audio Emotion Detector")
st.write("Upload an audio file to detect the emotion in the speech.")
# File uploader
uploaded_file = st.file_uploader("Choose an audio file", type=["wav", "mp3"])
if uploaded_file is not None:
# Create two columns
col1, col2 = st.columns(2)
with col1:
st.audio(uploaded_file, format='audio/wav')
with col2:
# Display a progress bar
progress_bar = st.progress(0)
status_text = st.empty()
for i in range(100):
# Update progress bar
progress_bar.progress(i + 1)
status_text.text(f"Processing: {i+1}%")
time.sleep(0.01)
# Save uploaded file temporarily
with open("temp_audio.wav", "wb") as f:
f.write(uploaded_file.getbuffer())
# Predict emotion
predicted_emotion = predict_emotion(os.path.join("temp_audio.wav"), model)
# Display result
st.success(f"Predicted Emotion: {predicted_emotion}")
# Display waveform and spectrogram
st.write("### Audio Visualization")
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 8))
y, sr = librosa.load(os.path.join("temp_audio.wav"))
# Waveform
librosa.display.waveshow(y, sr=sr, ax=ax1)
ax1.set_title('Waveform')
# Spectrogram
D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max)
librosa.display.specshow(D, sr=sr, x_axis='time', y_axis='hz', ax=ax2)
ax2.set_title('Spectrogram')
plt.tight_layout()
st.pyplot(fig)
# Remove temporary file
os.remove("temp_audio.wav")