File size: 977 Bytes
f9bdddd
 
 
 
 
 
2ba4fe2
f9bdddd
2ba4fe2
f9bdddd
 
 
 
2ba4fe2
f9bdddd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import numpy as np
import torch
import gradio as gr
labels = ['sadness', 'joy','love', 'anger','fear', 'surprise']

#device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model_name = "abdulmatinomotoso/emotion_detection_finetuned_distilbert"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)


def get_emotion(text):
  input_tensor = tokenizer.encode(text, return_tensors="pt")
  logits = model(input_tensor).logits

  softmax = torch.nn.Softmax(dim=1)
  probs = softmax(logits)[0]
  probs = probs.cpu().detach().numpy()
  max_index = np.argmax(probs)
  emotion = labels[max_index]
  return emotion
  
demo = gr.Interface(get_emotion, inputs='text',
                    outputs="text",
                    title = "Emotion Detection")
                    
if __name__ == "__main__":
    demo.launch(debug=True)