imtisal-924's picture
Create app.py
ca51a15 verified
raw
history blame contribute delete
939 Bytes
# app.py
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
# Load pretrained model and tokenizer
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Create sentiment analysis pipeline
classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
# Define inference function
def predict_sentiment(text):
result = classifier(text)
label = result[0]['label']
score = result[0]['score']
return f"{label} ({score:.2f})"
# Gradio UI
iface = gr.Interface(
fn=predict_sentiment,
inputs=gr.Textbox(lines=4, placeholder="Type your text here..."),
outputs="text",
title="Sentiment Analysis App",
description="Enter text and get sentiment prediction (positive/negative)."
)
# Launch app
iface.launch()