Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -489,7 +489,7 @@ def load_data(x1_name, x2_name, x3_name, y_name, x1_levels_str, x2_levels_str, x
|
|
| 489 |
data_str (str): Datos del experimento en formato CSV, separados por comas.
|
| 490 |
|
| 491 |
Returns:
|
| 492 |
-
tuple: (pd.DataFrame, str, str, str, str, list, list, list, gr.update)
|
| 493 |
"""
|
| 494 |
try:
|
| 495 |
# Convertir los niveles a listas de números
|
|
@@ -516,11 +516,10 @@ def load_data(x1_name, x2_name, x3_name, y_name, x1_levels_str, x2_levels_str, x
|
|
| 516 |
except Exception as e:
|
| 517 |
return None, "", "", "", "", [], [], [], gr.update(visible=False), f"Error: {e}"
|
| 518 |
|
| 519 |
-
def fit_and_optimize_model():
|
| 520 |
if 'rsm' not in globals():
|
| 521 |
-
return None, None, None, None, None, None, None, None, None, "Error: Carga los datos primero."
|
| 522 |
|
| 523 |
-
global rsm_plots
|
| 524 |
model_completo, pareto_completo = rsm.fit_model()
|
| 525 |
model_simplificado, pareto_simplificado = rsm.fit_simplified_model()
|
| 526 |
optimization_table = rsm.optimize()
|
|
@@ -536,22 +535,35 @@ def fit_and_optimize_model():
|
|
| 536 |
equation_formatted = equation.replace(" + ", "<br>+ ").replace(" ** ", "^").replace("*", " × ")
|
| 537 |
equation_formatted = f"### Ecuación del Modelo Simplificado:<br>{equation_formatted}"
|
| 538 |
|
| 539 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 540 |
|
| 541 |
-
def generate_rsm_plot(plot_index):
|
| 542 |
-
if
|
| 543 |
return None, gr.update(visible=False), "Error: Genera los gráficos primero."
|
| 544 |
|
| 545 |
plot_index = int(plot_index)
|
| 546 |
-
if 0 <= plot_index < len(
|
| 547 |
-
selected_plot =
|
| 548 |
-
return selected_plot, gr.update(visible=True, value=plot_index)
|
| 549 |
else:
|
| 550 |
return None, gr.update(visible=False), "Error: Índice de gráfico fuera de rango."
|
| 551 |
|
| 552 |
def download_excel():
|
| 553 |
if 'rsm' not in globals():
|
| 554 |
-
return
|
| 555 |
|
| 556 |
output = io.BytesIO()
|
| 557 |
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
|
|
@@ -567,27 +579,27 @@ def download_excel():
|
|
| 567 |
href = f'<a download="resultados_rsm.xlsx" href="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{b64}">Descargar Excel</a>'
|
| 568 |
return href
|
| 569 |
|
| 570 |
-
def download_selected_image(plot_index):
|
| 571 |
-
if
|
| 572 |
-
return
|
| 573 |
|
| 574 |
plot_index = int(plot_index)
|
| 575 |
-
if 0 <= plot_index < len(
|
| 576 |
-
selected_plot =
|
| 577 |
img_bytes = selected_plot.to_image(format="png")
|
| 578 |
b64 = b64encode(img_bytes).decode('utf-8')
|
| 579 |
href = f'<a download="grafico_rsm_{plot_index}.png" href="data:image/png;base64,{b64}">Descargar Gráfico {plot_index}</a>'
|
| 580 |
return href
|
| 581 |
else:
|
| 582 |
-
return
|
| 583 |
|
| 584 |
-
def download_all_images():
|
| 585 |
-
if
|
| 586 |
-
return
|
| 587 |
|
| 588 |
zip_output = io.BytesIO()
|
| 589 |
with zipfile.ZipFile(zip_output, 'w') as zipf:
|
| 590 |
-
for i, fig in enumerate(
|
| 591 |
img_bytes = fig.to_image(format="png")
|
| 592 |
zipf.writestr(f"grafico_rsm_{i}.png", img_bytes)
|
| 593 |
|
|
@@ -633,6 +645,9 @@ with gr.Blocks() as demo:
|
|
| 633 |
gr.Markdown("## Datos Cargados")
|
| 634 |
data_output = gr.Dataframe(label="Tabla de Datos")
|
| 635 |
|
|
|
|
|
|
|
|
|
|
| 636 |
# Hacer que la sección de análisis y gráficos sea visible solo después de cargar los datos
|
| 637 |
with gr.Row(visible=False) as analysis_row:
|
| 638 |
with gr.Column():
|
|
@@ -674,30 +689,52 @@ with gr.Blocks() as demo:
|
|
| 674 |
|
| 675 |
fit_button.click(
|
| 676 |
fit_and_optimize_model,
|
| 677 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 678 |
)
|
| 679 |
|
| 680 |
previous_plot_button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
generate_rsm_plot,
|
| 682 |
-
inputs=[plot_index_slider],
|
| 683 |
-
outputs=[rsm_plot_output, plot_index_slider]
|
| 684 |
-
)
|
| 685 |
|
| 686 |
next_plot_button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 687 |
generate_rsm_plot,
|
| 688 |
-
inputs=[plot_index_slider],
|
| 689 |
-
outputs=[rsm_plot_output, plot_index_slider]
|
| 690 |
-
)
|
| 691 |
|
| 692 |
plot_index_slider.change(
|
| 693 |
generate_rsm_plot,
|
| 694 |
-
inputs=[plot_index_slider],
|
| 695 |
-
outputs=[rsm_plot_output, plot_index_slider]
|
| 696 |
)
|
| 697 |
|
| 698 |
-
download_excel_button.click(download_excel, outputs=
|
| 699 |
-
download_image_button.click(download_selected_image, inputs=[plot_index_slider], outputs=
|
| 700 |
-
download_all_images_button.click(download_all_images, outputs=
|
| 701 |
|
| 702 |
# Ejemplo de uso
|
| 703 |
gr.Markdown("## Ejemplo de uso")
|
|
@@ -710,4 +747,4 @@ with gr.Blocks() as demo:
|
|
| 710 |
gr.Markdown("7. Haz clic en 'Descargar Gráfico' para descargar la imagen del gráfico actual.")
|
| 711 |
gr.Markdown("8. Haz clic en 'Descargar Todos los Gráficos' para descargar un archivo zip con todas las imágenes de los gráficos.")
|
| 712 |
|
| 713 |
-
demo.launch()
|
|
|
|
| 489 |
data_str (str): Datos del experimento en formato CSV, separados por comas.
|
| 490 |
|
| 491 |
Returns:
|
| 492 |
+
tuple: (pd.DataFrame, str, str, str, str, list, list, list, gr.update, list)
|
| 493 |
"""
|
| 494 |
try:
|
| 495 |
# Convertir los niveles a listas de números
|
|
|
|
| 516 |
except Exception as e:
|
| 517 |
return None, "", "", "", "", [], [], [], gr.update(visible=False), f"Error: {e}"
|
| 518 |
|
| 519 |
+
def fit_and_optimize_model(rsm_plots_state=None):
|
| 520 |
if 'rsm' not in globals():
|
| 521 |
+
return (None, None, None, None, None, None, None, None, None, None, [], "Error: Carga los datos primero.")
|
| 522 |
|
|
|
|
| 523 |
model_completo, pareto_completo = rsm.fit_model()
|
| 524 |
model_simplificado, pareto_simplificado = rsm.fit_simplified_model()
|
| 525 |
optimization_table = rsm.optimize()
|
|
|
|
| 535 |
equation_formatted = equation.replace(" + ", "<br>+ ").replace(" ** ", "^").replace("*", " × ")
|
| 536 |
equation_formatted = f"### Ecuación del Modelo Simplificado:<br>{equation_formatted}"
|
| 537 |
|
| 538 |
+
return (
|
| 539 |
+
model_completo.summary().tables[0].as_html(),
|
| 540 |
+
pareto_completo,
|
| 541 |
+
model_completo.summary().tables[1].as_html(),
|
| 542 |
+
model_simplificado.summary().as_html(),
|
| 543 |
+
pareto_simplificado,
|
| 544 |
+
equation_formatted,
|
| 545 |
+
optimization_table,
|
| 546 |
+
prediction_table,
|
| 547 |
+
contribution_table,
|
| 548 |
+
anova_table,
|
| 549 |
+
rsm_plots, # Esto se asignará al estado
|
| 550 |
+
gr.update(visible=True, maximum=len(rsm_plots) -1) if rsm_plots else gr.update()
|
| 551 |
+
)
|
| 552 |
|
| 553 |
+
def generate_rsm_plot(plot_index, rsm_plots_state):
|
| 554 |
+
if not rsm_plots_state:
|
| 555 |
return None, gr.update(visible=False), "Error: Genera los gráficos primero."
|
| 556 |
|
| 557 |
plot_index = int(plot_index)
|
| 558 |
+
if 0 <= plot_index < len(rsm_plots_state):
|
| 559 |
+
selected_plot = rsm_plots_state[plot_index]
|
| 560 |
+
return selected_plot, gr.update(visible=True, value=plot_index), ""
|
| 561 |
else:
|
| 562 |
return None, gr.update(visible=False), "Error: Índice de gráfico fuera de rango."
|
| 563 |
|
| 564 |
def download_excel():
|
| 565 |
if 'rsm' not in globals():
|
| 566 |
+
return "Error: Carga los datos y ajusta el modelo primero."
|
| 567 |
|
| 568 |
output = io.BytesIO()
|
| 569 |
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
|
|
|
|
| 579 |
href = f'<a download="resultados_rsm.xlsx" href="data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,{b64}">Descargar Excel</a>'
|
| 580 |
return href
|
| 581 |
|
| 582 |
+
def download_selected_image(plot_index, rsm_plots_state):
|
| 583 |
+
if not rsm_plots_state:
|
| 584 |
+
return "Error: Genera los gráficos primero."
|
| 585 |
|
| 586 |
plot_index = int(plot_index)
|
| 587 |
+
if 0 <= plot_index < len(rsm_plots_state):
|
| 588 |
+
selected_plot = rsm_plots_state[plot_index]
|
| 589 |
img_bytes = selected_plot.to_image(format="png")
|
| 590 |
b64 = b64encode(img_bytes).decode('utf-8')
|
| 591 |
href = f'<a download="grafico_rsm_{plot_index}.png" href="data:image/png;base64,{b64}">Descargar Gráfico {plot_index}</a>'
|
| 592 |
return href
|
| 593 |
else:
|
| 594 |
+
return "Error: Índice de gráfico fuera de rango."
|
| 595 |
|
| 596 |
+
def download_all_images(rsm_plots_state):
|
| 597 |
+
if not rsm_plots_state:
|
| 598 |
+
return "Error: Genera los gráficos primero."
|
| 599 |
|
| 600 |
zip_output = io.BytesIO()
|
| 601 |
with zipfile.ZipFile(zip_output, 'w') as zipf:
|
| 602 |
+
for i, fig in enumerate(rsm_plots_state):
|
| 603 |
img_bytes = fig.to_image(format="png")
|
| 604 |
zipf.writestr(f"grafico_rsm_{i}.png", img_bytes)
|
| 605 |
|
|
|
|
| 645 |
gr.Markdown("## Datos Cargados")
|
| 646 |
data_output = gr.Dataframe(label="Tabla de Datos")
|
| 647 |
|
| 648 |
+
# Definir el estado para rsm_plots
|
| 649 |
+
rsm_plots_state = gr.State([])
|
| 650 |
+
|
| 651 |
# Hacer que la sección de análisis y gráficos sea visible solo después de cargar los datos
|
| 652 |
with gr.Row(visible=False) as analysis_row:
|
| 653 |
with gr.Column():
|
|
|
|
| 689 |
|
| 690 |
fit_button.click(
|
| 691 |
fit_and_optimize_model,
|
| 692 |
+
inputs=None,
|
| 693 |
+
outputs=[
|
| 694 |
+
model_completo_output1,
|
| 695 |
+
pareto_completo,
|
| 696 |
+
model_completo_output2,
|
| 697 |
+
model_simplificado_output,
|
| 698 |
+
pareto_simplificado_output,
|
| 699 |
+
equation_output,
|
| 700 |
+
optimization_table_output,
|
| 701 |
+
prediction_table_output,
|
| 702 |
+
contribution_table_output,
|
| 703 |
+
anova_table_output,
|
| 704 |
+
rsm_plots_state,
|
| 705 |
+
plot_index_slider
|
| 706 |
+
]
|
| 707 |
)
|
| 708 |
|
| 709 |
previous_plot_button.click(
|
| 710 |
+
lambda x: x - 1 if x > 0 else x,
|
| 711 |
+
inputs=plot_index_slider,
|
| 712 |
+
outputs=plot_index_slider
|
| 713 |
+
).then(
|
| 714 |
generate_rsm_plot,
|
| 715 |
+
inputs=[plot_index_slider, rsm_plots_state],
|
| 716 |
+
outputs=[rsm_plot_output, plot_index_slider, gr.Textbox()]
|
| 717 |
+
)
|
| 718 |
|
| 719 |
next_plot_button.click(
|
| 720 |
+
lambda x: x + 1 if x < 8 else x,
|
| 721 |
+
inputs=plot_index_slider,
|
| 722 |
+
outputs=plot_index_slider
|
| 723 |
+
).then(
|
| 724 |
generate_rsm_plot,
|
| 725 |
+
inputs=[plot_index_slider, rsm_plots_state],
|
| 726 |
+
outputs=[rsm_plot_output, plot_index_slider, gr.Textbox()]
|
| 727 |
+
)
|
| 728 |
|
| 729 |
plot_index_slider.change(
|
| 730 |
generate_rsm_plot,
|
| 731 |
+
inputs=[plot_index_slider, rsm_plots_state],
|
| 732 |
+
outputs=[rsm_plot_output, plot_index_slider, gr.Textbox()]
|
| 733 |
)
|
| 734 |
|
| 735 |
+
download_excel_button.click(download_excel, outputs=download_excel_button)
|
| 736 |
+
download_image_button.click(download_selected_image, inputs=[plot_index_slider, rsm_plots_state], outputs=download_image_button)
|
| 737 |
+
download_all_images_button.click(download_all_images, inputs=[rsm_plots_state], outputs=download_all_images_button)
|
| 738 |
|
| 739 |
# Ejemplo de uso
|
| 740 |
gr.Markdown("## Ejemplo de uso")
|
|
|
|
| 747 |
gr.Markdown("7. Haz clic en 'Descargar Gráfico' para descargar la imagen del gráfico actual.")
|
| 748 |
gr.Markdown("8. Haz clic en 'Descargar Todos los Gráficos' para descargar un archivo zip con todas las imágenes de los gráficos.")
|
| 749 |
|
| 750 |
+
demo.launch()
|