File size: 1,107 Bytes
b00c57d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import gradio as gr
import torch
from transformers import DistilBertForSequenceClassification, DistilBertTokenizerFast

def translate(text):
  model_name = 'sbenel/emotion-distilbert'
  tokenizer = DistilBertTokenizerFast.from_pretrained(model_name)
  model= DistilBertForSequenceClassification.from_pretrained(model_name)

  input = tokenizer(text, return_tensors="pt")
  labels = torch.tensor([1]).unsqueeze(0)  # Batch size 1
  output = model(**input, labels=labels)
#  output = model.generate(input["input_ids"], max_length=40, num_beams=4, early_stopping=True)
  
  return tokenizer.decode(output[0], skip_special_tokens=True)
  
title = "Text Emotion Classification"
inputs = gr.inputs.Textbox(lines=1, label="Text")
outputs = [gr.outputs.Textbox(label="Emotions")]

description = "Here use the [emotion-distilbert](https://huggingface.co/sbenel/emotion-distilbert) that was trained with [emotion dataset](https://huggingface.co/datasets/emotion)."

iface = gr.Interface(fn=translate, inputs=inputs, outputs=outputs, theme="grass", title=title, description=description)
iface.launch(enable_queue=True)