douglasgoodwin commited on
Commit
d3b769d
·
verified ·
1 Parent(s): dfaded4

starting over

Browse files
Files changed (1) hide show
  1. app.py +21 -42
app.py CHANGED
@@ -1,49 +1,28 @@
 
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
-
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 scores returned by the model
18
- print(predictions)
19
- for item in predictions:
20
- iscore = float(item["score"])
21
- ilabel = item["label"]
22
- print(f"iscore is {iscore}")
23
- if iscore < 0.1:
24
- iscore = iscore*10.0
25
 
26
- print(f"iscore for {ilabel} is now {iscore}")
27
- emotion_scores[ilabel] = iscore
28
-
29
- return emotion_scores
30
 
31
- # Create a custom Gradio interface with title, description, and examples
32
- gr.Interface(
33
- fn=classify_emotion,
34
- inputs=gr.Textbox(
35
- placeholder="Enter text to analyze...",
36
- label="Input Text",
37
- lines=4
38
- ),
39
- outputs=gr.Label(label="Emotion Probabilities"), # Use gr.Label for a cleaner interface
40
- title="CMACHINES25 | Emotion Detection with DistilBERT",
41
- description="This app uses the DistilBERT model fine-tuned for emotion detection. Enter a piece of text to analyze its emotional content!",
42
- examples=[
43
- "I am not so happy to see you!",
44
- "I'm really angry about what happened.",
45
- "The sunset was absolutely beautiful today.",
46
- "I'm worried about the upcoming exam.",
47
- "Fear is the mind-killer. I will face my fear."
48
- ]
49
- ).launch()
 
1
+ ```python
2
  import gradio as gr
3
  from transformers import pipeline
4
 
5
+ classifier = pipeline(
6
+ "text-classification",
7
+ model="bhadresh-savani/distilbert-base-uncased-emotion",
8
+ return_all_scores=True,
9
+ )
10
 
11
+ EMOTIONS = ["sadness", "joy", "love", "anger", "fear", "surprise"]
 
12
 
13
+ def predict_emotion(text):
14
+ results = classifier(text)[0]
15
+ return {result["label"]: result["score"] for result in results if result["label"] in EMOTIONS}
 
 
 
 
 
 
 
 
 
 
 
 
16
 
 
 
 
 
17
 
18
+ iface = gr.Interface(
19
+ fn=predict_emotion,
20
+ inputs=gr.Textbox(lines=3, placeholder="Enter text here..."),
21
+ outputs=gr.Label(num_top_classes=6),
22
+ title="Emotion Detector",
23
+ description="Enter some text and see the predicted emotions.",
24
+ )
25
+
26
+ if __name__ == "__main__":
27
+ iface.launch()
28
+ ```