File size: 1,576 Bytes
b4b9f1a
 
 
300ded4
645bc1a
1f140b1
 
 
 
 
 
b4b9f1a
 
 
645bc1a
b4b9f1a
 
 
53a58d8
b4b9f1a
 
 
 
 
1f140b1
b4b9f1a
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

pipe = pipeline("text-classification", model="kitrofimov/news-clf", top_k=3)
label_names = dict(zip([f"LABEL_{i}" for i in range(4)], ["World", "Sports", "Business", "Sci/Tech"]))
examples = [
    ["NASA announces new discovery on Mars water."],
    ["Stock markets rally after positive earnings report."],
    ["Lionel Messi scores a hat-trick in the Champions League."],
    ["UN summit discusses climate change and global policies."]
]

def classify(text):
    preds = pipe(text)[0]
    return {label_names[p["label"]]: float(p["score"]) for p in preds}

with gr.Blocks() as demo:
    gr.Markdown("# News Classifier")
    gr.Markdown("Paste a news article below and see which category it belongs to! (one of \"world\", \"sports\", \"business\" and \"science / technology\")")
    gr.Markdown("This model is based on a [`distilbert-base-uncased`](https://huggingface.co/distilbert/distilbert-base-uncased) architecture and was fine-tuned on the [AG News](https://huggingface.co/datasets/fancyzhx/ag_news) dataset for 3 epochs. Training code [here](https://colab.research.google.com/drive/1KTai0S1dzwIoS3Sba_jJG9ZNISRjSKGo)")
    
    with gr.Row():
        with gr.Column():
            input = gr.Textbox(lines=5, placeholder="Enter your news article...")
            gr.Examples(examples=examples, inputs=input)
            classify_btn = gr.Button("Classify")
        with gr.Column():
            output = gr.Label(num_top_classes=3)

    classify_btn.click(classify, inputs=input, outputs=output)

demo.launch()