File size: 1,704 Bytes
07e0380
 
 
 
 
 
 
 
fc31cc8
07e0380
 
fc31cc8
 
 
 
07e0380
 
 
fc31cc8
64b7228
 
 
 
 
 
 
 
 
 
 
 
 
 
ae02fae
07e0380
 
 
 
ae02fae
07e0380
fc31cc8
07e0380
 
fc31cc8
07e0380
 
 
 
 
fc31cc8
64b7228
 
 
 
 
 
07e0380
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import gradio as gr
from transformers import pipeline

def analyze_sentiment(text):
    sentiment_analyzer = pipeline(
        "sentiment-analysis",
        model="nlptown/bert-base-multilingual-uncased-sentiment"
    )
    
    result = sentiment_analyzer(text, return_all_scores=True)
    
    score = int(result[0]['score'] * 5)
    sentiment_stars = "⭐" * score
    
    return sentiment_stars

with gr.Blocks() as demo:
    gr.Markdown("## Sentiment Analysis Demo")
    
    with gr.Row():
        examples_dropdown = gr.Dropdown(
            label="Click to load example texts",
            choices=[
                "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."
            ],
            interactive=True
        )
    
    def load_example(selected_example):
        return selected_example
    
    with gr.Row():
        input_text = gr.Textbox(
            label="Enter your text here",
            placeholder="Type or paste your text...",
            lines=3
        )
    
    with gr.Row():
        analyze_button = gr.Button("Analyze Sentiment", variant="primary")
    
    with gr.Row():
        output_text = gr.Textbox(
            label="Sentiment (Stars)",
            lines=1
        )
    
    examples_dropdown.change(
        fn=load_example,
        inputs=examples_dropdown,
        outputs=input_text
    )
    
    analyze_button.click(
        fn=analyze_sentiment,
        inputs=input_text,
        outputs=output_text
    )

if __name__ == "__main__":
    demo.launch()