CoolFace
Apppublic

fawad5/News-Topic-Classifier

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
app.py27 linesDownload Raw Back to root
1import gradio as gr2from transformers import pipeline3 4# 1. Load the model from your Hub repository5# REPLACE 'your-username' with your actual username6model_path = "fawad5/bert-news-classifier-agnews"7classifier = pipeline("text-classification", model=model_path)8 9# Labels for AG News10labels = ['World', 'Sports', 'Business', 'Sci/Tech']11 12def predict(text):13    outputs = classifier(text, top_k=None)14    # Format the results for Gradio15    results = {labels[int(res['label'].split('_')[-1])]: float(res['score']) for res in outputs}16    return results17 18# 2. Define the Gradio Interface19demo = gr.Interface(20    fn=predict,21    inputs=gr.Textbox(placeholder="Enter a news headline...", label="Input News"),22    outputs=gr.Label(num_top_classes=4, label="Prediction"),23    title="Live News Topic Classifier",24    description="Categorize news into World, Sports, Business, or Sci/Tech using a fine-tuned BERT model."25)26 27demo.launch()