|
import gradio as gr |
|
from transformers import pipeline |
|
|
|
|
|
sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment") |
|
|
|
|
|
def analyze_sentiment(text): |
|
result = sentiment_analyzer(text)[0] |
|
sentiment_score = result['label'] |
|
|
|
|
|
if sentiment_score == '1 star': |
|
return 1 |
|
elif sentiment_score == '2 stars': |
|
return 2 |
|
elif sentiment_score == '3 stars': |
|
return 3 |
|
elif sentiment_score == '4 stars': |
|
return 4 |
|
else: |
|
return 5 |
|
|
|
|
|
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."] |
|
] |
|
|
|
|
|
iface = gr.Interface( |
|
fn=analyze_sentiment, |
|
inputs=[ |
|
gr.Textbox(label="Enter Text", placeholder="Type or paste a sentence or paragraph here...", lines=5), |
|
gr.Button("Analyze Sentiment") |
|
], |
|
outputs=gr.Textbox(label="Sentiment Rating (1 to 5 stars)"), |
|
live=False, |
|
examples=examples, |
|
description="Sentiment analysis using BERT-based model for multilingual sentiment prediction." |
|
) |
|
|
|
|
|
iface.launch() |
|
|
|
|