pentarosarium commited on
Commit
e853c54
·
1 Parent(s): 8075c83
Files changed (1) hide show
  1. app.py +44 -40
app.py CHANGED
@@ -643,10 +643,12 @@ def create_output_file(df, uploaded_file):
643
 
644
  def create_interface():
645
  control = ProcessControl()
646
- file_data = gr.State(value=None)
647
-
648
  with gr.Blocks(theme=gr.themes.Soft()) as app:
649
- gr.Markdown("# AI-анализ мониторинга новостей v.1.44")
 
 
 
650
 
651
  with gr.Row():
652
  file_input = gr.File(
@@ -687,7 +689,8 @@ def create_interface():
687
  stats = gr.DataFrame(
688
  label="Результаты анализа",
689
  interactive=False,
690
- wrap=True
 
691
  )
692
 
693
  with gr.Row():
@@ -696,17 +699,18 @@ def create_interface():
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
  file_output = gr.File(
706
- label="Скачать результаты",
707
- visible=False
 
708
  )
709
-
710
  def stop_processing():
711
  control.request_stop()
712
  return "Остановка обработки..."
@@ -716,12 +720,13 @@ def create_interface():
716
  if file_bytes is None:
717
  gr.Warning("Пожалуйста, загрузите файл")
718
  return (
719
- pd.DataFrame(), # empty dataframe for stats
720
- None, # sentiment plot
721
- None, # events plot
722
- gr.update(visible=False), # download button visibility
723
- "Ожидание файла...", # progress message
724
- "" # dedup message
 
725
  )
726
 
727
  try:
@@ -798,27 +803,27 @@ def create_interface():
798
  result_df = pd.DataFrame(processed_rows)
799
  output_bytes_io = create_output_file(result_df, file_obj)
800
  fig_sentiment, fig_events = create_visualizations(result_df)
801
-
802
  if output_bytes_io:
803
- output_bytes = output_bytes_io.getvalue()
804
  return (
805
  result_df,
806
  fig_sentiment,
807
  fig_events,
808
  gr.update(visible=True),
809
- f"Обработано {len(processed_rows)}/{total} строк",
810
- dedup_message,
811
- (output_bytes, "результаты_анализа.xlsx")
812
  )
813
 
814
  return (
815
- pd.DataFrame(), # empty dataframe
816
- None, # sentiment plot
817
- None, # events plot
818
- gr.update(visible=False), # hide download button
819
- "Нет обработанных данных", # progress message
820
- dedup_message, # dedup message
821
- None
822
  )
823
 
824
  except Exception as e:
@@ -830,22 +835,21 @@ def create_interface():
830
  None,
831
  None,
832
  gr.update(visible=False),
 
833
  error_msg,
834
- "",
835
- None
836
  )
837
  finally:
838
  if detector:
839
  detector.cleanup()
840
 
841
- def handle_download(file_data):
842
- """Handle file download when button is clicked"""
843
- if file_data is not None:
844
- return file_data
845
- return None
846
 
847
  stop_btn.click(fn=stop_processing, outputs=[progress])
848
 
 
849
  analyze_btn.click(
850
  fn=process_and_download,
851
  inputs=[file_input],
@@ -853,16 +857,16 @@ def create_interface():
853
  stats,
854
  sentiment_plot,
855
  events_plot,
856
- download_button,
 
857
  progress,
858
- status_box,
859
- file_data
860
  ]
861
  )
862
 
863
- download_button.click(
864
- fn=handle_download,
865
- inputs=[file_data],
866
  outputs=[file_output]
867
  )
868
 
 
643
 
644
  def create_interface():
645
  control = ProcessControl()
646
+
 
647
  with gr.Blocks(theme=gr.themes.Soft()) as app:
648
+ # Create state for file data
649
+ current_file = gr.State(None)
650
+
651
+ gr.Markdown("# AI-анализ мониторинга новостей v.1.45")
652
 
653
  with gr.Row():
654
  file_input = gr.File(
 
689
  stats = gr.DataFrame(
690
  label="Результаты анализа",
691
  interactive=False,
692
+ wrap=True,
693
+ max_rows=20
694
  )
695
 
696
  with gr.Row():
 
699
  with gr.Column(scale=1):
700
  events_plot = gr.Plot(label="Распределение событий")
701
 
702
+ # Create a download row with both button and file components
703
  with gr.Row():
704
+ download_btn = gr.Button(
705
  "📥 Скачать результаты",
706
  visible=False
707
  )
 
708
  file_output = gr.File(
709
+ label="",
710
+ visible=False,
711
+ interactive=False
712
  )
713
+
714
  def stop_processing():
715
  control.request_stop()
716
  return "Остановка обработки..."
 
720
  if file_bytes is None:
721
  gr.Warning("Пожалуйста, загрузите файл")
722
  return (
723
+ pd.DataFrame(),
724
+ None,
725
+ None,
726
+ gr.update(visible=False),
727
+ None,
728
+ "Ожидание файла...",
729
+ ""
730
  )
731
 
732
  try:
 
803
  result_df = pd.DataFrame(processed_rows)
804
  output_bytes_io = create_output_file(result_df, file_obj)
805
  fig_sentiment, fig_events = create_visualizations(result_df)
806
+
807
  if output_bytes_io:
808
+ # Return values in the correct order
809
  return (
810
  result_df,
811
  fig_sentiment,
812
  fig_events,
813
  gr.update(visible=True),
814
+ output_bytes_io.getvalue(),
815
+ "Обработка завершена!",
816
+ dedup_message
817
  )
818
 
819
  return (
820
+ pd.DataFrame(),
821
+ None,
822
+ None,
823
+ gr.update(visible=False),
824
+ None,
825
+ "Нет обработанных данных",
826
+ dedup_message
827
  )
828
 
829
  except Exception as e:
 
835
  None,
836
  None,
837
  gr.update(visible=False),
838
+ None,
839
  error_msg,
840
+ ""
 
841
  )
842
  finally:
843
  if detector:
844
  detector.cleanup()
845
 
846
+ def update_file_output():
847
+ """Handler for download button click"""
848
+ return gr.update(visible=True, value=("результаты_анализа.xlsx", current_file.value))
 
 
849
 
850
  stop_btn.click(fn=stop_processing, outputs=[progress])
851
 
852
+ # Main processing
853
  analyze_btn.click(
854
  fn=process_and_download,
855
  inputs=[file_input],
 
857
  stats,
858
  sentiment_plot,
859
  events_plot,
860
+ download_btn,
861
+ current_file, # State for file data
862
  progress,
863
+ status_box
 
864
  ]
865
  )
866
 
867
+ # Download handling
868
+ download_btn.click(
869
+ fn=update_file_output,
870
  outputs=[file_output]
871
  )
872