|
|
import gradio as gr
|
|
|
from transformers import pipeline
|
|
|
|
|
|
|
|
|
classifier = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
|
|
|
|
|
|
|
|
|
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}%"
|
|
|
|
|
|
|
|
|
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])
|
|
|
|
|
|
|
|
|
demo.launch()
|
|
|
|