pentarosarium commited on
Commit
2ce3a78
·
1 Parent(s): 49b8374
Files changed (1) hide show
  1. app.py +73 -89
app.py CHANGED
@@ -645,7 +645,7 @@ def create_interface():
645
  control = ProcessControl()
646
 
647
  with gr.Blocks(theme=gr.themes.Soft()) as app:
648
- gr.Markdown("# AI-анализ мониторинга новостей v.1.38")
649
 
650
  with gr.Row():
651
  file_input = gr.File(
@@ -686,7 +686,8 @@ def create_interface():
686
  stats = gr.DataFrame(
687
  label="Результаты анализа",
688
  interactive=False,
689
- wrap=True
 
690
  )
691
 
692
  with gr.Row():
@@ -694,29 +695,29 @@ def create_interface():
694
  sentiment_plot = gr.Plot(label="Распределение тональности")
695
  with gr.Column(scale=1):
696
  events_plot = gr.Plot(label="Распределение событий")
697
- # Add download button with initial state of invisible
 
 
 
 
 
698
 
699
- download_button = gr.Button("📥 Скачать результаты", visible=False)
700
- file_output = gr.File(label="Результаты анализа", visible=False)
701
-
702
-
703
  def stop_processing():
704
  control.request_stop()
705
  return "Остановка обработки..."
706
-
707
- @spaces.GPU(duration=300)
708
  def process_and_download(file_bytes):
709
- """Process file and return download button state"""
710
  if file_bytes is None:
711
  gr.Warning("Пожалуйста, загрузите файл")
712
- return {
713
- "status": "Ожидание файла...",
714
- "dedup_message": "",
715
- "dataframe": None,
716
- "sentiment_plot": None,
717
- "events_plot": None,
718
- "download": None
719
- }
720
 
721
  try:
722
  file_obj = io.BytesIO(file_bytes)
@@ -735,6 +736,8 @@ def create_interface():
735
  processed_rows = []
736
  total = len(df)
737
  batch_size = 3
 
 
738
 
739
  for batch_start in range(0, total, batch_size):
740
  if control.should_stop():
@@ -785,103 +788,84 @@ def create_interface():
785
  continue
786
 
787
  # Create intermediate results
788
- if processed_rows:
789
- result_df = pd.DataFrame(processed_rows)
790
- output_bytes_io = create_output_file(result_df, file_obj)
791
- if output_bytes_io:
792
- fig_sentiment, fig_events = create_visualizations(result_df)
793
- yield (
794
- result_df,
795
- fig_sentiment,
796
- fig_events,
797
- True,
798
- (f"results_{len(processed_rows)}_rows.xlsx", output_bytes_io.getvalue()), # Return tuple of (name, bytes)
799
- f"Обработано {len(processed_rows)}/{total} строк",
800
- dedup_message
801
- )
802
-
803
- # Cleanup GPU resources after batch
804
- torch.cuda.empty_cache()
805
- time.sleep(2)
806
-
807
-
808
- # Create final results
809
  if processed_rows:
810
  result_df = pd.DataFrame(processed_rows)
811
- output_bytes_io = create_output_file(result_df, file_obj)
812
  fig_sentiment, fig_events = create_visualizations(result_df)
813
 
814
- # Create download component
815
- download = gr.Button("📥 Скачать результаты")
816
-
817
- @download.click
818
- def on_download_click():
819
- return {
820
- "name": "результаты_анализа.xlsx",
821
- "data": output_bytes_io.getvalue(),
822
- "mime": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
823
- }
824
-
825
- return {
826
- "status": "Обработка завершена!",
827
- "dedup_message": dedup_message,
828
- "dataframe": result_df,
829
- "sentiment_plot": fig_sentiment,
830
- "events_plot": fig_events,
831
- "download": download
832
- }
833
- else:
834
- return {
835
- "status": "Нет обработанных данных",
836
- "dedup_message": dedup_message,
837
- "dataframe": None,
838
- "sentiment_plot": None,
839
- "events_plot": None,
840
- "download": None
841
- }
842
 
843
  except Exception as e:
844
  error_msg = f"Ошибка анализа: {str(e)}"
845
  logger.error(error_msg)
846
  gr.Error(error_msg)
847
- return {
848
- "status": error_msg,
849
- "dedup_message": "",
850
- "dataframe": None,
851
- "sentiment_plot": None,
852
- "events_plot": None,
853
- "download": None
854
- }
855
  finally:
856
  if detector:
857
  detector.cleanup()
858
 
859
- def handle_file_download(file_bytes):
860
- """Handle file download when button is clicked"""
861
- if file_bytes is not None:
862
- return (file_bytes, "результаты_анализа.xlsx")
863
- return None
864
-
 
 
 
 
865
  stop_btn.click(fn=stop_processing, outputs=[progress])
 
866
  analyze_btn.click(
867
  fn=process_and_download,
868
  inputs=[file_input],
869
  outputs=[
870
- progress,
871
- status_box,
872
  stats,
873
  sentiment_plot,
874
  events_plot,
875
- download_button # This will update dynamically
 
 
876
  ]
877
  )
878
 
879
  download_button.click(
880
- fn=handle_file_download,
881
- inputs=[file_output],
882
- outputs=[file_output]
883
  )
884
-
885
  return app
886
 
887
  if __name__ == "__main__":
 
645
  control = ProcessControl()
646
 
647
  with gr.Blocks(theme=gr.themes.Soft()) as app:
648
+ gr.Markdown("# AI-анализ мониторинга новостей v.1.41")
649
 
650
  with gr.Row():
651
  file_input = gr.File(
 
686
  stats = gr.DataFrame(
687
  label="Результаты анализа",
688
  interactive=False,
689
+ wrap=True,
690
+ max_rows=20
691
  )
692
 
693
  with gr.Row():
 
695
  sentiment_plot = gr.Plot(label="Распределение тональности")
696
  with gr.Column(scale=1):
697
  events_plot = gr.Plot(label="Распределение событий")
698
+
699
+ with gr.Row():
700
+ download_button = gr.Button(
701
+ "📥 Скачать результаты",
702
+ visible=False
703
+ )
704
 
 
 
 
 
705
  def stop_processing():
706
  control.request_stop()
707
  return "Остановка обработки..."
708
+
709
+ @spaces.GPU(duration=300)
710
  def process_and_download(file_bytes):
 
711
  if file_bytes is None:
712
  gr.Warning("Пожалуйста, загрузите файл")
713
+ return (
714
+ pd.DataFrame(), # empty dataframe for stats
715
+ None, # sentiment plot
716
+ None, # events plot
717
+ gr.update(visible=False), # download button visibility
718
+ "Ожидание файла...", # progress message
719
+ "" # dedup message
720
+ )
721
 
722
  try:
723
  file_obj = io.BytesIO(file_bytes)
 
736
  processed_rows = []
737
  total = len(df)
738
  batch_size = 3
739
+
740
+
741
 
742
  for batch_start in range(0, total, batch_size):
743
  if control.should_stop():
 
788
  continue
789
 
790
  # Create intermediate results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
791
  if processed_rows:
792
  result_df = pd.DataFrame(processed_rows)
 
793
  fig_sentiment, fig_events = create_visualizations(result_df)
794
 
795
+ # Save the file and store bytes in session state for later download
796
+ output_bytes_io = create_output_file(result_df, file_obj)
797
+ if output_bytes_io:
798
+ # Store in session state for download
799
+ if not hasattr(st.session_state, 'download_data'):
800
+ st.session_state.download_data = {}
801
+ st.session_state.download_data['file'] = output_bytes_io.getvalue()
802
+
803
+ # Return results
804
+ return (
805
+ result_df, # stats dataframe
806
+ fig_sentiment, # sentiment plot
807
+ fig_events, # events plot
808
+ gr.update(visible=True), # show download button
809
+ "Обработка завершена!", # progress message
810
+ dedup_message # dedup message
811
+ )
812
+
813
+ return (
814
+ pd.DataFrame(), # empty dataframe
815
+ None, # sentiment plot
816
+ None, # events plot
817
+ gr.update(visible=False), # hide download button
818
+ "Нет обработанных данных", # progress message
819
+ dedup_message # dedup message
820
+ )
 
 
821
 
822
  except Exception as e:
823
  error_msg = f"Ошибка анализа: {str(e)}"
824
  logger.error(error_msg)
825
  gr.Error(error_msg)
826
+ return (
827
+ pd.DataFrame(),
828
+ None,
829
+ None,
830
+ gr.update(visible=False),
831
+ error_msg,
832
+ ""
833
+ )
834
  finally:
835
  if detector:
836
  detector.cleanup()
837
 
838
+ def handle_download():
839
+ """Download handler for the button"""
840
+ try:
841
+ if hasattr(st.session_state, 'download_data') and st.session_state.download_data.get('file'):
842
+ return st.session_state.download_data['file']
843
+ return None
844
+ except Exception as e:
845
+ logger.error(f"Download error: {str(e)}")
846
+ return None
847
+
848
  stop_btn.click(fn=stop_processing, outputs=[progress])
849
+
850
  analyze_btn.click(
851
  fn=process_and_download,
852
  inputs=[file_input],
853
  outputs=[
 
 
854
  stats,
855
  sentiment_plot,
856
  events_plot,
857
+ download_button,
858
+ progress,
859
+ status_box
860
  ]
861
  )
862
 
863
  download_button.click(
864
+ fn=handle_download,
865
+ inputs=[],
866
+ outputs=[gr.File(label="", visible=False)]
867
  )
868
+
869
  return app
870
 
871
  if __name__ == "__main__":