a7madmostafa's picture
Create app.py
a5207b2 verified
Raw
History Blame Contribute Delete
808 Bytes
import gradio as gr
from transformers import pipeline
# Load model
pipe = pipeline(
"text-classification",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english"
)
# Inference function
def classify_text(text):
if not text.strip():
return "Please enter some text."
result = pipe(text)[0]
label = result["label"]
score = round(result["score"], 4)
return f"Sentiment: {label}\nConfidence: {score}"
# Gradio UI
app = gr.Interface(
fn=classify_text,
inputs=gr.Textbox(
lines=4,
placeholder="Type a sentence to analyze sentiment..."
),
outputs=gr.Textbox(label="Result"),
title="Sentiment Analysis App",
description="Powered by DistilBERT (SST-2)\nClassifies text as POSITIVE or NEGATIVE."
)
# Run app
app.launch()