File size: 2,226 Bytes
479c058
4e7df43
2ad6233
4e7df43
479c058
4e7df43
 
2562763
35b4c74
479c058
4e7df43
 
35b4c74
4e7df43
 
35b4c74
2ad6233
4e7df43
 
 
2ad6233
35b4c74
4e7df43
35b4c74
4e7df43
 
 
 
 
35b4c74
4e7df43
2ad6233
4e7df43
2ad6233
4e7df43
 
35b4c74
2ad6233
4e7df43
35b4c74
 
 
2ad6233
4e7df43
35b4c74
2ad6233
4e7df43
 
35b4c74
4e7df43
35b4c74
 
 
4e7df43
 
0feed92
 
 
 
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
import gradio as gr
from transformers import pipeline
import matplotlib.pyplot as plt
from wordcloud import WordCloud

# Load sentiment analysis model
sentiment_pipeline = pipeline("sentiment-analysis")

# Function to analyze sentiment
def analyze_sentiment(text):
    if not text.strip():
        return "Enter text to analyze.", "", None, ""

    result = sentiment_pipeline(text)[0]
    sentiment, confidence = result['label'], result['score']

    sentiment_map = {
        "POSITIVE": ("🟒 Positive 😊", "green"),
        "NEGATIVE": ("πŸ”΄ Negative 😠", "red"),
        "NEUTRAL": ("🟑 Neutral 😐", "orange")
    }

    sentiment_label, color = sentiment_map.get(sentiment.upper(), ("βšͺ Unknown ❓", "gray"))

    # Generate Word Cloud
    wordcloud = WordCloud(width=400, height=200, background_color="white").generate(text)
    fig, ax = plt.subplots()
    ax.imshow(wordcloud, interpolation='bilinear')
    ax.axis("off")

    return sentiment_label, f"Confidence: {confidence:.2%}", fig, text

# UI Layout
with gr.Blocks(theme=gr.themes.Soft()) as iface:
    gr.Markdown("# 🎭 AI Sentiment Analyzer")
    gr.Markdown("Analyze sentiment in real-time with enhanced visualization.")

    with gr.Row():
        dark_mode = gr.Checkbox(label="πŸŒ™ Dark Mode")

    text_input = gr.Textbox(lines=3, placeholder="Type your text here...", label="Your Input")
    
    analyze_button = gr.Button("Analyze Sentiment ✨")
    reset_button = gr.Button("πŸ”„ Reset")

    with gr.Row():
        sentiment_output = gr.Label(label="Sentiment Result")
        confidence_output = gr.Label(label="Confidence Score")

    wordcloud_output = gr.Plot(label="Word Cloud Visualization")

    # Attach event handlers AFTER defining components
    text_input.change(analyze_sentiment, inputs=text_input, outputs=[sentiment_output, confidence_output, wordcloud_output, text_input])
    analyze_button.click(analyze_sentiment, inputs=text_input, outputs=[sentiment_output, confidence_output, wordcloud_output, text_input])
    reset_button.click(lambda: ("", "", None, ""), inputs=[], outputs=[sentiment_output, confidence_output, wordcloud_output, text_input])

# Launch the app
if __name__ == "__main__":
    iface.launch()