edwardthefma commited on
Commit
c9a1d9b
·
verified ·
1 Parent(s): 5ee5a24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -14
app.py CHANGED
@@ -12,6 +12,7 @@ from nltk.tokenize import sent_tokenize
12
  from better_profanity import profanity
13
  import tempfile
14
  import os
 
15
 
16
  # Download NLTK data
17
  nltk.download('punkt', quiet=True)
@@ -112,7 +113,7 @@ def fetch_x_post(x_url, model_name, pos_words, neg_words, intensity, custom_mode
112
  sample_text = "Sample X post from " + x_url
113
  return analyze_sentiment(sample_text, model_name, pos_words, neg_words, intensity, custom_model_path, source="X post")
114
 
115
- # Generate timeline (return BytesIO directly)
116
  def generate_timeline():
117
  if not sentiment_scores:
118
  return None
@@ -128,27 +129,21 @@ def generate_timeline():
128
  buf = io.BytesIO()
129
  plt.savefig(buf, format="png")
130
  buf.seek(0)
 
131
  plt.close()
132
- return buf # Return the BytesIO object instead of base64 string
133
 
134
- # Generate word cloud (return BytesIO directly)
135
  def generate_wordcloud(text):
136
  wordcloud = WordCloud(width=400, height=200, background_color="white").generate(text)
137
- buf = io.BytesIO()
138
- wordcloud.to_image().save(buf, format="PNG")
139
- buf.seek(0)
140
- return buf # Return the BytesIO object instead of base64 string
141
 
142
- # Generate QR code (return BytesIO directly)
143
  def generate_qr(url):
144
  qr = qrcode.QRCode(version=1, box_size=10, border=4)
145
  qr.add_data(url)
146
  qr.make(fit=True)
147
- img = qr.make_image(fill="black", back_color="white")
148
- buf = io.BytesIO()
149
- img.save(buf, format="PNG")
150
- buf.seek(0)
151
- return buf # Return the BytesIO object instead of base64 string
152
 
153
  # Export history with proper file handling
154
  def export_history():
@@ -230,6 +225,5 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as interface:
230
  theme_toggle.change(fn=toggle_theme, inputs=theme_toggle, outputs=theme_status)
231
  feedback_slider.change(fn=log_feedback, inputs=feedback_slider, outputs=feedback_output)
232
 
233
-
234
  # Launch the app
235
  interface.launch()
 
12
  from better_profanity import profanity
13
  import tempfile
14
  import os
15
+ from PIL import Image # Add PIL for image handling
16
 
17
  # Download NLTK data
18
  nltk.download('punkt', quiet=True)
 
113
  sample_text = "Sample X post from " + x_url
114
  return analyze_sentiment(sample_text, model_name, pos_words, neg_words, intensity, custom_model_path, source="X post")
115
 
116
+ # Generate timeline (return PIL Image)
117
  def generate_timeline():
118
  if not sentiment_scores:
119
  return None
 
129
  buf = io.BytesIO()
130
  plt.savefig(buf, format="png")
131
  buf.seek(0)
132
+ img = Image.open(buf) # Convert BytesIO to PIL Image
133
  plt.close()
134
+ return img
135
 
136
+ # Generate word cloud (return PIL Image directly)
137
  def generate_wordcloud(text):
138
  wordcloud = WordCloud(width=400, height=200, background_color="white").generate(text)
139
+ return wordcloud.to_image() # Directly return PIL Image
 
 
 
140
 
141
+ # Generate QR code (return PIL Image)
142
  def generate_qr(url):
143
  qr = qrcode.QRCode(version=1, box_size=10, border=4)
144
  qr.add_data(url)
145
  qr.make(fit=True)
146
+ return qr.make_image(fill="black", back_color="white") # Directly return PIL Image
 
 
 
 
147
 
148
  # Export history with proper file handling
149
  def export_history():
 
225
  theme_toggle.change(fn=toggle_theme, inputs=theme_toggle, outputs=theme_status)
226
  feedback_slider.change(fn=log_feedback, inputs=feedback_slider, outputs=feedback_output)
227
 
 
228
  # Launch the app
229
  interface.launch()