File size: 1,274 Bytes
3262cae
 
 
 
 
 
 
 
 
 
 
27f0b7c
3262cae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
import pandas as pd
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

labels = labels = ['Comment (Expert / Leadership)', 'Personal News','Event Participation', 'Obituary', 'Award / Recognition', 'Company achievement',
          'Financial Insight of stockholding', 'Job Updates', 'Philanthropy', 'Negative News', 'Achievement / Highlighting']
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')


model_name = 'almalabs/finetuned-distilbert-article-emotions-categorization'
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

def get_category(text):
  #text = read_in_text(file.name)

  input_tensor = tokenizer.encode(text, return_tensors='pt', truncation=True)
  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)
  sentiment = labels[max_index]
  return sentiment

demo = gr.Interface(get_category, inputs=gr.inputs.Textbox(),
                    outputs = 'text',
                    title='Articles emotion Categorization')

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