File size: 1,569 Bytes
51bb781
 
 
 
 
 
 
3141888
 
a843c60
51bb781
3f27f6d
51bb781
 
 
 
 
 
 
 
 
 
a843c60
2090148
51bb781
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd2e07a
48e42af
3f27f6d
51bb781
3f27f6d
51bb781
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import gradio as gr
import numpy as np
import pandas as pd
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch


target_list = ["Playful", "Infuriating", "Sentimental", "Cynical", "Depressing", "Awe-inspiring", "Patriotic", "Begrudging", "Educational", "Hopeful",
"Sarcastic", "Disrespectful", "Disparaging"]
#device = torch.device('cuda' if torch.cuda.is_available() else 'CPU')

model_name = "valurank/finetuned-distilbert-multi-label-emotion"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)


def get_probs(logits, threshold=0.5):
    sigm = 1 / (1 + np.exp(-logits))
    return sigm 

def multi_label_emotions(text):
  inputs = tokenizer(text, return_tensors="pt", truncation=True)
  #model.to(device)
    
  with torch.no_grad():
      logits = model(**inputs).logits
      #probs = logits.int().numpy()[0]
      log_probs = get_probs(logits)

      final_log_probs = []
      for log in log_probs:
        final_log_probs.append(log.numpy())

      final_output = []
      for i in zip(final_log_probs[0], target_list):
        final_output.append(i)

      final_output.sort(reverse=True)
      
      final_dict = {}
      for k,v in final_output:
        final_dict[v] = float(k)

      return final_dict


demo = gr.Interface(multi_label_emotions, inputs=gr.Textbox(),
                    outputs = gr.Label(num_top_classes=16),
                    title="Multi-label-emotion-classification")

if __name__ == "__main__":
  demo.launch(debug=True)