|
|
|
import gradio as gr |
|
import numpy as np |
|
import pandas as pd |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
import torch |
|
|
|
|
|
labels = ['Politics', 'Tech', 'Entertainment', 'Business', 'World', 'Sport'] |
|
|
|
|
|
|
|
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) |
|
|
|
|
|
def read_in_text(url): |
|
with open(url, 'r') as file: |
|
article = file.read() |
|
return 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 |
|
|
|
|
|
demo = gr.Interface(get_category, inputs=gr.inputs.File(label='Upload your .txt file here'), |
|
outputs = 'text', |
|
title='News Article Categorization') |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
demo.launch(debug=True) |