File size: 766 Bytes
e29d2c6
 
 
cc9cbd4
e29d2c6
cc9cbd4
 
e29d2c6
 
cc9cbd4
 
 
2265f0a
 
cc9cbd4
 
 
 
e29d2c6
cc9cbd4
 
 
 
 
 
 
3d8045e
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
import gradio as gr
from transformers import pipeline

classifier = pipeline(
    "sentiment-analysis",
    model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
    device=-1,
)

def classify_sentiment(text: str):
    if not text or not text.strip():
        return {"error": "No text provided"}
    # No top_k — returns flat list [{"label": ..., "score": ...}, ...]
    results = classifier(text, truncation=True, max_length=512)
    return [
        {"label": r["label"], "score": round(r["score"], 4)}
        for r in results
    ]

demo = gr.Interface(
    fn=classify_sentiment,
    inputs=gr.Textbox(label="Article Text"),
    outputs=gr.JSON(label="Sentiment Classification"),
    title="Sentiment Classifier",
)

demo.launch(ssr_mode=False)