olivercareyncl commited on
Commit
35b4c74
Β·
verified Β·
1 Parent(s): 9603cdb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -40
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
- # Store last input/output using localStorage
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
- with gr.Row():
59
- # With this (corrected)
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
- # Load previous session state
76
- state = load_state()
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