Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,28 +4,35 @@ import pandas as pd
|
|
| 4 |
import plotly.express as px
|
| 5 |
|
| 6 |
# ------------------------------
|
| 7 |
-
# Load pretrained models (CPU-friendly
|
| 8 |
# ------------------------------
|
| 9 |
text_classifier = pipeline(
|
| 10 |
"text-classification",
|
| 11 |
-
model="j-hartmann/emotion-english-distilroberta-base",
|
| 12 |
-
top_k=None
|
| 13 |
-
device=-1
|
| 14 |
)
|
| 15 |
|
|
|
|
| 16 |
audio_classifier = pipeline(
|
| 17 |
"audio-classification",
|
| 18 |
-
model="superb/wav2vec2-
|
| 19 |
-
device=-1
|
| 20 |
)
|
| 21 |
|
| 22 |
# ------------------------------
|
| 23 |
# Map emotion to emoji
|
| 24 |
# ------------------------------
|
| 25 |
EMOJI_MAP = {
|
| 26 |
-
"anger": "π‘",
|
| 27 |
-
"
|
| 28 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
# ------------------------------
|
|
@@ -40,8 +47,8 @@ def fuse_predictions(text_preds=None, audio_preds=None, w_text=0.5, w_audio=0.5)
|
|
| 40 |
scores = {l: 0.0 for l in labels}
|
| 41 |
|
| 42 |
def normalize(preds):
|
| 43 |
-
|
| 44 |
-
return {p['label']: p['score']/
|
| 45 |
|
| 46 |
if text_preds:
|
| 47 |
t_norm = normalize(text_preds)
|
|
@@ -56,7 +63,7 @@ def fuse_predictions(text_preds=None, audio_preds=None, w_text=0.5, w_audio=0.5)
|
|
| 56 |
return {"fused_label": best[0], "fused_score": round(best[1], 3), "all_scores": scores}
|
| 57 |
|
| 58 |
# ------------------------------
|
| 59 |
-
#
|
| 60 |
# ------------------------------
|
| 61 |
def make_bar_chart(scores_dict, title="Emotion Scores"):
|
| 62 |
df = pd.DataFrame({
|
|
@@ -76,9 +83,10 @@ def make_bar_chart(scores_dict, title="Emotion Scores"):
|
|
| 76 |
def predict(text, audio, w_text, w_audio):
|
| 77 |
text_preds, audio_preds = None, None
|
| 78 |
if text:
|
| 79 |
-
text_preds = text_classifier(text)
|
| 80 |
if audio:
|
| 81 |
-
audio_preds = audio_classifier(audio)
|
|
|
|
| 82 |
fused = fuse_predictions(text_preds, audio_preds, w_text, w_audio)
|
| 83 |
|
| 84 |
# Final emotion with emoji
|
|
@@ -86,27 +94,26 @@ def predict(text, audio, w_text, w_audio):
|
|
| 86 |
emoji = EMOJI_MAP.get(label, "")
|
| 87 |
final_emotion = f"### Final Predicted Emotion: {label.upper()} {emoji} (score: {fused['fused_score']})"
|
| 88 |
|
| 89 |
-
#
|
| 90 |
chart = make_bar_chart(fused['all_scores'], "Fused Emotion Scores")
|
| 91 |
-
|
| 92 |
return final_emotion, chart
|
| 93 |
|
| 94 |
# ------------------------------
|
| 95 |
-
# Gradio
|
| 96 |
# ------------------------------
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
-
gr.Markdown("## π
|
| 99 |
|
| 100 |
with gr.Row():
|
| 101 |
with gr.Column():
|
| 102 |
txt = gr.Textbox(label="Text input", placeholder="Type something emotional...")
|
| 103 |
aud = gr.Audio(type="filepath", label="Upload speech (wav/mp3)")
|
| 104 |
-
w1 = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Text weight")
|
| 105 |
-
w2 = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Audio weight")
|
| 106 |
btn = gr.Button("Predict")
|
| 107 |
with gr.Column():
|
| 108 |
-
final_label = gr.Markdown()
|
| 109 |
-
chart_output = gr.Plot()
|
| 110 |
|
| 111 |
btn.click(fn=predict, inputs=[txt, aud, w1, w2], outputs=[final_label, chart_output])
|
| 112 |
|
|
|
|
| 4 |
import plotly.express as px
|
| 5 |
|
| 6 |
# ------------------------------
|
| 7 |
+
# Load pretrained models (CPU-friendly)
|
| 8 |
# ------------------------------
|
| 9 |
text_classifier = pipeline(
|
| 10 |
"text-classification",
|
| 11 |
+
model="j-hartmann/emotion-english-distilroberta-base",
|
| 12 |
+
top_k=None # returns all scores
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
+
# Use a small, public audio model
|
| 16 |
audio_classifier = pipeline(
|
| 17 |
"audio-classification",
|
| 18 |
+
model="superb/wav2vec2-small-superb-er" # small model
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
# ------------------------------
|
| 22 |
# Map emotion to emoji
|
| 23 |
# ------------------------------
|
| 24 |
EMOJI_MAP = {
|
| 25 |
+
"anger": "π‘",
|
| 26 |
+
"disgust": "π€’",
|
| 27 |
+
"fear": "π¨",
|
| 28 |
+
"joy": "π",
|
| 29 |
+
"neutral": "π",
|
| 30 |
+
"sadness": "π’",
|
| 31 |
+
"surprise": "π²",
|
| 32 |
+
"hap": "π", # audio model labels
|
| 33 |
+
"neu": "π",
|
| 34 |
+
"sad": "π’",
|
| 35 |
+
"ang": "π‘"
|
| 36 |
}
|
| 37 |
|
| 38 |
# ------------------------------
|
|
|
|
| 47 |
scores = {l: 0.0 for l in labels}
|
| 48 |
|
| 49 |
def normalize(preds):
|
| 50 |
+
total = sum(p['score'] for p in preds)
|
| 51 |
+
return {p['label']: p['score']/total for p in preds}
|
| 52 |
|
| 53 |
if text_preds:
|
| 54 |
t_norm = normalize(text_preds)
|
|
|
|
| 63 |
return {"fused_label": best[0], "fused_score": round(best[1], 3), "all_scores": scores}
|
| 64 |
|
| 65 |
# ------------------------------
|
| 66 |
+
# Bar chart function
|
| 67 |
# ------------------------------
|
| 68 |
def make_bar_chart(scores_dict, title="Emotion Scores"):
|
| 69 |
df = pd.DataFrame({
|
|
|
|
| 83 |
def predict(text, audio, w_text, w_audio):
|
| 84 |
text_preds, audio_preds = None, None
|
| 85 |
if text:
|
| 86 |
+
text_preds = text_classifier(text) # list of dicts
|
| 87 |
if audio:
|
| 88 |
+
audio_preds = audio_classifier(audio) # list of dicts
|
| 89 |
+
|
| 90 |
fused = fuse_predictions(text_preds, audio_preds, w_text, w_audio)
|
| 91 |
|
| 92 |
# Final emotion with emoji
|
|
|
|
| 94 |
emoji = EMOJI_MAP.get(label, "")
|
| 95 |
final_emotion = f"### Final Predicted Emotion: {label.upper()} {emoji} (score: {fused['fused_score']})"
|
| 96 |
|
| 97 |
+
# Fused bar chart
|
| 98 |
chart = make_bar_chart(fused['all_scores'], "Fused Emotion Scores")
|
|
|
|
| 99 |
return final_emotion, chart
|
| 100 |
|
| 101 |
# ------------------------------
|
| 102 |
+
# Build Gradio interface
|
| 103 |
# ------------------------------
|
| 104 |
with gr.Blocks() as demo:
|
| 105 |
+
gr.Markdown("## π Multimodal Emotion Classification (Text + Speech)")
|
| 106 |
|
| 107 |
with gr.Row():
|
| 108 |
with gr.Column():
|
| 109 |
txt = gr.Textbox(label="Text input", placeholder="Type something emotional...")
|
| 110 |
aud = gr.Audio(type="filepath", label="Upload speech (wav/mp3)")
|
| 111 |
+
w1 = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Text weight (w_text)")
|
| 112 |
+
w2 = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Audio weight (w_audio)")
|
| 113 |
btn = gr.Button("Predict")
|
| 114 |
with gr.Column():
|
| 115 |
+
final_label = gr.Markdown(label="Predicted Emotion")
|
| 116 |
+
chart_output = gr.Plot(label="Fused Emotion Scores")
|
| 117 |
|
| 118 |
btn.click(fn=predict, inputs=[txt, aud, w1, w2], outputs=[final_label, chart_output])
|
| 119 |
|