C2MV commited on
Commit
da8be82
·
verified ·
1 Parent(s): 519b16c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -29
app.py CHANGED
@@ -10,11 +10,11 @@ from scipy.stats import t, f
10
  import gradio as gr
11
  import io
12
  import os
13
- from zipfile import ZipFile
14
 
15
  class RSM_BoxBehnken:
 
16
  def __init__(self, data, x1_name, x2_name, x3_name, y_name, x1_levels, x2_levels, x3_levels):
17
- # ... (El código de la clase RSM_BoxBehnken se mantiene igual, solo se modifican las funciones que generan dataframes o strings)
18
  self.data = data.copy()
19
  self.model = None
20
  self.model_simplified = None
@@ -407,29 +407,25 @@ def fit_and_optimize_model():
407
  current_plot_index = 0
408
  plot_images = []
409
 
410
- def generate_rsm_plot(fixed_variable, fixed_level, request: gr.Request):
411
  global current_plot_index, plot_images
412
 
413
  if 'rsm' not in globals():
414
- return None, "Error: Carga los datos primero.", None, None
415
 
416
  if not plot_images:
417
  plot_images = rsm.generate_all_plots()
418
 
419
  if not plot_images:
420
- return None, "Error: No se pudieron generar los gráficos.", None, None
421
 
422
  current_plot_index = (current_plot_index) % len(plot_images)
423
  fig = plot_images[current_plot_index]
424
 
 
425
  img_bytes = fig.to_image(format="png")
426
-
427
- # Crear un archivo temporal para guardar la imagen
428
- temp_file = os.path.join(request.kwargs['temp_dir'], f"plot_{current_plot_index}.png")
429
- with open(temp_file, "wb") as f:
430
- f.write(img_bytes)
431
 
432
- return fig, "", temp_file, gr.update(visible=True)
433
 
434
  def download_excel():
435
  if 'rsm' not in globals():
@@ -445,9 +441,9 @@ def download_excel():
445
 
446
  output.seek(0)
447
 
448
- return gr.File(value=output, visible=True, filename="resultados_rsm.xlsx")
449
 
450
- def download_images(request: gr.Request):
451
  global plot_images
452
  if 'rsm' not in globals():
453
  return None, "Error: Carga los datos primero."
@@ -455,18 +451,15 @@ def download_images(request: gr.Request):
455
  if not plot_images:
456
  return None, "Error: No se han generado gráficos."
457
 
458
- zip_filename = "graficos_rsm.zip"
459
- zip_path = os.path.join(request.kwargs['temp_dir'], zip_filename)
460
-
461
- with ZipFile(zip_path, 'w') as zipf:
462
  for i, fig in enumerate(plot_images):
463
  img_bytes = fig.to_image(format="png")
464
- img_path = os.path.join(request.kwargs['temp_dir'], f"plot_{i}.png")
465
- with open(img_path, "wb") as f:
466
- f.write(img_bytes)
467
- zipf.write(img_path, f"plot_{i}.png")
468
 
469
- return gr.File(value=zip_path, visible=True, filename=zip_filename)
 
470
 
471
  def next_plot():
472
  global current_plot_index
@@ -476,6 +469,7 @@ def next_plot():
476
  def prev_plot():
477
  global current_plot_index
478
  current_plot_index -= 1
 
479
  return current_plot_index
480
 
481
  # --- Crear la interfaz de Gradio ---
@@ -542,8 +536,7 @@ with gr.Blocks() as demo:
542
  prev_plot_button = gr.Button("<")
543
  next_plot_button = gr.Button(">")
544
  rsm_plot_output = gr.Plot()
545
- download_plot_button = gr.Button("Descargar Gráfico Actual")
546
- plot_image_output = gr.File(label="Gráfico Actual", visible=False)
547
 
548
 
549
  load_button.click(
@@ -554,17 +547,15 @@ with gr.Blocks() as demo:
554
 
555
  fit_button.click(fit_and_optimize_model, outputs=[model_completo_output, pareto_completo_output, model_simplificado_output, pareto_simplificado_output, equation_output, optimization_table_output, prediction_table_output, contribution_table_output, anova_table_output])
556
 
557
- plot_button.click(generate_rsm_plot, inputs=[fixed_variable_input, fixed_level_input], outputs=[rsm_plot_output, equation_output, plot_image_output, download_plot_button])
558
 
559
  download_excel_button.click(download_excel, outputs=download_excel_button, api_name="download_excel")
560
 
561
  download_images_button.click(download_images, outputs=download_images_button, api_name="download_images")
562
-
563
- download_plot_button.click(lambda x: x, inputs=[plot_image_output], outputs=[plot_image_output], api_name="download_plot")
564
 
565
- prev_plot_button.click(prev_plot, outputs=prev_plot_button).then(generate_rsm_plot, inputs=[fixed_variable_input, fixed_level_input], outputs=[rsm_plot_output, equation_output, plot_image_output, download_plot_button])
566
 
567
- next_plot_button.click(next_plot, outputs=next_plot_button).then(generate_rsm_plot, inputs=[fixed_variable_input, fixed_level_input], outputs=[rsm_plot_output, equation_output, plot_image_output, download_plot_button])
568
 
569
  # Ejemplo de uso
570
  gr.Markdown("## Ejemplo de uso")
 
10
  import gradio as gr
11
  import io
12
  import os
13
+ import zipfile
14
 
15
  class RSM_BoxBehnken:
16
+ # ... (La clase RSM_BoxBehnken se mantiene igual que en la respuesta anterior)
17
  def __init__(self, data, x1_name, x2_name, x3_name, y_name, x1_levels, x2_levels, x3_levels):
 
18
  self.data = data.copy()
19
  self.model = None
20
  self.model_simplified = None
 
407
  current_plot_index = 0
408
  plot_images = []
409
 
410
+ def generate_rsm_plot(fixed_variable, fixed_level):
411
  global current_plot_index, plot_images
412
 
413
  if 'rsm' not in globals():
414
+ return None, "Error: Carga los datos primero.", None
415
 
416
  if not plot_images:
417
  plot_images = rsm.generate_all_plots()
418
 
419
  if not plot_images:
420
+ return None, "Error: No se pudieron generar los gráficos.", None
421
 
422
  current_plot_index = (current_plot_index) % len(plot_images)
423
  fig = plot_images[current_plot_index]
424
 
425
+ # Convertir la figura a bytes de imagen
426
  img_bytes = fig.to_image(format="png")
 
 
 
 
 
427
 
428
+ return fig, "", img_bytes
429
 
430
  def download_excel():
431
  if 'rsm' not in globals():
 
441
 
442
  output.seek(0)
443
 
444
+ return gr.File(value=output, file_types=['.xlsx'], label="Descargar Excel")
445
 
446
+ def download_images():
447
  global plot_images
448
  if 'rsm' not in globals():
449
  return None, "Error: Carga los datos primero."
 
451
  if not plot_images:
452
  return None, "Error: No se han generado gráficos."
453
 
454
+ # Crear un archivo zip en memoria
455
+ zip_buffer = io.BytesIO()
456
+ with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zipf:
 
457
  for i, fig in enumerate(plot_images):
458
  img_bytes = fig.to_image(format="png")
459
+ zipf.writestr(f"plot_{i}.png", img_bytes)
 
 
 
460
 
461
+ zip_buffer.seek(0)
462
+ return gr.File(value=zip_buffer, file_types=['.zip'], label="Descargar ZIP")
463
 
464
  def next_plot():
465
  global current_plot_index
 
469
  def prev_plot():
470
  global current_plot_index
471
  current_plot_index -= 1
472
+
473
  return current_plot_index
474
 
475
  # --- Crear la interfaz de Gradio ---
 
536
  prev_plot_button = gr.Button("<")
537
  next_plot_button = gr.Button(">")
538
  rsm_plot_output = gr.Plot()
539
+ plot_image_output = gr.File(label="Descargar Gráfico Actual", file_types=["image/png"])
 
540
 
541
 
542
  load_button.click(
 
547
 
548
  fit_button.click(fit_and_optimize_model, outputs=[model_completo_output, pareto_completo_output, model_simplificado_output, pareto_simplificado_output, equation_output, optimization_table_output, prediction_table_output, contribution_table_output, anova_table_output])
549
 
550
+ plot_button.click(generate_rsm_plot, inputs=[fixed_variable_input, fixed_level_input], outputs=[rsm_plot_output, equation_output, plot_image_output])
551
 
552
  download_excel_button.click(download_excel, outputs=download_excel_button, api_name="download_excel")
553
 
554
  download_images_button.click(download_images, outputs=download_images_button, api_name="download_images")
 
 
555
 
556
+ prev_plot_button.click(prev_plot, outputs=prev_plot_button).then(generate_rsm_plot, inputs=[fixed_variable_input, fixed_level_input], outputs=[rsm_plot_output, equation_output, plot_image_output])
557
 
558
+ next_plot_button.click(next_plot, outputs=next_plot_button).then(generate_rsm_plot, inputs=[fixed_variable_input, fixed_level_input], outputs=[rsm_plot_output, equation_output, plot_image_output])
559
 
560
  # Ejemplo de uso
561
  gr.Markdown("## Ejemplo de uso")