PaulSouvik's picture
✨ Uploaded core app files for real-time sentiment analysis
3332761 verified
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()