Sa-m commited on
Commit
e0441b6
·
verified ·
1 Parent(s): 11b6240

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -4
app.py CHANGED
@@ -455,28 +455,39 @@ def analysis(Manifesto, Search):
455
  Search: Search term entered by the user
456
  '''
457
  try:
 
 
458
  # Check if a file was uploaded
459
  if Manifesto is None:
 
460
  return "Please upload a PDF file", {}, None, None, None, None, None, "No file uploaded"
461
 
462
  # Handle empty search term
463
  if Search is None or Search.strip() == "":
464
  Search = "government" # Default search term
 
 
 
465
 
466
  # Process the uploaded PDF
 
467
  raw_party = Parsing(Manifesto)
468
 
469
  # Check if parsing was successful
470
- if raw_party.startswith("Error"):
 
471
  return raw_party, {}, None, None, None, None, None, "Error generating summary due to parsing failure"
472
-
 
473
  text_Party = clean_text(raw_party)
474
  text_Party_processed = Preprocess(text_Party)
475
 
476
  # Generate summary using LLM
 
477
  summary = generate_summary(raw_party)
478
 
479
  # Sentiment analysis
 
480
  df = pd.DataFrame(raw_party.split('\n'), columns=['Content'])
481
  df['Subjectivity'] = df['Content'].apply(getSubjectivity)
482
  df['Polarity'] = df['Content'].apply(getPolarity)
@@ -484,6 +495,7 @@ def analysis(Manifesto, Search):
484
  df['Analysis on Subjectivity'] = df['Subjectivity'].apply(getAnalysis)
485
 
486
  # Generate sentiment analysis plot
 
487
  plt.title('Sentiment Analysis')
488
  plt.xlabel('Sentiment')
489
  plt.ylabel('Counts')
@@ -497,6 +509,7 @@ def analysis(Manifesto, Search):
497
  plt.clf()
498
 
499
  # Generate subjectivity analysis plot
 
500
  plt.figure(figsize=(4,3))
501
  df['Analysis on Subjectivity'].value_counts().plot(kind ='bar',color="#B667F1")
502
  plt.tight_layout()
@@ -507,23 +520,31 @@ def analysis(Manifesto, Search):
507
  plt.clf()
508
 
509
  # Generate word cloud
 
510
  img3 = word_cloud_generator(Manifesto.name, text_Party_processed)
511
 
512
  # Generate frequency distribution and dispersion plots
 
513
  fdist_Party = fDistance(text_Party_processed)
514
  img4 = fDistancePlot(text_Party_processed)
 
 
515
  img5 = DispersionPlot(text_Party_processed)
516
 
517
  # Search for the term in the text
 
518
  searChRes = get_all_phases_containing_tar_wrd(Search, text_Party_processed)
519
  searChRes = searChRes.replace(Search, "\u0332".join(Search))
520
 
521
  plt.close('all')
 
522
  return searChRes, fdist_Party, img1, img2, img3, img4, img5, summary
523
 
524
  except Exception as e:
525
  error_message = f"Error analyzing manifesto: {str(e)}"
526
  print(error_message)
 
 
527
  # Return placeholder values in case of error
528
  return error_message, {}, None, None, None, None, None, "Error generating summary. Please check the console for details."
529
 
@@ -578,9 +599,16 @@ with gr.Blocks(title='Manifesto Analysis') as demo:
578
  submit_btn.click(
579
  fn=analysis,
580
  inputs=[file_input, search_input],
581
- outputs=[text, mfw, plot1, plot2, plot3, plot4, plot5, summary_output]
 
 
 
 
582
  )
583
 
 
 
 
584
  gr.Examples(
585
  examples=[
586
  ['Example/AAP_Manifesto_2019.pdf', 'government'],
@@ -590,7 +618,7 @@ with gr.Blocks(title='Manifesto Analysis') as demo:
590
  inputs=[file_input, search_input]
591
  )
592
 
593
- demo.launch(debug=True, share=False)
594
 
595
 
596
  # Old interface code replaced by the Blocks implementation above
 
455
  Search: Search term entered by the user
456
  '''
457
  try:
458
+ print(f"Analysis function called with: Manifesto={Manifesto}, Search={Search}")
459
+
460
  # Check if a file was uploaded
461
  if Manifesto is None:
462
+ print("No file uploaded")
463
  return "Please upload a PDF file", {}, None, None, None, None, None, "No file uploaded"
464
 
465
  # Handle empty search term
466
  if Search is None or Search.strip() == "":
467
  Search = "government" # Default search term
468
+ print(f"Using default search term: {Search}")
469
+ else:
470
+ print(f"Using provided search term: {Search}")
471
 
472
  # Process the uploaded PDF
473
+ print(f"Processing file: {Manifesto.name if hasattr(Manifesto, 'name') else Manifesto}")
474
  raw_party = Parsing(Manifesto)
475
 
476
  # Check if parsing was successful
477
+ if isinstance(raw_party, str) and raw_party.startswith("Error"):
478
+ print(f"Parsing error: {raw_party}")
479
  return raw_party, {}, None, None, None, None, None, "Error generating summary due to parsing failure"
480
+
481
+ print("Parsing successful, cleaning text...")
482
  text_Party = clean_text(raw_party)
483
  text_Party_processed = Preprocess(text_Party)
484
 
485
  # Generate summary using LLM
486
+ print("Generating summary...")
487
  summary = generate_summary(raw_party)
488
 
489
  # Sentiment analysis
490
+ print("Performing sentiment analysis...")
491
  df = pd.DataFrame(raw_party.split('\n'), columns=['Content'])
492
  df['Subjectivity'] = df['Content'].apply(getSubjectivity)
493
  df['Polarity'] = df['Content'].apply(getPolarity)
 
495
  df['Analysis on Subjectivity'] = df['Subjectivity'].apply(getAnalysis)
496
 
497
  # Generate sentiment analysis plot
498
+ print("Generating sentiment analysis plot...")
499
  plt.title('Sentiment Analysis')
500
  plt.xlabel('Sentiment')
501
  plt.ylabel('Counts')
 
509
  plt.clf()
510
 
511
  # Generate subjectivity analysis plot
512
+ print("Generating subjectivity analysis plot...")
513
  plt.figure(figsize=(4,3))
514
  df['Analysis on Subjectivity'].value_counts().plot(kind ='bar',color="#B667F1")
515
  plt.tight_layout()
 
520
  plt.clf()
521
 
522
  # Generate word cloud
523
+ print("Generating word cloud...")
524
  img3 = word_cloud_generator(Manifesto.name, text_Party_processed)
525
 
526
  # Generate frequency distribution and dispersion plots
527
+ print("Generating frequency distribution...")
528
  fdist_Party = fDistance(text_Party_processed)
529
  img4 = fDistancePlot(text_Party_processed)
530
+
531
+ print("Generating dispersion plot...")
532
  img5 = DispersionPlot(text_Party_processed)
533
 
534
  # Search for the term in the text
535
+ print(f"Searching for term: {Search}")
536
  searChRes = get_all_phases_containing_tar_wrd(Search, text_Party_processed)
537
  searChRes = searChRes.replace(Search, "\u0332".join(Search))
538
 
539
  plt.close('all')
540
+ print("Analysis completed successfully")
541
  return searChRes, fdist_Party, img1, img2, img3, img4, img5, summary
542
 
543
  except Exception as e:
544
  error_message = f"Error analyzing manifesto: {str(e)}"
545
  print(error_message)
546
+ import traceback
547
+ traceback.print_exc()
548
  # Return placeholder values in case of error
549
  return error_message, {}, None, None, None, None, None, "Error generating summary. Please check the console for details."
550
 
 
599
  submit_btn.click(
600
  fn=analysis,
601
  inputs=[file_input, search_input],
602
+ outputs=[text, mfw, plot1, plot2, plot3, plot4, plot5, summary_output],
603
+ _js="() => {
604
+ console.log('Button clicked!');
605
+ return [];
606
+ }"
607
  )
608
 
609
+ # Add a debug print to verify the button is connected
610
+ print("Button connected to analysis function")
611
+
612
  gr.Examples(
613
  examples=[
614
  ['Example/AAP_Manifesto_2019.pdf', 'government'],
 
618
  inputs=[file_input, search_input]
619
  )
620
 
621
+ demo.launch(debug=True, share=False, show_error=True)
622
 
623
 
624
  # Old interface code replaced by the Blocks implementation above