taimoor61 commited on
Commit
2024591
Β·
1 Parent(s): 57dcb56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -13
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
- model = pipeline("text-classification", model=model_checkpoint)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
 
 
8
 
9
- def classify(text):
10
- label = model(text)[0]["label"]
11
- return label
12
 
13
- description = "This AI model is trained to classify texts expressing human emotion into six categories: sadness, joy, love, anger, fear, and surprise."
14
- title = "Classify Texts Expressing Emotion"
15
- # theme = "peach"
16
- examples=[["This is such a beautiful place"]]
17
 
18
- gr.Interface(fn=classify,
 
 
19
  inputs="textbox",
20
- outputs="text",
21
  title=title,
22
- # theme = theme,
23
  description=description,
24
- examples=examples,
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()