File size: 1,473 Bytes
c29d314
 
 
 
 
 
 
 
 
 
1073c6d
c29d314
 
15e4b11
 
c29d314
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#importing the necessary libraries
import gradio as gr
import numpy as np
import pandas as pd
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

#Defining the labels of the models
labels = ['Politics', 'Tech', 'Entertainment', 'Business', 'World', 'Sport']


#Defining the models and tokenuzer
model_name = 'valurank/finetuned-distilbert-news-article-categorization'
model = AutoModelForSequenceClassification.from_pretrained(model_name, use_auth_token=True)
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)

#Reading in the text file
def read_in_text(url):
  with open(url, 'r') as file:
    article = file.read()
    return article
 
#Defining a function to get the category of the news article   
def get_category(file):
  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)
  emotion = labels[max_index]
  return emotion
  
#Creating the interface for the radio app
demo = gr.Interface(get_category, inputs=gr.inputs.File(label='Upload your .txt file here'),
                    outputs = 'text',
                    title='News Article Categorization')
                    
                    
#Launching the radio app
if __name__ == '__main__':
  demo.launch(debug=True)