douglasgoodwin commited on
Commit
b19ac64
·
verified ·
1 Parent(s): d5a0bd7

bar plot percentages and a text table

Browse files
Files changed (1) hide show
  1. app.py +26 -5
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
3
 
4
  # Load the Hugging Face pipeline
5
  classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
@@ -14,11 +15,20 @@ def classify_emotion(text):
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 raw 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(
@@ -28,9 +38,20 @@ gr.Interface(
28
  label="Input Text",
29
  lines=4
30
  ),
31
- outputs=gr.Label(label="Emotion Raw Scores"), # Update label to reflect raw scores
32
- title="CMACHINES | motion Detection with DistilBERT (Raw Scores)",
33
- description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter a piece of text to analyze its emotional content with raw scores!",
 
 
 
 
 
 
 
 
 
 
 
34
  examples=[
35
  "I like you. I love you. Sometimes I hate you.",
36
  "I'm really angry about what happened.",
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import pandas as pd
4
 
5
  # Load the Hugging Face pipeline
6
  classifier = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
 
15
  # Initialize a dictionary with all emotions and a default score of 0
16
  emotion_scores = {emotion: 0.0 for emotion in ALL_EMOTIONS}
17
 
18
+ # Update the dictionary with the scores returned by the model
19
  for item in predictions:
20
  emotion_scores[item["label"]] = item["score"]
21
 
22
+ # Create a dataframe for the bar plot
23
+ df = pd.DataFrame.from_dict(emotion_scores, orient="index").reset_index()
24
+ df.columns = ["Emotion", "Score"]
25
+
26
+ # Prepare a text-based table for display
27
+ table = "Emotion Scores:\n"
28
+ table += "----------------\n"
29
+ table += "\n".join([f"{emotion}: {score:.4f}" for emotion, score in emotion_scores.items()])
30
+
31
+ return df, table
32
 
33
  # Create a custom Gradio interface with title, description, and examples
34
  gr.Interface(
 
38
  label="Input Text",
39
  lines=4
40
  ),
41
+ outputs=[
42
+ gr.BarPlot(
43
+ x="Emotion",
44
+ y="Score",
45
+ label="Emotion Scores Bar Plot",
46
+ title="Emotion Probabilities",
47
+ color="#2563eb", # Color for the bars
48
+ height=400,
49
+ vertical=True
50
+ ),
51
+ gr.Textbox(label="Emotion Scores Table") # Text-based table output
52
+ ],
53
+ title="CMACHINES25 | Emotion Detection with DistilBERT",
54
+ description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter a piece of text to analyze its emotional content! Both a bar plot and a text table of the scores will be displayed.",
55
  examples=[
56
  "I like you. I love you. Sometimes I hate you.",
57
  "I'm really angry about what happened.",