Spaces:
Sleeping
Sleeping
File size: 1,634 Bytes
51bb781 48e42af 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 53 54 |
import gradio as gr
import numpy as np
import pandas as pd
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
target_list = ["Cute", "Infuriating", "Sentimental", "Empathetic",
"Cynical", "Depressing", "Awe-inspiring", "Patriotic", "Educational",
"Encouraging", "Voyeuristic", "Funny", "Sarcastic", "Dismissive", "Disparaging"]
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model_name = 'abdulmatinomotoso/finetuned-distilbert-multi-label-emotion_5'
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.inputs.Textbox(),
outputs = gr.Label(num_top_classes=16),
title='Multi-label-emotion-classification')
if __name__ == '__main__':
demo.launch(debug=True) |