Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
import numpy as np
|
| 4 |
import librosa
|
| 5 |
-
|
| 6 |
from transformers import AutoFeatureExtractor
|
| 7 |
-
|
| 8 |
from model import MMSForMultilingualSER
|
| 9 |
|
| 10 |
MODEL_ID = "E-motionAssistant/mms-300m-multilingual-ser"
|
| 11 |
|
| 12 |
-
# Load feature extractor + model
|
| 13 |
-
feature_extractor = AutoFeatureExtractor.from_pretrained(MODEL_ID)
|
| 14 |
-
|
| 15 |
-
emotion_model = MMSForMultilingualSER.from_pretrained(
|
| 16 |
-
MODEL_ID,
|
| 17 |
-
ignore_mismatched_sizes=True
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
emotion_model.eval()
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Emotion labels (adjust if different)
|
| 24 |
emotion_labels = [
|
| 25 |
"neutral",
|
| 26 |
"happy",
|
|
@@ -29,10 +14,24 @@ emotion_labels = [
|
|
| 29 |
"fear"
|
| 30 |
]
|
| 31 |
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
inputs = feature_extractor(
|
| 38 |
speech,
|
|
@@ -52,20 +51,17 @@ with gr.Blocks() as demo:
|
|
| 52 |
|
| 53 |
gr.Markdown("# Emotion Regulation Assistant")
|
| 54 |
|
| 55 |
-
gr.
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
title="Emotion Detection"
|
| 62 |
-
)
|
| 63 |
-
],
|
| 64 |
-
|
| 65 |
-
[
|
| 66 |
-
"Emotion Detection"
|
| 67 |
-
]
|
| 68 |
-
)
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
| 3 |
import librosa
|
|
|
|
| 4 |
from transformers import AutoFeatureExtractor
|
|
|
|
| 5 |
from model import MMSForMultilingualSER
|
| 6 |
|
| 7 |
MODEL_ID = "E-motionAssistant/mms-300m-multilingual-ser"
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
emotion_labels = [
|
| 10 |
"neutral",
|
| 11 |
"happy",
|
|
|
|
| 14 |
"fear"
|
| 15 |
]
|
| 16 |
|
| 17 |
+
device = "cpu"
|
| 18 |
|
| 19 |
+
print("Loading model...")
|
| 20 |
|
| 21 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(MODEL_ID)
|
| 22 |
+
|
| 23 |
+
emotion_model = MMSForMultilingualSER.from_pretrained(
|
| 24 |
+
MODEL_ID
|
| 25 |
+
).to(device)
|
| 26 |
+
|
| 27 |
+
emotion_model.eval()
|
| 28 |
+
|
| 29 |
+
print("Model loaded")
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def detect_emotion(audio):
|
| 33 |
+
|
| 34 |
+
speech, sr = librosa.load(audio, sr=16000)
|
| 35 |
|
| 36 |
inputs = feature_extractor(
|
| 37 |
speech,
|
|
|
|
| 51 |
|
| 52 |
gr.Markdown("# Emotion Regulation Assistant")
|
| 53 |
|
| 54 |
+
with gr.Tab("Emotion Detection"):
|
| 55 |
+
|
| 56 |
+
audio_input = gr.Audio(type="filepath")
|
| 57 |
+
output = gr.Textbox(label="Detected Emotion")
|
| 58 |
+
|
| 59 |
+
btn = gr.Button("Detect Emotion")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
btn.click(
|
| 62 |
+
fn=detect_emotion,
|
| 63 |
+
inputs=audio_input,
|
| 64 |
+
outputs=output
|
| 65 |
+
)
|
| 66 |
|
| 67 |
demo.launch()
|