import gradio as gr from transformers import pipeline # Load the sentiment classification model classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english") # Define the function for Gradio interface def classify_sentiment(text): if not text.strip(): return "⚠️ Please enter a sentence to analyze.", "" result = classifier(text)[0] label = result['label'] confidence = round(result['score'] * 100, 2) return f"**Prediction:** {label}", f"**Confidence:** {confidence}%" # Create Gradio interface with gr.Blocks() as demo: gr.Markdown("# 🔍 Real-Time Sentiment Classifier") gr.Markdown(""" ### 💡 Model Instruction: This model is trained for binary sentiment classification — **Positive** and **Negative** only. Neutral or mixed opinions may be interpreted as leaning toward one side. For best results, input clearly positive or negative sentences. """) with gr.Row(): with gr.Column(): text_input = gr.Textbox( label="Enter your sentence below 👇", placeholder='Example: "I love the features of this app!" or "The update ruined the experience."', lines=3 ) analyze_button = gr.Button("Analyze Sentiment") with gr.Column(): prediction_output = gr.Markdown() confidence_output = gr.Markdown() analyze_button.click(fn=classify_sentiment, inputs=text_input, outputs=[prediction_output, confidence_output]) # Launch the app demo.launch()