Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,42 @@
|
|
1 |
from transformers import pipeline
|
2 |
import gradio as gr
|
|
|
3 |
|
|
|
4 |
model_checkpoint = "MuntasirHossain/RoBERTa-base-finetuned-emotion"
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
label = model(text)[0]["label"]
|
11 |
-
return label
|
12 |
|
13 |
-
|
14 |
-
title = "
|
15 |
-
|
16 |
-
examples=[["This is such a beautiful place"]]
|
17 |
|
18 |
-
|
|
|
|
|
19 |
inputs="textbox",
|
20 |
-
outputs="
|
21 |
title=title,
|
22 |
-
# theme = theme,
|
23 |
description=description,
|
24 |
-
|
25 |
-
).launch()
|
|
|
1 |
from transformers import pipeline
|
2 |
import gradio as gr
|
3 |
+
import emoji
|
4 |
|
5 |
+
# Load pre-trained emotion classification model
|
6 |
model_checkpoint = "MuntasirHossain/RoBERTa-base-finetuned-emotion"
|
7 |
+
emotion_model = pipeline("text-classification", model=model_checkpoint)
|
8 |
|
9 |
+
def classify_and_visualize_emotion(text):
|
10 |
+
# Get the predicted emotion label
|
11 |
+
emotion_label = emotion_model(text)[0]["label"]
|
12 |
+
|
13 |
+
# Map emotion labels to corresponding emojis
|
14 |
+
emoji_mapping = {
|
15 |
+
"joy": "π",
|
16 |
+
"sadness": "π’",
|
17 |
+
"love": "β€οΈ",
|
18 |
+
"anger": "π‘",
|
19 |
+
"fear": "π±",
|
20 |
+
"surprise": "π²",
|
21 |
+
}
|
22 |
+
|
23 |
+
# Get the corresponding emoji for the predicted emotion
|
24 |
+
predicted_emoji = emoji_mapping.get(emotion_label, "π€")
|
25 |
|
26 |
+
# Construct the result string with the predicted emotion and emoji
|
27 |
+
result_text = f"Predicted Emotion: {emotion_label.capitalize()} {predicted_emoji}"
|
28 |
|
29 |
+
return result_text
|
|
|
|
|
30 |
|
31 |
+
# Define interface title and description
|
32 |
+
title = "Emotion Classifier with Emoji Visualization"
|
33 |
+
description = "This AI model classifies text expressions into six emotions: joy, sadness, love, anger, fear, and surprise. The result is visualized with emojis."
|
|
|
34 |
|
35 |
+
# Set up Gradio Interface
|
36 |
+
gr.Interface(
|
37 |
+
fn=classify_and_visualize_emotion,
|
38 |
inputs="textbox",
|
39 |
+
outputs="html",
|
40 |
title=title,
|
|
|
41 |
description=description,
|
42 |
+
).launch()
|
|