abdulmatinomotoso commited on
Commit
c29d314
·
1 Parent(s): 7b32e40

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #importing the necessary libraries
2
+ import gradio as gr
3
+ import numpy as np
4
+ import pandas as pd
5
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
6
+ import torch
7
+
8
+ #Defining the labels of the models
9
+ labels = ['Politics', 'Tech', 'Entertainment', 'Business', 'World', 'Sport']
10
+
11
+ #Defining the models and tokenuzer
12
+ model_name = 'valurank/finetuned-distilbert-news-article-categorization'
13
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
14
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
15
+
16
+ #Reading in the text file
17
+ def read_in_text(url):
18
+ with open(url, 'r') as file:
19
+ article = file.read()
20
+ return article
21
+
22
+ #Defining a function to get the category of the news article
23
+ def get_category(file):
24
+ text = read_in_text(file.name)
25
+
26
+ input_tensor = tokenizer.encode(text, return_tensors='pt', truncation=True)
27
+ logits = model(input_tensor).logits
28
+
29
+ softmax = torch.nn.Softmax(dim=1)
30
+ probs = softmax(logits)[0]
31
+ probs = probs.cpu().detach().numpy()
32
+ max_index = np.argmax(probs)
33
+ emotion = labels[max_index]
34
+ return emotion
35
+
36
+ #Creating the interface for the radio app
37
+ demo = gr.Interface(get_category, inputs=gr.inputs.File(label='Upload your .txt file here'),
38
+ outputs = 'text',
39
+ title='News Article Categorization')
40
+
41
+
42
+ #Launching the radio app
43
+ if __name__ == '__main__':
44
+ demo.launch(debug=True)