|
import gradio as gr |
|
from transformers import pipeline |
|
|
|
|
|
|
|
sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment") |
|
|
|
def analyze_sentiment(text): |
|
""" |
|
Returns the predicted sentiment as a label ranging from 1 to 5 stars. |
|
""" |
|
result = sentiment_analyzer(text)[0] |
|
label = result["label"] |
|
return f"Predicted sentiment: {label}" |
|
|
|
|
|
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."] |
|
] |
|
|
|
|
|
demo = gr.Interface( |
|
fn=analyze_sentiment, |
|
inputs=gr.Textbox(lines=3, label="Enter Your Text Here"), |
|
outputs=gr.Textbox(label="Predicted Sentiment"), |
|
title="Multilingual Sentiment Analysis", |
|
description=( |
|
"This app uses the 'nlptown/bert-base-multilingual-uncased-sentiment' model " |
|
"to predict sentiment on a scale of 1 to 5 stars." |
|
), |
|
examples=examples, |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|