Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
|
| 3 |
+
|
| 4 |
+
class Emotionclass:
|
| 5 |
+
def __init__(self, model: str):
|
| 6 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(model)
|
| 7 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model)
|
| 8 |
+
self.pipeline = pipeline(
|
| 9 |
+
"text-classification",
|
| 10 |
+
model=self.model,
|
| 11 |
+
tokenizer=self.tokenizer,
|
| 12 |
+
return_all_scores=True,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
def predict(self, input: str):
|
| 16 |
+
output = self.pipeline(input)[0]
|
| 17 |
+
result = {
|
| 18 |
+
"sad": output[0]["score"],
|
| 19 |
+
"joy": output[1]["score"],
|
| 20 |
+
"love": output[2]["score"],
|
| 21 |
+
"anger": output[3]["score"],
|
| 22 |
+
"fear": output[4]["score"],
|
| 23 |
+
"surprise": output[5]["score"],
|
| 24 |
+
}
|
| 25 |
+
return result
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
model = Emotionclass("ncduy/bert-base-cased-finetuned-emotion")
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
fn=model.predict,
|
| 31 |
+
inputs=gr.inputs.Textbox(
|
| 32 |
+
lines=3,
|
| 33 |
+
placeholder="type here ...",
|
| 34 |
+
label="Input",
|
| 35 |
+
),
|
| 36 |
+
outputs="label",
|
| 37 |
+
title="Emotion Classifier",
|
| 38 |
+
)
|
| 39 |
+
iface.launch()
|