File size: 1,021 Bytes
180df8d | 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 | import os
import gradio as gr
from transformers import pipeline
MODEL_ID = os.environ.get(
"HF_MODEL", "distilbert/distilbert-base-uncased-finetuned-sst-2-english"
)
clf = pipeline("text-classification", model=MODEL_ID)
def predict(text: str):
text = (text or "").strip()
if not text:
return "No input", ""
out = clf(text, truncation=True, max_length=128)[0]
return out["label"], f"{float(out['score']):.6f}"
with gr.Blocks() as demo:
gr.Markdown("# 🧠 Sentiment Analysis Demo\n"
f"Model: `{MODEL_ID}`")
inp = gr.Textbox(lines=3, label="Input Text", placeholder="Type a sentence…")
out_label = gr.Textbox(label="Predicted Label", interactive=False)
out_score = gr.Textbox(label="Confidence Score", interactive=False)
btn = gr.Button("Classify", variant="primary")
btn.click(predict, inputs=inp, outputs=[out_label, out_score], api_name="predict")
demo.queue()
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)
|