Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,3 @@
|
|
| 1 |
-
# -*- coding: utf-8 -*-
|
| 2 |
-
"""app
|
| 3 |
-
|
| 4 |
-
Automatically generated by Colab.
|
| 5 |
-
|
| 6 |
-
Original file is located at
|
| 7 |
-
https://colab.research.google.com/drive/1GiJsUjgSfSzhuo0YkKYDvzQk5Cg2Qiao
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
import os
|
| 11 |
import pickle
|
| 12 |
import numpy as np
|
|
@@ -18,10 +9,8 @@ from faster_whisper import WhisperModel
|
|
| 18 |
from sentence_transformers import SentenceTransformer
|
| 19 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# -----------------------------
|
| 24 |
-
EMBED_MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
|
| 25 |
CENTROIDS_PATH = "emotion_avg.pkl"
|
| 26 |
|
| 27 |
with open(CENTROIDS_PATH, "rb") as f:
|
|
@@ -32,15 +21,11 @@ for k in list(emotion_avg.keys()):
|
|
| 32 |
|
| 33 |
EMOTIONS = list(emotion_avg.keys())
|
| 34 |
|
| 35 |
-
|
| 36 |
-
# Load models
|
| 37 |
-
# -----------------------------
|
| 38 |
embedder = SentenceTransformer(EMBED_MODEL_NAME)
|
| 39 |
whisper_model = WhisperModel("base", compute_type="int8")
|
| 40 |
|
| 41 |
-
|
| 42 |
-
# Prediction helper
|
| 43 |
-
# -----------------------------
|
| 44 |
def predict_emotion_sentence(sentence):
|
| 45 |
emb = embedder.encode([sentence], convert_to_numpy=True)[0]
|
| 46 |
labels = []
|
|
@@ -64,9 +49,7 @@ def predict_emotion_sentence(sentence):
|
|
| 64 |
"margin": float(sims[best_idx] - sims[second_idx])
|
| 65 |
}
|
| 66 |
|
| 67 |
-
|
| 68 |
-
# Main app function
|
| 69 |
-
# -----------------------------
|
| 70 |
def analyze_audio(audio_path):
|
| 71 |
if audio_path is None:
|
| 72 |
return "No transcript yet.", "None", 0.0, pd.DataFrame(columns=["sentence", "emotion", "score", "margin"])
|
|
@@ -103,11 +86,9 @@ def analyze_audio(audio_path):
|
|
| 103 |
df = pd.DataFrame(rows)
|
| 104 |
return transcript, latest_emotion, latest_margin, df
|
| 105 |
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
#
|
| 109 |
-
with gr.Blocks(title="Emotion Speech Analyzer") as demo:
|
| 110 |
-
gr.Markdown("# Emotion Speech Analyzer")
|
| 111 |
gr.Markdown("Upload or record audio, transcribe it, and detect sentence-level emotion.")
|
| 112 |
|
| 113 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import pickle
|
| 3 |
import numpy as np
|
|
|
|
| 9 |
from sentence_transformers import SentenceTransformer
|
| 10 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 11 |
|
| 12 |
+
|
| 13 |
+
EMBED_MODEL_NAME = "sentence-transformers/all-mpnet-base-v2"
|
|
|
|
|
|
|
| 14 |
CENTROIDS_PATH = "emotion_avg.pkl"
|
| 15 |
|
| 16 |
with open(CENTROIDS_PATH, "rb") as f:
|
|
|
|
| 21 |
|
| 22 |
EMOTIONS = list(emotion_avg.keys())
|
| 23 |
|
| 24 |
+
|
|
|
|
|
|
|
| 25 |
embedder = SentenceTransformer(EMBED_MODEL_NAME)
|
| 26 |
whisper_model = WhisperModel("base", compute_type="int8")
|
| 27 |
|
| 28 |
+
|
|
|
|
|
|
|
| 29 |
def predict_emotion_sentence(sentence):
|
| 30 |
emb = embedder.encode([sentence], convert_to_numpy=True)[0]
|
| 31 |
labels = []
|
|
|
|
| 49 |
"margin": float(sims[best_idx] - sims[second_idx])
|
| 50 |
}
|
| 51 |
|
| 52 |
+
|
|
|
|
|
|
|
| 53 |
def analyze_audio(audio_path):
|
| 54 |
if audio_path is None:
|
| 55 |
return "No transcript yet.", "None", 0.0, pd.DataFrame(columns=["sentence", "emotion", "score", "margin"])
|
|
|
|
| 86 |
df = pd.DataFrame(rows)
|
| 87 |
return transcript, latest_emotion, latest_margin, df
|
| 88 |
|
| 89 |
+
|
| 90 |
+
with gr.Blocks(title="Emotion Speech Classifier") as demo:
|
| 91 |
+
gr.Markdown("# Emotion Speech Classifier")
|
|
|
|
|
|
|
| 92 |
gr.Markdown("Upload or record audio, transcribe it, and detect sentence-level emotion.")
|
| 93 |
|
| 94 |
with gr.Row():
|