Spaces:
Runtime error
Runtime error
abdulmatinomotoso
commited on
Commit
·
f9bdddd
1
Parent(s):
437cf63
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
+
import numpy as np
|
3 |
+
import torch
|
4 |
+
import gradio as gr
|
5 |
+
labels = ['sadness', 'joy','love', 'anger','fear', 'surprise']
|
6 |
+
|
7 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
8 |
+
model_name = "abdulmatinomotoso/emotion_detection_finetuned_distilbert"
|
9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name).to(device)
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
+
|
12 |
+
|
13 |
+
def get_emotion(text):
|
14 |
+
input_tensor = tokenizer.encode(text, return_tensors="pt").to("cuda")
|
15 |
+
logits = model(input_tensor).logits
|
16 |
+
|
17 |
+
softmax = torch.nn.Softmax(dim=1)
|
18 |
+
probs = softmax(logits)[0]
|
19 |
+
probs = probs.cpu().detach().numpy()
|
20 |
+
max_index = np.argmax(probs)
|
21 |
+
emotion = labels[max_index]
|
22 |
+
return emotion
|
23 |
+
|
24 |
+
demo = gr.Interface(get_emotion, inputs='text',
|
25 |
+
outputs="text",
|
26 |
+
title = "Emotion Detection")
|
27 |
+
|
28 |
+
if __name__ == "__main__":
|
29 |
+
demo.launch(debug=True)
|