sachin7777777 commited on
Commit
d921763
·
verified ·
1 Parent(s): 0f86732

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -1,5 +1,7 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
 
3
 
4
  # ------------------------------
5
  # Load pretrained models (CPU)
@@ -13,7 +15,7 @@ text_classifier = pipeline(
13
 
14
  audio_classifier = pipeline(
15
  "audio-classification",
16
- model="m3hrdadfi/wav2vec2-mini-xlsr-emotion",
17
  device=-1 # CPU
18
  )
19
 
@@ -61,6 +63,21 @@ 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
  # Prediction function
66
  # ------------------------------
@@ -77,15 +94,15 @@ def predict(text, audio, w_text, w_audio):
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
@@ -102,7 +119,7 @@ with gr.Blocks() as demo:
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
 
 
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 (CPU)
 
15
 
16
  audio_classifier = pipeline(
17
  "audio-classification",
18
+ model="mrm8488/wav2vec2-small-xlsr-53-english-emotion",
19
  device=-1 # CPU
20
  )
21
 
 
63
  best = max(scores.items(), key=lambda x: x[1]) if scores else ("none", 0)
64
  return {"fused_label": best[0], "fused_score": round(best[1], 3), "all_scores": scores}
65
 
66
+ # ------------------------------
67
+ # Create bar chart
68
+ # ------------------------------
69
+ def make_bar_chart(scores_dict, title="Emotion Scores"):
70
+ df = pd.DataFrame({
71
+ "Emotion": list(scores_dict.keys()),
72
+ "Score": list(scores_dict.values())
73
+ })
74
+ fig = px.bar(df, x="Emotion", y="Score", text="Score",
75
+ title=title, range_y=[0,1],
76
+ color="Emotion", color_discrete_sequence=px.colors.qualitative.Bold)
77
+ fig.update_traces(texttemplate='%{text:.2f}', textposition='outside')
78
+ fig.update_layout(yaxis_title="Probability", xaxis_title="Emotion", showlegend=False)
79
+ return fig
80
+
81
  # ------------------------------
82
  # Prediction function
83
  # ------------------------------
 
94
  emoji = EMOJI_MAP.get(label, "")
95
  final_emotion = f"### Final Predicted Emotion: {label.upper()} {emoji} (score: {fused['fused_score']})"
96
 
97
+ # Bar charts
98
+ charts = []
99
  if text_preds:
100
+ charts.append(make_bar_chart({p['label']: p['score'] for p in text_preds}, "Text Emotion Scores"))
101
  if audio_preds:
102
+ charts.append(make_bar_chart({p['label']: p['score'] for p in audio_preds}, "Audio Emotion Scores"))
103
+ charts.append(make_bar_chart(fused['all_scores'], "Fused Emotion Scores"))
104
 
105
+ return final_emotion, charts
106
 
107
  # ------------------------------
108
  # Build Gradio interface
 
119
  btn = gr.Button("Predict")
120
  with gr.Column():
121
  final_label = gr.Markdown(label="Predicted Emotion")
122
+ chart_output = gr.Plot(label="Emotion Scores")
123
 
124
  btn.click(fn=predict, inputs=[txt, aud, w1, w2], outputs=[final_label, chart_output])
125