S-4-G-4-R commited on
Commit
17db978
·
verified ·
1 Parent(s): b154803

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -6
app.py CHANGED
@@ -7,9 +7,19 @@ import plotly.graph_objects as go
7
  model_id = "S-4-G-4-R/distilbert-base-uncased-finetuned-emotion"
8
  classifier = pipeline("text-classification", model=model_id)
9
 
10
- # Define emotion labels
11
  EMOTION_LABELS = ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise']
12
 
 
 
 
 
 
 
 
 
 
 
13
  # Emoji mapping for emotions
14
  EMOTION_EMOJIS = {
15
  'sadness': '😢',
@@ -30,12 +40,13 @@ def classify_emotion(text):
30
  # Get predictions
31
  preds = classifier(text, return_all_scores=True)[0]
32
 
33
- # Create DataFrame
34
  df = pd.DataFrame(preds)
 
35
  df['score'] = df['score'] * 100 # Convert to percentage
36
 
37
  # Add emojis to labels
38
- df['display_label'] = df['label'].map(lambda x: f"{EMOTION_EMOJIS.get(x, '')} {x.capitalize()}")
39
 
40
  # Sort by score for better visualization
41
  df = df.sort_values('score', ascending=True)
@@ -79,14 +90,14 @@ def classify_emotion(text):
79
  sorted_df = df.sort_values('score', ascending=False)
80
 
81
  top_emotion = sorted_df.iloc[0]
82
- results_text += f"**Top Emotion:** {EMOTION_EMOJIS.get(top_emotion['label'], '')} **{top_emotion['label'].capitalize()}** ({top_emotion['score']:.2f}%)\n\n"
83
  results_text += "---\n\n**All Emotions:**\n\n"
84
 
85
  for _, row in sorted_df.iterrows():
86
- emoji = EMOTION_EMOJIS.get(row['label'], '')
87
  bar_length = int(row['score'] / 5)
88
  bar = '█' * bar_length
89
- results_text += f"{emoji} **{row['label'].capitalize()}**: {row['score']:.2f}% {bar}\n\n"
90
 
91
  return fig, results_text
92
 
 
7
  model_id = "S-4-G-4-R/distilbert-base-uncased-finetuned-emotion"
8
  classifier = pipeline("text-classification", model=model_id)
9
 
10
+ # Define emotion labels mapping (LABEL_0 to LABEL_5)
11
  EMOTION_LABELS = ['sadness', 'joy', 'love', 'anger', 'fear', 'surprise']
12
 
13
+ # Label mapping from model output to emotion names
14
+ LABEL_MAPPING = {
15
+ 'LABEL_0': 'sadness',
16
+ 'LABEL_1': 'joy',
17
+ 'LABEL_2': 'love',
18
+ 'LABEL_3': 'anger',
19
+ 'LABEL_4': 'fear',
20
+ 'LABEL_5': 'surprise'
21
+ }
22
+
23
  # Emoji mapping for emotions
24
  EMOTION_EMOJIS = {
25
  'sadness': '😢',
 
40
  # Get predictions
41
  preds = classifier(text, return_all_scores=True)[0]
42
 
43
+ # Create DataFrame and map labels to emotion names
44
  df = pd.DataFrame(preds)
45
+ df['emotion'] = df['label'].map(LABEL_MAPPING)
46
  df['score'] = df['score'] * 100 # Convert to percentage
47
 
48
  # Add emojis to labels
49
+ df['display_label'] = df['emotion'].map(lambda x: f"{EMOTION_EMOJIS.get(x, '')} {x.capitalize()}")
50
 
51
  # Sort by score for better visualization
52
  df = df.sort_values('score', ascending=True)
 
90
  sorted_df = df.sort_values('score', ascending=False)
91
 
92
  top_emotion = sorted_df.iloc[0]
93
+ results_text += f"**Top Emotion:** {EMOTION_EMOJIS.get(top_emotion['emotion'], '')} **{top_emotion['emotion'].capitalize()}** ({top_emotion['score']:.2f}%)\n\n"
94
  results_text += "---\n\n**All Emotions:**\n\n"
95
 
96
  for _, row in sorted_df.iterrows():
97
+ emoji = EMOTION_EMOJIS.get(row['emotion'], '')
98
  bar_length = int(row['score'] / 5)
99
  bar = '█' * bar_length
100
+ results_text += f"{emoji} **{row['emotion'].capitalize()}**: {row['score']:.2f}% {bar}\n\n"
101
 
102
  return fig, results_text
103