File size: 1,356 Bytes
4ffa2ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
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,  # Function to call for sentiment analysis
    inputs=[
        gr.Textbox(label="Enter Text", placeholder="Type or paste a sentence or paragraph here...", lines=5),
        gr.Button("Analyze Sentiment")  # Button to trigger analysis
    ],
    outputs=gr.Textbox(label="Sentiment Rating (1 to 5 stars)"),  # Display sentiment rating
    live=False,  # Disable live preview while typing
    examples=examples,  # Predefined examples
    description="Sentiment analysis using BERT-based model for multilingual sentiment prediction."
)

iface.launch()