C2MV commited on
Commit
341ebec
·
verified ·
1 Parent(s): e0fbfa8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -51
app.py CHANGED
@@ -3,7 +3,6 @@ import pandas as pd
3
  import statsmodels.formula.api as smf
4
  import statsmodels.api as sm
5
  import plotly.graph_objects as go
6
- from plotly.subplots import make_subplots
7
  from scipy.optimize import minimize
8
  import plotly.express as px
9
  from scipy.stats import t, f
@@ -568,11 +567,13 @@ def load_data(x1_name, x2_name, x3_name, y_name, x1_levels_str, x2_levels_str, x
568
  return data.round(3), x1_name, x2_name, x3_name, y_name, x1_levels, x2_levels, x3_levels, gr.update(visible=True)
569
 
570
  except Exception as e:
571
- return None, "", "", "", "", [], [], [], gr.update(visible=False), f"Error: {e}"
 
 
572
 
573
  def fit_and_optimize_model():
574
  if 'rsm' not in globals():
575
- return None, None, None, None, None, None, None, None, None, gr.State(), gr.State()
576
 
577
  # Ajustar modelos y optimizar
578
  model_completo, pareto_completo = rsm.fit_model()
@@ -601,26 +602,23 @@ def fit_and_optimize_model():
601
  contribution_table,
602
  anova_table,
603
  rsm.all_figures, # Devuelve todas las figuras generadas
604
- gr.State() # Inicializa el índice actual
605
  )
606
 
607
- def generate_rsm_plot(all_figures, current_index):
608
  if not all_figures:
609
  return None, "No hay gráficos disponibles.", current_index
610
-
611
  selected_fig = all_figures[current_index]
612
  plot_info_text = f"Gráfico {current_index + 1} de {len(all_figures)}"
613
-
614
  return selected_fig, plot_info_text, current_index
615
 
616
- def navigate_plot(direction, current_index, total_plots, all_figures):
617
  """
618
  Navega entre los gráficos.
619
 
620
  Args:
621
  direction (str): 'left' o 'right'.
622
  current_index (int): Índice actual.
623
- total_plots (int): Total de gráficos.
624
  all_figures (list): Lista de todas las figuras.
625
 
626
  Returns:
@@ -630,14 +628,14 @@ def navigate_plot(direction, current_index, total_plots, all_figures):
630
  return None, "No hay gráficos disponibles.", current_index
631
 
632
  if direction == 'left':
633
- new_index = (current_index - 1) % total_plots
634
  elif direction == 'right':
635
- new_index = (current_index + 1) % total_plots
636
  else:
637
  new_index = current_index
638
 
639
  selected_fig = all_figures[new_index]
640
- plot_info_text = f"Gráfico {new_index + 1} de {total_plots}"
641
 
642
  return selected_fig, plot_info_text, new_index
643
 
@@ -762,13 +760,15 @@ with gr.Blocks() as demo:
762
  download_plot_button = gr.DownloadButton("Descargar Gráfico Actual (PNG)")
763
  download_all_plots_button = gr.DownloadButton("Descargar Todos los Gráficos (ZIP)")
764
  current_index_state = gr.State(0) # Estado para el índice actual
765
-
 
766
  load_button.click(
767
  load_data,
768
  inputs=[x1_name_input, x2_name_input, x3_name_input, y_name_input, x1_levels_input, x2_levels_input, x3_levels_input, data_input],
769
  outputs=[data_output, x1_name_input, x2_name_input, x3_name_input, y_name_input, x1_levels_input, x2_levels_input, x3_levels_input, analysis_row]
770
  )
771
 
 
772
  fit_button.click(
773
  fit_and_optimize_model,
774
  inputs=[],
@@ -783,75 +783,48 @@ with gr.Blocks() as demo:
783
  contribution_table_output,
784
  anova_table_output,
785
  gr.State(), # all_figures
786
- gr.State() # current_index (reset)
787
  ]
788
  )
789
 
 
790
  plot_button.click(
791
- lambda: (None, "Gráfico 1 de 9", 0), # Inicializar plot y índice
792
- inputs=[],
793
  outputs=[rsm_plot_output, plot_info, current_index_state]
794
  )
795
 
796
  # Navegación de gráficos
797
  left_button.click(
798
- navigate_plot,
799
- inputs=["left", current_index_state, gr.Number(value=9), gr.State()],
800
  outputs=[rsm_plot_output, plot_info, current_index_state]
801
  )
802
  right_button.click(
803
- navigate_plot,
804
- inputs=["right", current_index_state, gr.Number(value=9), gr.State()],
805
  outputs=[rsm_plot_output, plot_info, current_index_state]
806
  )
807
 
808
- # Función para actualizar y mostrar el gráfico actual
809
- def update_plot(direction, current_index, total_plots, all_figures):
810
- return navigate_plot(direction, current_index, int(total_plots), all_figures)
811
-
812
  # Descargar gráfico actual
813
  download_plot_button.click(
814
  download_current_plot,
815
  inputs=[gr.State(), current_index_state],
816
- outputs=[download_plot_button]
817
  )
818
 
819
  # Descargar todos los gráficos en ZIP
820
  download_all_plots_button.click(
821
  download_all_plots_zip,
822
  inputs=[gr.State()],
823
- outputs=[download_all_plots_button]
824
  )
825
 
826
  # Descargar todas las tablas en Excel
827
  download_excel_button.click(
828
  download_all_tables_excel,
829
  inputs=[],
830
- outputs=[download_excel_button]
831
- )
832
-
833
- # Mostrar el primer gráfico después de generar todos
834
- def show_first_plot(all_figures):
835
- if not all_figures:
836
- return None, "No hay gráficos disponibles.", 0
837
- return all_figures[0], "Gráfico 1 de 9", 0
838
-
839
- plot_button.click(
840
- lambda: (None, "Gráfico 1 de 9", 0), # Placeholder para reiniciar el índice
841
- inputs=[],
842
- outputs=[rsm_plot_output, plot_info, current_index_state]
843
- )
844
-
845
- # Actualizar gráficos al navegar
846
- left_button.click(
847
- update_plot,
848
- inputs=["left", current_index_state, gr.Number(value=9), gr.State()],
849
- outputs=[rsm_plot_output, plot_info, current_index_state]
850
- )
851
- right_button.click(
852
- update_plot,
853
- inputs=["right", current_index_state, gr.Number(value=9), gr.State()],
854
- outputs=[rsm_plot_output, plot_info, current_index_state]
855
  )
856
 
857
  # Ejemplo de uso
 
3
  import statsmodels.formula.api as smf
4
  import statsmodels.api as sm
5
  import plotly.graph_objects as go
 
6
  from scipy.optimize import minimize
7
  import plotly.express as px
8
  from scipy.stats import t, f
 
567
  return data.round(3), x1_name, x2_name, x3_name, y_name, x1_levels, x2_levels, x3_levels, gr.update(visible=True)
568
 
569
  except Exception as e:
570
+ # Nota: Para mostrar mensajes de error, Gradio necesita manejarlo de manera diferente
571
+ # Aquí, simplemente retornamos nada y ocultamos la sección de análisis
572
+ return None, "", "", "", "", [], [], [], gr.update(visible=False)
573
 
574
  def fit_and_optimize_model():
575
  if 'rsm' not in globals():
576
+ return [None]*10, 0
577
 
578
  # Ajustar modelos y optimizar
579
  model_completo, pareto_completo = rsm.fit_model()
 
602
  contribution_table,
603
  anova_table,
604
  rsm.all_figures, # Devuelve todas las figuras generadas
605
+ 0 # Inicializar el índice actual
606
  )
607
 
608
+ def show_plot(all_figures, current_index):
609
  if not all_figures:
610
  return None, "No hay gráficos disponibles.", current_index
 
611
  selected_fig = all_figures[current_index]
612
  plot_info_text = f"Gráfico {current_index + 1} de {len(all_figures)}"
 
613
  return selected_fig, plot_info_text, current_index
614
 
615
+ def navigate_plot(direction, current_index, all_figures):
616
  """
617
  Navega entre los gráficos.
618
 
619
  Args:
620
  direction (str): 'left' o 'right'.
621
  current_index (int): Índice actual.
 
622
  all_figures (list): Lista de todas las figuras.
623
 
624
  Returns:
 
628
  return None, "No hay gráficos disponibles.", current_index
629
 
630
  if direction == 'left':
631
+ new_index = (current_index - 1) % len(all_figures)
632
  elif direction == 'right':
633
+ new_index = (current_index + 1) % len(all_figures)
634
  else:
635
  new_index = current_index
636
 
637
  selected_fig = all_figures[new_index]
638
+ plot_info_text = f"Gráfico {new_index + 1} de {len(all_figures)}"
639
 
640
  return selected_fig, plot_info_text, new_index
641
 
 
760
  download_plot_button = gr.DownloadButton("Descargar Gráfico Actual (PNG)")
761
  download_all_plots_button = gr.DownloadButton("Descargar Todos los Gráficos (ZIP)")
762
  current_index_state = gr.State(0) # Estado para el índice actual
763
+
764
+ # Cargar datos
765
  load_button.click(
766
  load_data,
767
  inputs=[x1_name_input, x2_name_input, x3_name_input, y_name_input, x1_levels_input, x2_levels_input, x3_levels_input, data_input],
768
  outputs=[data_output, x1_name_input, x2_name_input, x3_name_input, y_name_input, x1_levels_input, x2_levels_input, x3_levels_input, analysis_row]
769
  )
770
 
771
+ # Ajustar modelo y optimizar
772
  fit_button.click(
773
  fit_and_optimize_model,
774
  inputs=[],
 
783
  contribution_table_output,
784
  anova_table_output,
785
  gr.State(), # all_figures
786
+ gr.State() # current_index
787
  ]
788
  )
789
 
790
+ # Generar y mostrar el primer gráfico
791
  plot_button.click(
792
+ lambda all_figures: show_plot(all_figures, 0),
793
+ inputs=[gr.State()],
794
  outputs=[rsm_plot_output, plot_info, current_index_state]
795
  )
796
 
797
  # Navegación de gráficos
798
  left_button.click(
799
+ lambda current_index, all_figures: navigate_plot('left', current_index, all_figures),
800
+ inputs=[current_index_state, gr.State()],
801
  outputs=[rsm_plot_output, plot_info, current_index_state]
802
  )
803
  right_button.click(
804
+ lambda current_index, all_figures: navigate_plot('right', current_index, all_figures),
805
+ inputs=[current_index_state, gr.State()],
806
  outputs=[rsm_plot_output, plot_info, current_index_state]
807
  )
808
 
 
 
 
 
809
  # Descargar gráfico actual
810
  download_plot_button.click(
811
  download_current_plot,
812
  inputs=[gr.State(), current_index_state],
813
+ outputs=[gr.File()]
814
  )
815
 
816
  # Descargar todos los gráficos en ZIP
817
  download_all_plots_button.click(
818
  download_all_plots_zip,
819
  inputs=[gr.State()],
820
+ outputs=[gr.File()]
821
  )
822
 
823
  # Descargar todas las tablas en Excel
824
  download_excel_button.click(
825
  download_all_tables_excel,
826
  inputs=[],
827
+ outputs=[gr.File()]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
828
  )
829
 
830
  # Ejemplo de uso