|
import gradio as gr |
|
from transformers import pipeline |
|
|
|
|
|
sentiment_pipeline = pipeline("text-classification", model="nlptown/bert-base-multilingual-uncased-sentiment") |
|
|
|
def analyze_sentiment(text): |
|
if not text.strip(): |
|
return "Please enter some text to analyze." |
|
|
|
result = sentiment_pipeline(text)[0] |
|
return f"Predicted Sentiment: {result['label']} stars" |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("# Sentiment Analysis using BERT Model") |
|
gr.Markdown("Enter a sentence or paragraph below and click 'Analyze' to get the predicted sentiment (1 to 5 stars).") |
|
|
|
text_input = gr.Textbox(label="Input Text", placeholder="Enter your text here...", lines=3) |
|
analyze_button = gr.Button("Analyze Sentiment") |
|
output_text = gr.Textbox(label="Predicted Sentiment", interactive=False) |
|
|
|
examples = [ |
|
"I love this product! It's amazing!", |
|
"This was the worst experience I've ever had.", |
|
"The movie was okay, not great but not bad either.", |
|
"Absolutely fantastic! I would recommend it to everyone." |
|
] |
|
|
|
gr.Examples(examples=examples, inputs=text_input) |
|
analyze_button.click(analyze_sentiment, inputs=text_input, outputs=output_text) |
|
|
|
|
|
demo.launch() |