Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,34 +4,28 @@ import pandas as pd
|
|
| 4 |
import plotly.express as px
|
| 5 |
|
| 6 |
# ------------------------------
|
| 7 |
-
# Load pretrained models
|
| 8 |
# ------------------------------
|
| 9 |
text_classifier = pipeline(
|
| 10 |
"text-classification",
|
| 11 |
-
model="j-hartmann/emotion-english-distilroberta-base",
|
| 12 |
-
return_all_scores
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
audio_classifier = pipeline(
|
| 16 |
"audio-classification",
|
| 17 |
-
model="superb/wav2vec2-base-superb-er"
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
# ------------------------------
|
| 21 |
# Map emotion to emoji
|
| 22 |
# ------------------------------
|
| 23 |
EMOJI_MAP = {
|
| 24 |
-
"anger": "π‘",
|
| 25 |
-
"
|
| 26 |
-
"
|
| 27 |
-
"joy": "π",
|
| 28 |
-
"neutral": "π",
|
| 29 |
-
"sadness": "π’",
|
| 30 |
-
"surprise": "π²",
|
| 31 |
-
"hap": "π", # for audio model
|
| 32 |
-
"neu": "π",
|
| 33 |
-
"sad": "π’",
|
| 34 |
-
"ang": "π‘"
|
| 35 |
}
|
| 36 |
|
| 37 |
# ------------------------------
|
|
@@ -82,44 +76,38 @@ def make_bar_chart(scores_dict, title="Emotion Scores"):
|
|
| 82 |
def predict(text, audio, w_text, w_audio):
|
| 83 |
text_preds, audio_preds = None, None
|
| 84 |
if text:
|
| 85 |
-
text_preds = text_classifier(text)
|
| 86 |
if audio:
|
| 87 |
audio_preds = audio_classifier(audio)
|
| 88 |
fused = fuse_predictions(text_preds, audio_preds, w_text, w_audio)
|
| 89 |
|
| 90 |
-
#
|
| 91 |
label = fused['fused_label']
|
| 92 |
emoji = EMOJI_MAP.get(label, "")
|
| 93 |
final_emotion = f"### Final Predicted Emotion: {label.upper()} {emoji} (score: {fused['fused_score']})"
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
if text_preds:
|
| 98 |
-
charts.append(make_bar_chart({p['label']: p['score'] for p in text_preds}, "Text Emotion Scores"))
|
| 99 |
-
if audio_preds:
|
| 100 |
-
charts.append(make_bar_chart({p['label']: p['score'] for p in audio_preds}, "Audio Emotion Scores"))
|
| 101 |
-
charts.append(make_bar_chart(fused['all_scores'], "Fused Emotion Scores"))
|
| 102 |
|
| 103 |
-
return final_emotion,
|
| 104 |
|
| 105 |
# ------------------------------
|
| 106 |
-
#
|
| 107 |
# ------------------------------
|
| 108 |
with gr.Blocks() as demo:
|
| 109 |
-
gr.Markdown("## π Multimodal Emotion
|
| 110 |
|
| 111 |
with gr.Row():
|
| 112 |
with gr.Column():
|
| 113 |
txt = gr.Textbox(label="Text input", placeholder="Type something emotional...")
|
| 114 |
aud = gr.Audio(type="filepath", label="Upload speech (wav/mp3)")
|
| 115 |
-
w1 = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Text weight
|
| 116 |
-
w2 = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Audio weight
|
| 117 |
btn = gr.Button("Predict")
|
| 118 |
with gr.Column():
|
| 119 |
-
final_label = gr.Markdown(
|
| 120 |
-
chart_output = gr.Plot(
|
| 121 |
|
| 122 |
-
# Button click triggers prediction
|
| 123 |
btn.click(fn=predict, inputs=[txt, aud, w1, w2], outputs=[final_label, chart_output])
|
| 124 |
|
| 125 |
-
demo.launch()
|
|
|
|
| 4 |
import plotly.express as px
|
| 5 |
|
| 6 |
# ------------------------------
|
| 7 |
+
# Load pretrained models (CPU-friendly, public)
|
| 8 |
# ------------------------------
|
| 9 |
text_classifier = pipeline(
|
| 10 |
"text-classification",
|
| 11 |
+
model="j-hartmann/emotion-english-distilroberta-base", # public
|
| 12 |
+
top_k=None, # replaces deprecated return_all_scores
|
| 13 |
+
device=-1
|
| 14 |
)
|
| 15 |
|
| 16 |
audio_classifier = pipeline(
|
| 17 |
"audio-classification",
|
| 18 |
+
model="superb/wav2vec2-base-superb-er", # public model
|
| 19 |
+
device=-1
|
| 20 |
)
|
| 21 |
|
| 22 |
# ------------------------------
|
| 23 |
# Map emotion to emoji
|
| 24 |
# ------------------------------
|
| 25 |
EMOJI_MAP = {
|
| 26 |
+
"anger": "π‘", "disgust": "π€’", "fear": "π¨", "joy": "π",
|
| 27 |
+
"neutral": "π", "sadness": "π’", "surprise": "π²",
|
| 28 |
+
"hap": "π", "neu": "π", "sad": "π’", "ang": "π‘"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
# ------------------------------
|
|
|
|
| 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
|
| 85 |
label = fused['fused_label']
|
| 86 |
emoji = EMOJI_MAP.get(label, "")
|
| 87 |
final_emotion = f"### Final Predicted Emotion: {label.upper()} {emoji} (score: {fused['fused_score']})"
|
| 88 |
|
| 89 |
+
# Only show fused chart to avoid Gradio multiple output issues
|
| 90 |
+
chart = make_bar_chart(fused['all_scores'], "Fused Emotion Scores")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
return final_emotion, chart
|
| 93 |
|
| 94 |
# ------------------------------
|
| 95 |
+
# Gradio Interface
|
| 96 |
# ------------------------------
|
| 97 |
with gr.Blocks() as demo:
|
| 98 |
+
gr.Markdown("## π Public CPU-Friendly Multimodal Emotion Classifier")
|
| 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 |
|
| 113 |
+
demo.launch()
|