sachin7777777 commited on
Commit
0f86732
Β·
verified Β·
1 Parent(s): 619a408

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -30
app.py CHANGED
@@ -1,20 +1,20 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- 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=True
 
13
  )
14
 
15
  audio_classifier = pipeline(
16
  "audio-classification",
17
- model="superb/wav2vec2-base-superb-er"
 
18
  )
19
 
20
  # ------------------------------
@@ -28,7 +28,7 @@ EMOJI_MAP = {
28
  "neutral": "😐",
29
  "sadness": "😒",
30
  "surprise": "😲",
31
- "hap": "πŸ˜„", # for audio model
32
  "neu": "😐",
33
  "sad": "😒",
34
  "ang": "😑"
@@ -61,21 +61,6 @@ def fuse_predictions(text_preds=None, audio_preds=None, w_text=0.5, w_audio=0.5)
61
  best = max(scores.items(), key=lambda x: x[1]) if scores else ("none", 0)
62
  return {"fused_label": best[0], "fused_score": round(best[1], 3), "all_scores": scores}
63
 
64
- # ------------------------------
65
- # Create bar chart
66
- # ------------------------------
67
- def make_bar_chart(scores_dict, title="Emotion Scores"):
68
- df = pd.DataFrame({
69
- "Emotion": list(scores_dict.keys()),
70
- "Score": list(scores_dict.values())
71
- })
72
- fig = px.bar(df, x="Emotion", y="Score", text="Score",
73
- title=title, range_y=[0,1],
74
- color="Emotion", color_discrete_sequence=px.colors.qualitative.Bold)
75
- fig.update_traces(texttemplate='%{text:.2f}', textposition='outside')
76
- fig.update_layout(yaxis_title="Probability", xaxis_title="Emotion", showlegend=False)
77
- return fig
78
-
79
  # ------------------------------
80
  # Prediction function
81
  # ------------------------------
@@ -92,21 +77,21 @@ def predict(text, audio, w_text, w_audio):
92
  emoji = EMOJI_MAP.get(label, "")
93
  final_emotion = f"### Final Predicted Emotion: {label.upper()} {emoji} (score: {fused['fused_score']})"
94
 
95
- # Bar charts
96
- charts = []
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, charts
104
 
105
  # ------------------------------
106
  # Build Gradio interface
107
  # ------------------------------
108
  with gr.Blocks() as demo:
109
- gr.Markdown("## 🎭 Multimodal Emotion Classification (Text + Speech)")
110
 
111
  with gr.Row():
112
  with gr.Column():
@@ -117,9 +102,8 @@ with gr.Blocks() as demo:
117
  btn = gr.Button("Predict")
118
  with gr.Column():
119
  final_label = gr.Markdown(label="Predicted Emotion")
120
- chart_output = gr.Plot(label="Emotion Scores")
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()
 
1
  import gradio as gr
2
  from transformers import pipeline
 
 
3
 
4
  # ------------------------------
5
+ # Load pretrained models (CPU)
6
  # ------------------------------
7
  text_classifier = pipeline(
8
  "text-classification",
9
  model="j-hartmann/emotion-english-distilroberta-base",
10
+ return_all_scores=True,
11
+ device=-1 # CPU
12
  )
13
 
14
  audio_classifier = pipeline(
15
  "audio-classification",
16
+ model="m3hrdadfi/wav2vec2-mini-xlsr-emotion",
17
+ device=-1 # CPU
18
  )
19
 
20
  # ------------------------------
 
28
  "neutral": "😐",
29
  "sadness": "😒",
30
  "surprise": "😲",
31
+ "hap": "πŸ˜„",
32
  "neu": "😐",
33
  "sad": "😒",
34
  "ang": "😑"
 
61
  best = max(scores.items(), key=lambda x: x[1]) if scores else ("none", 0)
62
  return {"fused_label": best[0], "fused_score": round(best[1], 3), "all_scores": scores}
63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  # ------------------------------
65
  # Prediction function
66
  # ------------------------------
 
77
  emoji = EMOJI_MAP.get(label, "")
78
  final_emotion = f"### Final Predicted Emotion: {label.upper()} {emoji} (score: {fused['fused_score']})"
79
 
80
+ # Simple text-based bar chart
81
+ chart_text = "\n"
82
  if text_preds:
83
+ chart_text += "Text scores:\n" + "\n".join([f"{p['label']}: {p['score']:.2f}" for p in text_preds]) + "\n"
84
  if audio_preds:
85
+ chart_text += "Audio scores:\n" + "\n".join([f"{p['label']}: {p['score']:.2f}" for p in audio_preds]) + "\n"
86
+ chart_text += "Fused scores:\n" + "\n".join([f"{k}: {v:.2f}" for k,v in fused['all_scores'].items()])
87
 
88
+ return final_emotion, chart_text
89
 
90
  # ------------------------------
91
  # Build Gradio interface
92
  # ------------------------------
93
  with gr.Blocks() as demo:
94
+ gr.Markdown("## 🎭 CPU-Friendly Multimodal Emotion Classification")
95
 
96
  with gr.Row():
97
  with gr.Column():
 
102
  btn = gr.Button("Predict")
103
  with gr.Column():
104
  final_label = gr.Markdown(label="Predicted Emotion")
105
+ chart_output = gr.Textbox(label="Emotion Scores", interactive=False)
106
 
 
107
  btn.click(fn=predict, inputs=[txt, aud, w1, w2], outputs=[final_label, chart_output])
108
 
109
  demo.launch()