POKWIR's picture
Update app.py
ba1802b verified
import gradio as gr
from transformers import pipeline
MODEL_ID = "pokwir/Bert_sentiment_classifier"
LABEL_ORDER = ["Neutral", "Positive", "Negative"]
ID2LABEL = {"LABEL_0": "Neutral", "LABEL_1": "Positive", "LABEL_2": "Negative"}
clf = pipeline(
"text-classification",
model=MODEL_ID,
tokenizer=MODEL_ID,
top_k=None # replaces deprecated return_all_scores=True
)
def predict(text: str):
text = (text or "").strip()
if not text:
return {lab: 0.0 for lab in LABEL_ORDER}, {}
scores = clf(text)[0] # list of dicts
ordered = {lab: 0.0 for lab in LABEL_ORDER}
for d in scores:
lab = ID2LABEL.get(d["label"], d["label"])
if lab in ordered:
ordered[lab] = float(d["score"])
return ordered, ordered
demo = gr.Interface(
fn=predict,
inputs=gr.Textbox(lines=3, placeholder="Type a sentence..."),
outputs=[gr.Label(label="Scores"), gr.JSON(label="Scores (JSON)")],
title="BERT Sentiment Classifier (3-class)",
description=f"Model: {MODEL_ID}",
examples=[
["I had surgery last month. and I was very impressed with the quality of service from the moment I got in till I left. Also I like to mention the nurses they were out standing"],
["I received the update and will review it later this week."],
["Dirty. Generally poor attitude among the nurses, even the good know the place sucks. When patients are crying for help nurse should not be busy watching Tik-Tok. Too many mistakes made too often. Teaching nurses instructing student nurse procedures incorrectly. Yes, it is bad."]
],
)
if __name__ == "__main__":
demo.launch(ssr_mode=False)