Spaces:
Sleeping
Sleeping
Update the interface completely
Browse files
app.py
CHANGED
|
@@ -1,7 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# AUTOGENERATED! DO NOT EDIT!
|
| 2 |
+
|
| 3 |
+
# %% auto 0
|
| 4 |
+
__all__ = ['learn', 'categories', 'audio', 'label', 'inf', 'extract_emotion', 'get_y', 'classify_audio']
|
| 5 |
+
|
| 6 |
+
from fastai.vision.all import *
|
| 7 |
import gradio as gr
|
| 8 |
+
import matplotlib.pyplot as plt
|
| 9 |
+
import librosa
|
| 10 |
+
import librosa.display
|
| 11 |
+
from pathlib import Path
|
| 12 |
+
import os
|
| 13 |
+
|
| 14 |
+
def extract_emotion(file_name: str) -> str:
|
| 15 |
+
"""
|
| 16 |
+
Given the name of the file, return the label
|
| 17 |
+
indicating the emotion associated with the audio.
|
| 18 |
+
"""
|
| 19 |
+
# Split the filename at each underscore
|
| 20 |
+
parts = file_name.split('_')
|
| 21 |
+
# Label is after second
|
| 22 |
+
label_with_extension = parts[-1]
|
| 23 |
+
# Remove the extension to get only the label
|
| 24 |
+
label = label_with_extension[:-4]
|
| 25 |
+
return label
|
| 26 |
+
|
| 27 |
+
def get_y(filepath): return extract_emotion(str(filepath).split("/")[-1])
|
| 28 |
+
|
| 29 |
+
# Load Learner
|
| 30 |
+
learn = load_learner("emotion_model.pkl")
|
| 31 |
+
categories = learn.dls.vocab
|
| 32 |
+
|
| 33 |
+
def classify_audio(audio_file):
|
| 34 |
+
"""
|
| 35 |
+
Takes the audio file and returns its
|
| 36 |
+
prediction of emotions along with probabilities.
|
| 37 |
+
"""
|
| 38 |
+
# Load the audio file
|
| 39 |
+
sample, sample_rate = librosa.load(audio_file, sr=None, duration=20)
|
| 40 |
+
# Create spectogram
|
| 41 |
+
S = librosa.feature.melspectrogram(y=sample, sr=sample_rate)
|
| 42 |
+
S_DB = librosa.power_to_db(S, ref=np.max)
|
| 43 |
+
# Prepare the figure for saving the spectrogram
|
| 44 |
+
fig, ax = plt.subplots()
|
| 45 |
+
fig.tight_layout(pad=0)
|
| 46 |
+
# Create the spectogram image
|
| 47 |
+
img = librosa.display.specshow(S_DB, sr=sample_rate, x_axis='time',
|
| 48 |
+
y_axis='mel', ax=ax)
|
| 49 |
+
# Turn off the axis for saving
|
| 50 |
+
plt.axis('off')
|
| 51 |
+
# Save the spectogram temporarily
|
| 52 |
+
temp_img_path = Path("temp_spectogram.png")
|
| 53 |
+
plt.savefig(temp_img_path)
|
| 54 |
+
|
| 55 |
+
pred,idx, probs = learn.predict(temp_img_path)
|
| 56 |
+
|
| 57 |
+
# Remove the temporary spectogram image
|
| 58 |
+
os.remove(temp_img_path)
|
| 59 |
|
| 60 |
+
return dict(zip(categories, map(float, probs)))
|
|
|
|
| 61 |
|
| 62 |
+
audio = gr.Audio(type="filepath", label="Upload Audio <=20 seconds")
|
| 63 |
+
label = gr.Label()
|
| 64 |
+
# Gradio Interface
|
| 65 |
+
inf = gr.Interface(fn=classify_audio, inputs=audio, outputs=label)
|
| 66 |
+
inf.launch()
|