C2MV commited on
Commit
71817ec
verified
1 Parent(s): ddd7e2a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -0
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ 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
10
+ import gradio as gr
11
+
12
+ class RSM_BoxBehnken:
13
+ # ... (Tu c贸digo de la clase RSM_BoxBehnken se mantiene igual) ...
14
+
15
+ # Crear un DataFrame a partir de la tabla
16
+ data = pd.DataFrame({
17
+ 'Exp.': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
18
+ 'Glucosa': [-1, 1, -1, 1, -1, 1, -1, 1, 0, 0, 0, 0, 0, 0, 0],
19
+ 'Extracto de Levadura': [-1, -1, 1, 1, 0, 0, 0, 0, -1, 1, -1, 1, 0, 0, 0],
20
+ 'Tript贸fano': [0, 0, 0, 0, -1, -1, 1, 1, -1, -1, 1, 1, 0, 0, 0],
21
+ 'AIA (ppm)': [166.594, 177.557, 127.261, 147.573, 188.883, 224.527, 190.238, 226.483, 195.550, 149.493, 187.683, 148.621, 278.951, 297.238, 280.896]
22
+ })
23
+
24
+ # Crear una instancia de la clase RSM_BoxBehnken
25
+ rsm = RSM_BoxBehnken(data)
26
+
27
+ # --- Funciones para la interfaz de Gradio ---
28
+
29
+ def fit_full_model():
30
+ rsm.fit_model()
31
+ return "Modelo completo ajustado. Revisa la consola para ver el resumen."
32
+
33
+ def fit_simplified_model():
34
+ rsm.fit_simplified_model()
35
+ return "Modelo simplificado ajustado. Revisa la consola para ver el resumen."
36
+
37
+ def optimize_model(method):
38
+ rsm.optimize(method)
39
+ return (f"Optimizaci贸n realizada con {method}. Revisa la consola para ver los niveles 贸ptimos.\n"
40
+ f"Niveles 贸ptimos (codificados): {rsm.optimal_levels}\n"
41
+ f"Valor m谩ximo de {rsm.y_name}: {-rsm.optimized_results.fun:.4f}")
42
+
43
+ def generate_plot(fixed_variable, fixed_level_natural):
44
+ fig = rsm.plot_rsm_individual(fixed_variable, fixed_level_natural)
45
+ if fig is not None:
46
+ return fig
47
+ else:
48
+ return "Ajusta el modelo simplificado primero."
49
+
50
+ # --- Creaci贸n de la interfaz de Gradio ---
51
+
52
+ with gr.Blocks() as demo:
53
+ gr.Markdown("# An谩lisis de Superficie de Respuesta (RSM) - Dise帽o Box-Behnken")
54
+
55
+ with gr.Tab("Ajuste de Modelos"):
56
+ with gr.Row():
57
+ full_model_button = gr.Button("Ajustar Modelo Completo")
58
+ simplified_model_button = gr.Button("Ajustar Modelo Simplificado")
59
+ model_output = gr.Textbox(label="Resultado del Ajuste")
60
+
61
+ full_model_button.click(fn=fit_full_model, outputs=model_output)
62
+ simplified_model_button.click(fn=fit_simplified_model, outputs=model_output)
63
+
64
+ with gr.Tab("Optimizaci贸n"):
65
+ method_dropdown = gr.Dropdown(
66
+ choices=['Nelder-Mead', 'Powell', 'BFGS'],
67
+ value='Nelder-Mead',
68
+ label="M茅todo de Optimizaci贸n"
69
+ )
70
+ optimize_button = gr.Button("Optimizar")
71
+ optimization_output = gr.Textbox(label="Resultado de la Optimizaci贸n")
72
+
73
+ optimize_button.click(fn=optimize_model, inputs=method_dropdown, outputs=optimization_output)
74
+
75
+ with gr.Tab("Gr谩ficos de Superficie de Respuesta"):
76
+ with gr.Row():
77
+ fixed_variable_dropdown = gr.Dropdown(
78
+ choices=[rsm.x1_name, rsm.x2_name, rsm.x3_name],
79
+ value=rsm.x1_name,
80
+ label="Variable Fija"
81
+ )
82
+ fixed_level_slider = gr.Slider(
83
+ minimum=min(rsm.get_levels(rsm.x1_name)),
84
+ maximum=max(rsm.get_levels(rsm.x1_name)),
85
+ step=0.01,
86
+ value=rsm.get_levels(rsm.x1_name)[1],
87
+ label="Nivel de Variable Fija (Natural)"
88
+ )
89
+ plot_button = gr.Button("Generar Gr谩fico")
90
+ plot_output = gr.Plot(label="Gr谩fico RSM")
91
+
92
+ def update_slider_range(fixed_variable):
93
+ levels = rsm.get_levels(fixed_variable)
94
+ return gr.Slider.update(minimum=min(levels), maximum=max(levels), value=levels[1])
95
+
96
+ fixed_variable_dropdown.change(
97
+ fn=update_slider_range,
98
+ inputs=fixed_variable_dropdown,
99
+ outputs=fixed_level_slider
100
+ )
101
+ plot_button.click(
102
+ fn=generate_plot,
103
+ inputs=[fixed_variable_dropdown, fixed_level_slider],
104
+ outputs=plot_output
105
+ )
106
+
107
+ demo.launch()