douglasgoodwin commited on
Commit
3f1044f
·
verified ·
1 Parent(s): 9ad695e

see ALL the emotions, even the ones with low scores

Browse files
Files changed (1) hide show
  1. app.py +14 -4
app.py CHANGED
@@ -4,11 +4,21 @@ from transformers import pipeline
4
  # Load the Hugging Face pipeline
5
  classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
6
 
 
 
 
7
  def classify_emotion(text):
8
  # Make predictions using the Hugging Face pipeline
9
  predictions = classifier(text)
10
- # Convert predictions to a dictionary for gr.Label output
11
- return {item["label"]: item["score"] for item in predictions}
 
 
 
 
 
 
 
12
 
13
  # Create a custom Gradio interface with title, description, and examples
14
  gr.Interface(
@@ -22,10 +32,10 @@ gr.Interface(
22
  title="CMACHINES | Emotion Detection with DistilBERT",
23
  description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter a piece of text to analyze its emotional content!",
24
  examples=[
 
25
  "I am so happy to see you!",
26
  "I'm really angry about what happened.",
27
  "The sunset was absolutely beautiful today.",
28
- "I'm worried about the upcoming exam.",
29
- "Fear is the mind-killer. I will face my fear."
30
  ]
31
  ).launch()
 
4
  # Load the Hugging Face pipeline
5
  classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
6
 
7
+ # Define the full list of possible emotions (based on the model output structure)
8
+ ALL_EMOTIONS = ["sadness", "joy", "love", "anger", "fear", "surprise"]
9
+
10
  def classify_emotion(text):
11
  # Make predictions using the Hugging Face pipeline
12
  predictions = classifier(text)
13
+
14
+ # Initialize a dictionary with all emotions and a default score of 0
15
+ emotion_scores = {emotion: 0.0 for emotion in ALL_EMOTIONS}
16
+
17
+ # Update the dictionary with the scores returned by the model
18
+ for item in predictions:
19
+ emotion_scores[item["label"]] = item["score"]
20
+
21
+ return emotion_scores
22
 
23
  # Create a custom Gradio interface with title, description, and examples
24
  gr.Interface(
 
32
  title="CMACHINES | Emotion Detection with DistilBERT",
33
  description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter a piece of text to analyze its emotional content!",
34
  examples=[
35
+ "I like you. I love you. Sometime I hate you too.",
36
  "I am so happy to see you!",
37
  "I'm really angry about what happened.",
38
  "The sunset was absolutely beautiful today.",
39
+ "I'm worried about the upcoming exam."
 
40
  ]
41
  ).launch()