testing / app.py
OS84505's picture
Create app.py
180df8d verified
Raw History Blame Contribute Delete
1.02 kB
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)