File size: 1,303 Bytes
4e0fcd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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()