Spaces:
Runtime error
Runtime error
danielferreira
commited on
Commit
β’
a788dcb
1
Parent(s):
fef0bcc
Updated app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,46 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
def
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
}
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
iface = gr.Interface(fn = predict,
|
19 |
-
inputs = "text",
|
20 |
-
outputs = gr.outputs.Label(num_top_classes=None, type="auto", label=None),
|
21 |
-
title = 'Sentiment Analysis')
|
22 |
|
23 |
-
|
|
|
|
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 |
+
)
|
41 |
+
|
42 |
+
iface.launch()
|
43 |
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
if __name__ == "__main__":
|
46 |
+
main()
|