Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -287,35 +287,55 @@ def fDistancePlot(text2Party):
|
|
| 287 |
plt.tight_layout()
|
| 288 |
return safe_plot(plot_func)
|
| 289 |
|
| 290 |
-
# --- Updated DispersionPlot without passing 'ax' ---
|
| 291 |
def DispersionPlot(textParty):
|
| 292 |
"""Generates the word dispersion plot."""
|
| 293 |
buf = None # Initialize buffer
|
| 294 |
try:
|
| 295 |
word_tokens_party = word_tokenize(textParty)
|
|
|
|
| 296 |
if not word_tokens_party:
|
| 297 |
print("Warning: No tokens found for dispersion plot.")
|
| 298 |
return None
|
| 299 |
|
| 300 |
moby = Text(word_tokens_party)
|
| 301 |
fdistance = FreqDist(word_tokens_party)
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
else:
|
| 307 |
-
word_Lst = [
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
buf = BytesIO()
|
| 321 |
# Handle potential apply_aspect error for dispersion plot
|
|
@@ -340,9 +360,11 @@ def DispersionPlot(textParty):
|
|
| 340 |
buf.close() # Ensure buffer is closed on error
|
| 341 |
traceback.print_exc()
|
| 342 |
plt.close('all') # Aggressive close on error
|
|
|
|
| 343 |
return None # Return None on error
|
| 344 |
|
| 345 |
-
|
|
|
|
| 346 |
def word_cloud_generator(parsed_text_name, text_Party):
|
| 347 |
"""Generates the word cloud image."""
|
| 348 |
buf = None # Initialize buffer
|
|
@@ -362,14 +384,14 @@ def word_cloud_generator(parsed_text_name, text_Party):
|
|
| 362 |
elif 'aap' in filename_lower:
|
| 363 |
mask_path = 'aapMain2.jpg'
|
| 364 |
|
| 365 |
-
|
| 366 |
if text_Party.strip() == "":
|
| 367 |
raise ValueError("Text for word cloud is empty")
|
| 368 |
|
| 369 |
# Generate word cloud object
|
| 370 |
if mask_path and os.path.exists(mask_path):
|
| 371 |
orgImg = Image.open(mask_path)
|
| 372 |
-
|
| 373 |
if orgImg.mode != 'RGB':
|
| 374 |
orgImg = orgImg.convert('RGB')
|
| 375 |
mask = np.array(orgImg)
|
|
@@ -505,7 +527,6 @@ with gr.Blocks(title='Manifesto Analysis') as demo:
|
|
| 505 |
dispersion_output = gr.Image(label='Dispersion Plot', interactive=False, height=400) # Adjust height as needed
|
| 506 |
|
| 507 |
# --- Link Button Click to Function and Outputs ---
|
| 508 |
-
# Ensure the order of outputs matches the function return order
|
| 509 |
submit_btn.click(
|
| 510 |
fn=analysis,
|
| 511 |
inputs=[file_input, search_input],
|
|
@@ -534,6 +555,6 @@ with gr.Blocks(title='Manifesto Analysis') as demo:
|
|
| 534 |
fn=analysis # Run analysis on example click
|
| 535 |
)
|
| 536 |
|
| 537 |
-
|
| 538 |
if __name__ == "__main__":
|
| 539 |
demo.launch(debug=True, share=False, show_error=True)
|
|
|
|
| 287 |
plt.tight_layout()
|
| 288 |
return safe_plot(plot_func)
|
| 289 |
|
|
|
|
| 290 |
def DispersionPlot(textParty):
|
| 291 |
"""Generates the word dispersion plot."""
|
| 292 |
buf = None # Initialize buffer
|
| 293 |
try:
|
| 294 |
word_tokens_party = word_tokenize(textParty)
|
| 295 |
+
print(f"Debug DispersionPlot: Total tokens: {len(word_tokens_party)}") # Debug print
|
| 296 |
if not word_tokens_party:
|
| 297 |
print("Warning: No tokens found for dispersion plot.")
|
| 298 |
return None
|
| 299 |
|
| 300 |
moby = Text(word_tokens_party)
|
| 301 |
fdistance = FreqDist(word_tokens_party)
|
| 302 |
+
print(f"Debug DispersionPlot: FreqDist sample: {list(fdistance.most_common(10))}") # Debug print
|
| 303 |
+
|
| 304 |
+
# --- Improved word selection logic ---
|
| 305 |
+
# Get common words, excluding very short words which might be punctuation/non-informative
|
| 306 |
+
# or words that dispersion_plot might have trouble with.
|
| 307 |
+
common_words_raw = fdistance.most_common(15) # Check a few more common words
|
| 308 |
+
# Filter: length > 2, isalpha (to avoid punctuation), not just digits
|
| 309 |
+
common_words_filtered = [(word, freq) for word, freq in common_words_raw if len(word) > 2 and word.isalpha() and not word.isdigit()]
|
| 310 |
+
print(f"Debug DispersionPlot: Filtered common words: {common_words_filtered}") # Debug print
|
| 311 |
+
|
| 312 |
+
# Select top 5 from filtered list
|
| 313 |
+
if len(common_words_filtered) < 5:
|
| 314 |
+
word_Lst = [word for word, _ in common_words_filtered]
|
| 315 |
else:
|
| 316 |
+
word_Lst = [common_words_filtered[x][0] for x in range(5)]
|
| 317 |
+
|
| 318 |
+
# Final check: Ensure words are present in the Text object (moby)
|
| 319 |
+
# This is crucial for dispersion_plot
|
| 320 |
+
final_word_list = [word for word in word_Lst if word in moby] # Check membership in the Text object
|
| 321 |
+
print(f"Debug DispersionPlot: Final word list for plot: {final_word_list}") # Debug print
|
| 322 |
+
|
| 323 |
+
if not final_word_list:
|
| 324 |
+
print("Warning: No suitable words found for dispersion plot after filtering and checking membership.")
|
| 325 |
+
# Create a simple plot indicating no data
|
| 326 |
+
fig, ax = plt.subplots(figsize=(8, 3))
|
| 327 |
+
ax.text(0.5, 0.5, "No suitable words found for dispersion plot", ha='center', va='center', transform=ax.transAxes)
|
| 328 |
+
ax.set_xlim(0, 1)
|
| 329 |
+
ax.set_ylim(0, 1)
|
| 330 |
+
ax.axis('off') # Hide axes for the message
|
| 331 |
+
fig.suptitle('Dispersion Plot')
|
| 332 |
+
else:
|
| 333 |
+
# --- Manage figure explicitly without passing 'ax' ---
|
| 334 |
+
fig = plt.figure(figsize=(10, 5)) # Create figure explicitly
|
| 335 |
+
plt.title('Dispersion Plot')
|
| 336 |
+
# Call dispersion_plot with the verified word list
|
| 337 |
+
moby.dispersion_plot(final_word_list)
|
| 338 |
+
plt.tight_layout()
|
| 339 |
|
| 340 |
buf = BytesIO()
|
| 341 |
# Handle potential apply_aspect error for dispersion plot
|
|
|
|
| 360 |
buf.close() # Ensure buffer is closed on error
|
| 361 |
traceback.print_exc()
|
| 362 |
plt.close('all') # Aggressive close on error
|
| 363 |
+
# Optionally return a placeholder image or None
|
| 364 |
return None # Return None on error
|
| 365 |
|
| 366 |
+
|
| 367 |
+
|
| 368 |
def word_cloud_generator(parsed_text_name, text_Party):
|
| 369 |
"""Generates the word cloud image."""
|
| 370 |
buf = None # Initialize buffer
|
|
|
|
| 384 |
elif 'aap' in filename_lower:
|
| 385 |
mask_path = 'aapMain2.jpg'
|
| 386 |
|
| 387 |
+
|
| 388 |
if text_Party.strip() == "":
|
| 389 |
raise ValueError("Text for word cloud is empty")
|
| 390 |
|
| 391 |
# Generate word cloud object
|
| 392 |
if mask_path and os.path.exists(mask_path):
|
| 393 |
orgImg = Image.open(mask_path)
|
| 394 |
+
|
| 395 |
if orgImg.mode != 'RGB':
|
| 396 |
orgImg = orgImg.convert('RGB')
|
| 397 |
mask = np.array(orgImg)
|
|
|
|
| 527 |
dispersion_output = gr.Image(label='Dispersion Plot', interactive=False, height=400) # Adjust height as needed
|
| 528 |
|
| 529 |
# --- Link Button Click to Function and Outputs ---
|
|
|
|
| 530 |
submit_btn.click(
|
| 531 |
fn=analysis,
|
| 532 |
inputs=[file_input, search_input],
|
|
|
|
| 555 |
fn=analysis # Run analysis on example click
|
| 556 |
)
|
| 557 |
|
| 558 |
+
|
| 559 |
if __name__ == "__main__":
|
| 560 |
demo.launch(debug=True, share=False, show_error=True)
|