File size: 3,695 Bytes
9258fc6
 
 
 
0f86732
9258fc6
 
 
 
0f86732
 
9258fc6
 
 
 
0f86732
 
9258fc6
 
 
 
 
 
 
 
 
 
 
 
 
0f86732
9258fc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f86732
 
9258fc6
0f86732
9258fc6
0f86732
 
9258fc6
0f86732
9258fc6
 
 
 
 
0f86732
9258fc6
 
 
 
 
 
 
 
 
 
0f86732
9258fc6
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import gradio as gr
from transformers import pipeline

# ------------------------------
# Load pretrained models (CPU)
# ------------------------------
text_classifier = pipeline(
    "text-classification",
    model="j-hartmann/emotion-english-distilroberta-base",
    return_all_scores=True,
    device=-1  # CPU
)

audio_classifier = pipeline(
    "audio-classification",
    model="m3hrdadfi/wav2vec2-mini-xlsr-emotion",
    device=-1  # CPU
)

# ------------------------------
# Map emotion to emoji
# ------------------------------
EMOJI_MAP = {
    "anger": "😑",
    "disgust": "🀒",
    "fear": "😨",
    "joy": "πŸ˜„",
    "neutral": "😐",
    "sadness": "😒",
    "surprise": "😲",
    "hap": "πŸ˜„",
    "neu": "😐",
    "sad": "😒",
    "ang": "😑"
}

# ------------------------------
# Fusion function
# ------------------------------
def fuse_predictions(text_preds=None, audio_preds=None, w_text=0.5, w_audio=0.5):
    labels = set()
    if text_preds:
        labels |= {p['label'] for p in text_preds}
    if audio_preds:
        labels |= {p['label'] for p in audio_preds}
    scores = {l: 0.0 for l in labels}

    def normalize(preds):
        s = sum(p['score'] for p in preds)
        return {p['label']: p['score']/s for p in preds}

    if text_preds:
        t_norm = normalize(text_preds)
        for l in labels:
            scores[l] += w_text * t_norm.get(l, 0)
    if audio_preds:
        a_norm = normalize(audio_preds)
        for l in labels:
            scores[l] += w_audio * a_norm.get(l, 0)

    best = max(scores.items(), key=lambda x: x[1]) if scores else ("none", 0)
    return {"fused_label": best[0], "fused_score": round(best[1], 3), "all_scores": scores}

# ------------------------------
# Prediction function
# ------------------------------
def predict(text, audio, w_text, w_audio):
    text_preds, audio_preds = None, None
    if text:
        text_preds = text_classifier(text)[0]
    if audio:
        audio_preds = audio_classifier(audio)
    fused = fuse_predictions(text_preds, audio_preds, w_text, w_audio)

    # Display final predicted emotion with emoji
    label = fused['fused_label']
    emoji = EMOJI_MAP.get(label, "")
    final_emotion = f"### Final Predicted Emotion: {label.upper()} {emoji} (score: {fused['fused_score']})"

    # Simple text-based bar chart
    chart_text = "\n"
    if text_preds:
        chart_text += "Text scores:\n" + "\n".join([f"{p['label']}: {p['score']:.2f}" for p in text_preds]) + "\n"
    if audio_preds:
        chart_text += "Audio scores:\n" + "\n".join([f"{p['label']}: {p['score']:.2f}" for p in audio_preds]) + "\n"
    chart_text += "Fused scores:\n" + "\n".join([f"{k}: {v:.2f}" for k,v in fused['all_scores'].items()])

    return final_emotion, chart_text

# ------------------------------
# Build Gradio interface
# ------------------------------
with gr.Blocks() as demo:
    gr.Markdown("## 🎭 CPU-Friendly Multimodal Emotion Classification")

    with gr.Row():
        with gr.Column():
            txt = gr.Textbox(label="Text input", placeholder="Type something emotional...")
            aud = gr.Audio(type="filepath", label="Upload speech (wav/mp3)")
            w1 = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Text weight (w_text)")
            w2 = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Audio weight (w_audio)")
            btn = gr.Button("Predict")
        with gr.Column():
            final_label = gr.Markdown(label="Predicted Emotion")
            chart_output = gr.Textbox(label="Emotion Scores", interactive=False)

    btn.click(fn=predict, inputs=[txt, aud, w1, w2], outputs=[final_label, chart_output])

demo.launch()