Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,80 +2,55 @@ import gradio as gr
|
|
2 |
from transformers import pipeline
|
3 |
import matplotlib.pyplot as plt
|
4 |
from wordcloud import WordCloud
|
5 |
-
import json
|
6 |
-
import os
|
7 |
|
8 |
# Load sentiment analysis model
|
9 |
sentiment_pipeline = pipeline("sentiment-analysis")
|
10 |
|
11 |
-
#
|
12 |
-
STATE_FILE = "state.json"
|
13 |
-
|
14 |
-
def save_state(text, sentiment, confidence):
|
15 |
-
with open(STATE_FILE, "w") as f:
|
16 |
-
json.dump({"text": text, "sentiment": sentiment, "confidence": confidence}, f)
|
17 |
-
|
18 |
-
def load_state():
|
19 |
-
if os.path.exists(STATE_FILE):
|
20 |
-
with open(STATE_FILE, "r") as f:
|
21 |
-
return json.load(f)
|
22 |
-
return {"text": "", "sentiment": "", "confidence": ""}
|
23 |
-
|
24 |
-
# Function to analyze sentiment in real-time
|
25 |
def analyze_sentiment(text):
|
26 |
if not text.strip():
|
27 |
return "Enter text to analyze.", "", None, ""
|
28 |
-
|
29 |
result = sentiment_pipeline(text)[0]
|
30 |
sentiment, confidence = result['label'], result['score']
|
31 |
-
|
32 |
sentiment_map = {
|
33 |
"POSITIVE": ("π’ Positive π", "green"),
|
34 |
"NEGATIVE": ("π΄ Negative π ", "red"),
|
35 |
"NEUTRAL": ("π‘ Neutral π", "orange")
|
36 |
}
|
37 |
-
|
38 |
sentiment_label, color = sentiment_map.get(sentiment.upper(), ("βͺ Unknown β", "gray"))
|
39 |
-
|
40 |
# Generate Word Cloud
|
41 |
wordcloud = WordCloud(width=400, height=200, background_color="white").generate(text)
|
42 |
fig, ax = plt.subplots()
|
43 |
ax.imshow(wordcloud, interpolation='bilinear')
|
44 |
ax.axis("off")
|
45 |
-
|
46 |
-
save_state(text, sentiment_label, confidence)
|
47 |
-
|
48 |
return sentiment_label, f"Confidence: {confidence:.2%}", fig, text
|
49 |
|
50 |
# UI Layout
|
51 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
52 |
gr.Markdown("# π AI Sentiment Analyzer")
|
53 |
gr.Markdown("Analyze sentiment in real-time with enhanced visualization.")
|
54 |
-
|
55 |
with gr.Row():
|
56 |
dark_mode = gr.Checkbox(label="π Dark Mode")
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
text_input = gr.Textbox(lines=3, placeholder="Type your text here...", label="Your Input")
|
61 |
-
|
62 |
-
# Attach the `.change()` event to enable real-time analysis
|
63 |
-
text_input.change(analyze_sentiment, inputs=text_input, outputs=[sentiment_output, confidence_output, wordcloud_output, text_input])
|
64 |
-
|
65 |
-
|
66 |
analyze_button = gr.Button("Analyze Sentiment β¨")
|
67 |
reset_button = gr.Button("π Reset")
|
68 |
-
|
69 |
with gr.Row():
|
70 |
sentiment_output = gr.Label(label="Sentiment Result")
|
71 |
confidence_output = gr.Label(label="Confidence Score")
|
72 |
-
|
73 |
wordcloud_output = gr.Plot(label="Word Cloud Visualization")
|
74 |
-
|
75 |
-
#
|
76 |
-
|
77 |
-
text_input.value, sentiment_output.value, confidence_output.value = state['text'], state['sentiment'], state['confidence']
|
78 |
-
|
79 |
analyze_button.click(analyze_sentiment, inputs=text_input, outputs=[sentiment_output, confidence_output, wordcloud_output, text_input])
|
80 |
reset_button.click(lambda: ("", "", None, ""), inputs=[], outputs=[sentiment_output, confidence_output, wordcloud_output, text_input])
|
81 |
|
|
|
2 |
from transformers import pipeline
|
3 |
import matplotlib.pyplot as plt
|
4 |
from wordcloud import WordCloud
|
|
|
|
|
5 |
|
6 |
# Load sentiment analysis model
|
7 |
sentiment_pipeline = pipeline("sentiment-analysis")
|
8 |
|
9 |
+
# Function to analyze sentiment
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def analyze_sentiment(text):
|
11 |
if not text.strip():
|
12 |
return "Enter text to analyze.", "", None, ""
|
13 |
+
|
14 |
result = sentiment_pipeline(text)[0]
|
15 |
sentiment, confidence = result['label'], result['score']
|
16 |
+
|
17 |
sentiment_map = {
|
18 |
"POSITIVE": ("π’ Positive π", "green"),
|
19 |
"NEGATIVE": ("π΄ Negative π ", "red"),
|
20 |
"NEUTRAL": ("π‘ Neutral π", "orange")
|
21 |
}
|
22 |
+
|
23 |
sentiment_label, color = sentiment_map.get(sentiment.upper(), ("βͺ Unknown β", "gray"))
|
24 |
+
|
25 |
# Generate Word Cloud
|
26 |
wordcloud = WordCloud(width=400, height=200, background_color="white").generate(text)
|
27 |
fig, ax = plt.subplots()
|
28 |
ax.imshow(wordcloud, interpolation='bilinear')
|
29 |
ax.axis("off")
|
30 |
+
|
|
|
|
|
31 |
return sentiment_label, f"Confidence: {confidence:.2%}", fig, text
|
32 |
|
33 |
# UI Layout
|
34 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
35 |
gr.Markdown("# π AI Sentiment Analyzer")
|
36 |
gr.Markdown("Analyze sentiment in real-time with enhanced visualization.")
|
37 |
+
|
38 |
with gr.Row():
|
39 |
dark_mode = gr.Checkbox(label="π Dark Mode")
|
40 |
+
|
41 |
+
text_input = gr.Textbox(lines=3, placeholder="Type your text here...", label="Your Input")
|
42 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
analyze_button = gr.Button("Analyze Sentiment β¨")
|
44 |
reset_button = gr.Button("π Reset")
|
45 |
+
|
46 |
with gr.Row():
|
47 |
sentiment_output = gr.Label(label="Sentiment Result")
|
48 |
confidence_output = gr.Label(label="Confidence Score")
|
49 |
+
|
50 |
wordcloud_output = gr.Plot(label="Word Cloud Visualization")
|
51 |
+
|
52 |
+
# Attach event handlers AFTER defining components
|
53 |
+
text_input.change(analyze_sentiment, inputs=text_input, outputs=[sentiment_output, confidence_output, wordcloud_output, text_input])
|
|
|
|
|
54 |
analyze_button.click(analyze_sentiment, inputs=text_input, outputs=[sentiment_output, confidence_output, wordcloud_output, text_input])
|
55 |
reset_button.click(lambda: ("", "", None, ""), inputs=[], outputs=[sentiment_output, confidence_output, wordcloud_output, text_input])
|
56 |
|