Sentiment / app.py
C22454222's picture
Update app.py
3d8045e verified
Raw
History Blame Contribute Delete
766 Bytes
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)