douglasgoodwin commited on
Commit
ab31676
·
verified ·
1 Parent(s): ef25307
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+
5
+ # Initialize the emotion classification pipeline
6
+ classifier = pipeline("text-classification",
7
+ model="bhadresh-savani/distilbert-base-uncased-emotion",
8
+ return_all_scores=True)
9
+
10
+ def predict_emotion(text):
11
+ if not text:
12
+ return {
13
+ "sadness": 0,
14
+ "joy": 0,
15
+ "love": 0,
16
+ "anger": 0,
17
+ "fear": 0,
18
+ "surprise": 0
19
+ }
20
+
21
+ # Get predictions
22
+ predictions = classifier(text)[0]
23
+
24
+ # Convert to dictionary format for the bar chart
25
+ scores = {pred['label']: pred['score'] for pred in predictions}
26
+ return scores
27
+
28
+ # Create the Gradio interface
29
+ demo = gr.Interface(
30
+ fn=predict_emotion,
31
+ inputs=gr.Textbox(placeholder="Enter text to analyze...", label="Input Text"),
32
+ outputs=gr.BarPlot(
33
+ x="emotion",
34
+ y="probability",
35
+ title="Emotion Probabilities",
36
+ tooltip=["emotion", "probability"],
37
+ height=400
38
+ ),
39
+ title="Emotion Detection with DistilBERT",
40
+ description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter any text to analyze its emotional content.",
41
+ examples=[
42
+ ["I am so happy to see you!"],
43
+ ["I'm really angry about what happened."],
44
+ ["The sunset was absolutely beautiful today."],
45
+ ["I'm worried about the upcoming exam."]
46
+ ],
47
+ allow_flagging="never"
48
+ )
49
+
50
+ demo.launch()