abdull4h's picture
Create app.py
4e0fcd1 verified
import gradio as gr
from transformers import pipeline
# Initialize the sentiment analysis pipeline
# Model: nlptown/bert-base-multilingual-uncased-sentiment
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"] # e.g., "1 star", "2 stars", "3 stars", "4 stars", or "5 stars"
return f"Predicted sentiment: {label}"
# Predefined examples
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."]
]
# Create the Gradio interface
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()