Spaces:
Sleeping
Sleeping
iabualhaol
commited on
Commit
β’
efb360e
1
Parent(s):
a5b7ad9
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
3 |
+
|
4 |
+
|
5 |
+
class EmotionClassifier:
|
6 |
+
def __init__(self, model_name: str):
|
7 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
8 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
self.pipeline = pipeline(
|
10 |
+
"text-classification",
|
11 |
+
model=self.model,
|
12 |
+
tokenizer=self.tokenizer,
|
13 |
+
return_all_scores=True,
|
14 |
+
)
|
15 |
+
|
16 |
+
def predict(self, input_text: str):
|
17 |
+
pred = self.pipeline(input_text)[0]
|
18 |
+
result = {
|
19 |
+
"Sadness π": pred[0]["score"],
|
20 |
+
"Joy π": pred[1]["score"],
|
21 |
+
"Love π": pred[2]["score"],
|
22 |
+
"Anger π ": pred[3]["score"],
|
23 |
+
"Fear π¨": pred[4]["score"],
|
24 |
+
"Surprise π²": pred[5]["score"],
|
25 |
+
}
|
26 |
+
return result
|
27 |
+
|
28 |
+
|
29 |
+
def main():
|
30 |
+
model = EmotionClassifier("bhadresh-savani/bert-base-uncased-emotion")
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=model.predict,
|
33 |
+
inputs=gr.inputs.Textbox(
|
34 |
+
lines=3,
|
35 |
+
placeholder="Type a phrase that has some emotion",
|
36 |
+
label="Input Text",
|
37 |
+
),
|
38 |
+
outputs="label",
|
39 |
+
title="Emotion Classification",
|
40 |
+
examples=[
|
41 |
+
"I get so down when I'm alone",
|
42 |
+
"I believe that today everything will work out",
|
43 |
+
"It was so dark there I was afraid to go",
|
44 |
+
"I loved the gift you gave me",
|
45 |
+
"I was very surprised by your presentation.",
|
46 |
+
],
|
47 |
+
)
|
48 |
+
|
49 |
+
iface.launch()
|
50 |
+
|
51 |
+
|
52 |
+
if __name__ == "__main__":
|
53 |
+
main()
|