File size: 1,502 Bytes
c488555
 
c489380
c488555
 
 
 
 
 
 
 
5a6a301
 
 
 
 
c489380
 
 
 
 
 
5a6a301
c489380
 
 
 
 
 
 
 
 
 
 
 
 
 
5a6a301
c488555
 
b8d9c2f
 
5a6a301
b8d9c2f
5a6a301
b8d9c2f
 
c488555
b8d9c2f
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
import gradio as gr
import nltk
import plotly.graph_objects as go
from nltk.sentiment.vader import SentimentIntensityAnalyzer

nltk.download("vader_lexicon")
sid = SentimentIntensityAnalyzer()

def sentiment_analysis(text):
    scores = sid.polarity_scores(text)
    del scores["compound"]
    
    labels = list(scores.keys())
    values = list(scores.values())
    colors = ['red' if label == 'neg' else 'green' if label == 'pos' else 'white' for label in labels]
    
    fig = go.Figure(go.Bar(
        x=values,
        y=labels,
        orientation='h',
        marker=dict(color=colors)
    ))
    
    fig.update_layout(
        title="Sentiment Analysis",
        xaxis_title="Score",
        yaxis_title="Sentiment",
        xaxis=dict(range=[0, 1]),
        yaxis=dict(showgrid=True),
        plot_bgcolor='rgba(0,0,0,0)',
        paper_bgcolor='rgba(0,0,0,0)'
    )
    
    fig.update_xaxes(showgrid=True)
    fig.update_yaxes(showgrid=True)
    
    fig.write_image('sentiment_chart.png')
    return gr.Image('sentiment_chart.png')

demo = gr.Interface(
    fn=sentiment_analysis,
    inputs=gr.Textbox(lines=5, placeholder="Enter a positive or negative sentence here..."),
    outputs="image",
    title="Sentiment Analysis",
    description="Enter a sentence to analyze its sentiment. The model will return the sentiment scores for positive, negative, and neutral tones as a bar chart.",
    examples=[["This is wonderful!"], ["I hate this!"], ["It's okay, not bad."]]
)

demo.launch()