CoolFace
Apppublic

mssab/News_Categorization

sourceHugging Faceotherupdated 2y agoView on Hugging Face
0likes
app.py68 linesDownload Raw Back to root
1#importing the necessary libraries2import gradio as gr3import numpy as np4import pandas as pd5import re6from transformers import AutoTokenizer, AutoModelForSequenceClassification7import torch8 9#Defining the labels of the models10labels = [ "business", "science", "health", "world", "sport", "politics", "entertainment", "technology", "education", "environment", "travel", "lifestyle", "crime", "opinion", "weather", "culture", "art", "food", "automotive", "finance", "international" ]11 12#Defining the models and tokenuzer13model_name = "valurank/finetuned-distilbert-news-article-categorization"14model = AutoModelForSequenceClassification.from_pretrained(model_name)15tokenizer = AutoTokenizer.from_pretrained(model_name)16 17"""18#Reading in the text file19def read_in_text(url):20  with open(url, 'r') as file:21    article = file.read()22      23    return article24"""25 26def clean_text(raw_text):27  text = raw_text.encode("ascii", errors="ignore").decode(28          "ascii"29    )  # remove non-ascii, Chinese characters30    31  text = re.sub(r"\n", " ", text)32  text = re.sub(r"\n\n", " ", text)33  text = re.sub(r"\t", " ", text)34  text = text.strip(" ")35  text = re.sub(36        " +", " ", text37    ).strip()  # get rid of multiple spaces and replace with a single38 39  text = re.sub(r"Date\s\d{1,2}\/\d{1,2}\/\d{4}", "", text) #remove date40  text = re.sub(r"\d{1,2}:\d{2}\s[A-Z]+\s[A-Z]+", "", text) #remove time41    42  return text43 44#Defining a function to get the category of the news article   45def get_category(text):46  text = clean_text(text)47 48  input_tensor = tokenizer.encode(text, return_tensors="pt", truncation=True)49  logits = model(input_tensor).logits50 51  softmax = torch.nn.Softmax(dim=1)52  probs = softmax(logits)[0]53  probs = probs.cpu().detach().numpy()54  max_index = np.argmax(probs)55  emotion = labels[max_index]56    57  return emotion58  59#Creating the interface for the radio app60demo = gr.Interface(get_category, inputs=gr.Textbox(label="Drop your articles here"),61                    outputs = "text",62                    title="News Article Categorization")63 64 65#Launching the gradio app66if __name__ == "__main__":67  demo.launch(debug=True)68