starting over
Browse files
app.py
CHANGED
@@ -1,49 +1,28 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
ALL_EMOTIONS = ["sadness", "joy", "love", "anger", "fear", "surprise"]
|
9 |
|
10 |
-
def
|
11 |
-
|
12 |
-
|
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 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
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 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|