abdulmatinomotoso commited on
Commit
3262cae
1 Parent(s): f8dbb99

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import pandas as pd
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
+ import torch
6
+
7
+ labels = labels = ['Comment (Expert / Leadership)', 'Personal News','Event Participation', 'Obituary', 'Award / Recognition', 'Company achievement',
8
+ 'Financial Insight of stockholding', 'Job Updates', 'Philanthropy', 'Negative News', 'Achievement / Highlighting']
9
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
10
+
11
+
12
+ model_name = 'abdulmatinomotoso/finetuned-distilbert-shidhant-emotion-article-categorization'
13
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
14
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
15
+
16
+ def get_category(text):
17
+ #text = read_in_text(file.name)
18
+
19
+ input_tensor = tokenizer.encode(text, return_tensors='pt', truncation=True)
20
+ logits = model(input_tensor).logits
21
+
22
+ softmax = torch.nn.Softmax(dim=1)
23
+ probs = softmax(logits)[0]
24
+ probs = probs.cpu().detach().numpy()
25
+ max_index = np.argmax(probs)
26
+ sentiment = labels[max_index]
27
+ return sentiment
28
+
29
+ demo = gr.Interface(get_category, inputs=gr.inputs.Textbox(),
30
+ outputs = 'text',
31
+ title='Articles emotion Categorization')
32
+
33
+ if __name__ == '__main__':
34
+ demo.launch(debug=True)