pentarosarium commited on
Commit
3ee8d61
1 Parent(s): 810b0fe
Files changed (2) hide show
  1. app.py +58 -21
  2. sample_file.xlsx +0 -0
app.py CHANGED
@@ -532,7 +532,7 @@ def create_interface():
532
  control = ProcessControl()
533
 
534
  with gr.Blocks(theme=gr.themes.Soft()) as app:
535
- gr.Markdown("# AI-анализ мониторинга новостей v.1.27")
536
 
537
  with gr.Row():
538
  file_input = gr.File(
@@ -575,24 +575,33 @@ def create_interface():
575
  with gr.Column(scale=1):
576
  events_plot = gr.Plot(label="Распределение событий")
577
 
 
 
 
 
 
 
 
 
578
  def stop_processing():
579
  control.request_stop()
580
  return "Остановка обработки..."
581
-
582
- @spaces.GPU(duration=300) # 5 minutes duration for the entire analysis
583
  def analyze(file_bytes):
584
  if file_bytes is None:
585
  gr.Warning("Пожалуйста, загрузите файл")
586
- return None, None, None, "Ожидание файла..."
587
 
588
  try:
589
- # Reset control and initialize detector
590
  control.reset()
591
- detector = EventDetector()
592
 
593
  file_obj = io.BytesIO(file_bytes)
594
  logger.info("File loaded into BytesIO successfully")
595
 
 
 
596
  # Read and deduplicate data
597
  df = pd.read_excel(file_obj, sheet_name='Публикации')
598
  original_count = len(df)
@@ -605,6 +614,19 @@ def create_interface():
605
 
606
  for batch_start in range(0, total, batch_size):
607
  if control.should_stop():
 
 
 
 
 
 
 
 
 
 
 
 
 
608
  break
609
 
610
  batch_end = min(batch_start + batch_size, total)
@@ -630,50 +652,65 @@ def create_interface():
630
  'Reasoning': results['reasoning'],
631
  'Event_Type': results['event_type'],
632
  'Event_Summary': results['event_summary'],
633
- 'Текст': text[:1000]
634
  })
635
 
636
  except Exception as e:
637
  logger.error(f"Error processing row {idx}: {str(e)}")
638
  continue
639
 
640
- # Create intermediate results
641
  if processed_rows:
642
  result_df = pd.DataFrame(processed_rows)
643
- fig_sentiment, fig_events = create_visualizations(result_df)
644
- yield (
645
- result_df,
646
- fig_sentiment,
647
- fig_events,
648
- f"Обработано {len(processed_rows)}/{total} строк"
649
- )
 
 
 
650
 
651
  # Cleanup GPU resources after batch
652
  torch.cuda.empty_cache()
653
  time.sleep(2)
654
 
 
655
  if processed_rows:
656
  final_df = pd.DataFrame(processed_rows)
657
- fig_sentiment, fig_events = create_visualizations(final_df)
658
- return final_df, fig_sentiment, fig_events, "Обработка завершена!"
 
 
 
 
 
 
 
 
659
  else:
660
- return None, None, None, "Нет обработанных данных"
661
 
662
  except Exception as e:
663
  error_msg = f"Ошибка анализа: {str(e)}"
664
  logger.error(error_msg)
665
  gr.Error(error_msg)
666
- return None, None, None, error_msg
 
 
 
667
 
668
  stop_btn.click(fn=stop_processing, outputs=[progress])
669
  analyze_btn.click(
670
  fn=analyze,
671
  inputs=[file_input],
672
- outputs=[stats, sentiment_plot, events_plot, progress]
673
  )
674
 
675
  return app
676
 
677
  if __name__ == "__main__":
678
  app = create_interface()
679
- app.launch(share=True)
 
532
  control = ProcessControl()
533
 
534
  with gr.Blocks(theme=gr.themes.Soft()) as app:
535
+ gr.Markdown("# AI-анализ мониторинга новостей v.1.28")
536
 
537
  with gr.Row():
538
  file_input = gr.File(
 
575
  with gr.Column(scale=1):
576
  events_plot = gr.Plot(label="Распределение событий")
577
 
578
+ # Add download button to UI
579
+ with gr.Row():
580
+ download_file = gr.File(
581
+ label="📥 Скачать полный отчет",
582
+ file_types=[".xlsx"],
583
+ interactive=False
584
+ )
585
+
586
  def stop_processing():
587
  control.request_stop()
588
  return "Остановка обработки..."
589
+
590
+ @spaces.GPU(duration=300)
591
  def analyze(file_bytes):
592
  if file_bytes is None:
593
  gr.Warning("Пожалуйста, загрузите файл")
594
+ return None, None, None, None, "Ожидание файла..."
595
 
596
  try:
597
+ # Reset stop flag
598
  control.reset()
 
599
 
600
  file_obj = io.BytesIO(file_bytes)
601
  logger.info("File loaded into BytesIO successfully")
602
 
603
+ detector = EventDetector()
604
+
605
  # Read and deduplicate data
606
  df = pd.read_excel(file_obj, sheet_name='Публикации')
607
  original_count = len(df)
 
614
 
615
  for batch_start in range(0, total, batch_size):
616
  if control.should_stop():
617
+ # Create partial results if stopped
618
+ if processed_rows:
619
+ result_df = pd.DataFrame(processed_rows)
620
+ output = create_output_file(result_df, file_obj)
621
+ if output:
622
+ fig_sentiment, fig_events = create_visualizations(result_df)
623
+ return (
624
+ result_df,
625
+ fig_sentiment,
626
+ fig_events,
627
+ (output, f"partial_results_{len(processed_rows)}_rows.xlsx"),
628
+ f"Обработка остановлена. Обработано {len(processed_rows)}/{total} строк"
629
+ )
630
  break
631
 
632
  batch_end = min(batch_start + batch_size, total)
 
652
  'Reasoning': results['reasoning'],
653
  'Event_Type': results['event_type'],
654
  'Event_Summary': results['event_summary'],
655
+ 'Выдержки из текста': text[:1000]
656
  })
657
 
658
  except Exception as e:
659
  logger.error(f"Error processing row {idx}: {str(e)}")
660
  continue
661
 
662
+ # Create intermediate results and yield
663
  if processed_rows:
664
  result_df = pd.DataFrame(processed_rows)
665
+ output = create_output_file(result_df, file_obj)
666
+ if output:
667
+ fig_sentiment, fig_events = create_visualizations(result_df)
668
+ yield (
669
+ result_df,
670
+ fig_sentiment,
671
+ fig_events,
672
+ (output, f"results_{len(processed_rows)}_rows.xlsx"),
673
+ f"Обработано {len(processed_rows)}/{total} строк"
674
+ )
675
 
676
  # Cleanup GPU resources after batch
677
  torch.cuda.empty_cache()
678
  time.sleep(2)
679
 
680
+ # Create final results
681
  if processed_rows:
682
  final_df = pd.DataFrame(processed_rows)
683
+ output = create_output_file(final_df, file_obj)
684
+ if output:
685
+ fig_sentiment, fig_events = create_visualizations(final_df)
686
+ return (
687
+ final_df,
688
+ fig_sentiment,
689
+ fig_events,
690
+ (output, "final_results.xlsx"),
691
+ "Обработка завершена!"
692
+ )
693
  else:
694
+ return None, None, None, None, "Нет обработанных данных"
695
 
696
  except Exception as e:
697
  error_msg = f"Ошибка анализа: {str(e)}"
698
  logger.error(error_msg)
699
  gr.Error(error_msg)
700
+ return None, None, None, None, error_msg
701
+ finally:
702
+ if detector:
703
+ detector.cleanup()
704
 
705
  stop_btn.click(fn=stop_processing, outputs=[progress])
706
  analyze_btn.click(
707
  fn=analyze,
708
  inputs=[file_input],
709
+ outputs=[stats, sentiment_plot, events_plot, download_file, progress]
710
  )
711
 
712
  return app
713
 
714
  if __name__ == "__main__":
715
  app = create_interface()
716
+ app.launch(share=True)
sample_file.xlsx ADDED
Binary file (139 kB). View file