go back to a working
Browse files
app.py
CHANGED
@@ -1,21 +1,13 @@
|
|
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(
|
7 |
-
"text-classification",
|
8 |
-
model="bhadresh-savani/distilbert-base-uncased-emotion"
|
9 |
-
)
|
10 |
|
11 |
# Define the full list of possible emotions (based on the model output structure)
|
12 |
ALL_EMOTIONS = ["sadness", "joy", "love", "anger", "fear", "surprise"]
|
13 |
|
14 |
def classify_emotion(text):
|
15 |
-
# Check if input text is valid
|
16 |
-
if not text or not text.strip():
|
17 |
-
raise ValueError("Input text cannot be empty.") # Raise an error for invalid input
|
18 |
-
|
19 |
# Make predictions using the Hugging Face pipeline
|
20 |
predictions = classifier(text)
|
21 |
|
@@ -26,18 +18,7 @@ def classify_emotion(text):
|
|
26 |
for item in predictions:
|
27 |
emotion_scores[item["label"]] = item["score"]
|
28 |
|
29 |
-
|
30 |
-
df = pd.DataFrame({
|
31 |
-
"Emotion": list(emotion_scores.keys()),
|
32 |
-
"Score": list(emotion_scores.values())
|
33 |
-
})
|
34 |
-
|
35 |
-
# Prepare a text-based table for display
|
36 |
-
table = "Emotion Scores:\n"
|
37 |
-
table += "----------------\n"
|
38 |
-
table += "\n".join([f"{emotion}: {score:.4f}" for emotion, score in emotion_scores.items()])
|
39 |
-
|
40 |
-
return df, table
|
41 |
|
42 |
# Create a custom Gradio interface with title, description, and examples
|
43 |
gr.Interface(
|
@@ -47,22 +28,11 @@ gr.Interface(
|
|
47 |
label="Input Text",
|
48 |
lines=4
|
49 |
),
|
50 |
-
outputs=
|
51 |
-
|
52 |
-
|
53 |
-
y="Score", # Specify the y-axis column
|
54 |
-
label="Emotion Scores Bar Plot",
|
55 |
-
title="Emotion Probabilities",
|
56 |
-
color="#2563eb", # Set the bar color
|
57 |
-
height=400,
|
58 |
-
vertical=True
|
59 |
-
),
|
60 |
-
gr.Textbox(label="Emotion Scores Table") # Text-based table output
|
61 |
-
],
|
62 |
-
title="Emotion Detection with DistilBERT",
|
63 |
-
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.",
|
64 |
examples=[
|
65 |
-
"I am so happy to see you!",
|
66 |
"I'm really angry about what happened.",
|
67 |
"The sunset was absolutely beautiful today.",
|
68 |
"I'm worried about the upcoming exam.",
|
|
|
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")
|
|
|
|
|
|
|
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 |
|
|
|
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 |
label="Input Text",
|
29 |
lines=4
|
30 |
),
|
31 |
+
outputs=gr.Label(label="Emotion Probabilities"), # Use gr.Label for a cleaner interface
|
32 |
+
title="CMACHINES25 | 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 am not so happy to see you!",
|
36 |
"I'm really angry about what happened.",
|
37 |
"The sunset was absolutely beautiful today.",
|
38 |
"I'm worried about the upcoming exam.",
|