Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
def main():
|
28 |
+
model = EmotionClassifier("bhadresh-savani/bert-base-uncased-emotion")
|
29 |
+
iface = gr.Interface(
|
30 |
+
fn=model.predict,
|
31 |
+
inputs=gr.inputs.Textbox(
|
32 |
+
lines=3,
|
33 |
+
placeholder="Type a phrase that has some emotion",
|
34 |
+
label="Input Text",
|
35 |
+
),
|
36 |
+
outputs="label",
|
37 |
+
title="Emotion Classification",
|
38 |
+
examples=[
|
39 |
+
"I get so down when I'm alone",
|
40 |
+
"I believe that today everything will work out",
|
41 |
+
"It was so dark there I was afraid to go",
|
42 |
+
"I loved the gift you gave me",
|
43 |
+
"Some AI is scary",
|
44 |
+
"I don't like heights",
|
45 |
+
"I was very surprised by your presentation.",
|
46 |
+
],
|
47 |
+
)
|
48 |
+
|
49 |
+
iface.launch()
|