# UI.py import gradio as gr def create_interface(process_and_plot): with gr.Blocks() as demo: gr.Markdown("# Aplicación de Modelado de Bioprocesos con Integración de Yi-Coder") file_input = gr.File(label="Subir Archivo Excel") MAX_EQUATIONS = 3 biomass_equations = [] biomass_params = [] biomass_bounds = [] substrate_equations = [] substrate_params = [] substrate_bounds = [] product_equations = [] product_params = [] product_bounds = [] def create_model_inputs(model_name, equations_list, params_list, bounds_list): with gr.Column(): gr.Markdown(f"### Modelos de {model_name}") for i in range(MAX_EQUATIONS): with gr.Row(visible=(i == 0)) as row: equation_input = gr.Textbox( label=f"Ecuación del Modelo {model_name} {i+1}", placeholder="Introduce la ecuación en términos de t y parámetros", lines=1, value="" if i > 0 else "Ecuación por defecto" ) params_input = gr.Textbox( label=f"Parámetros del Modelo {model_name} {i+1}", placeholder="Parámetros separados por comas", lines=1, value="" if i > 0 else "Parámetros" ) bounds_input = gr.Textbox( label=f"Límites del Modelo {model_name} {i+1}", placeholder="(inferior, superior) para cada parámetro", lines=1 ) equations_list.append((row, equation_input)) params_list.append(params_input) bounds_list.append(bounds_input) add_btn = gr.Button(f"Agregar Ecuación de {model_name}") remove_btn = gr.Button(f"Eliminar Ecuación de {model_name}") return add_btn, remove_btn with gr.Accordion("Definición de Modelos", open=True): with gr.Row(): with gr.Column(): add_biomass_btn, remove_biomass_btn = create_model_inputs( "Biomasa", biomass_equations, biomass_params, biomass_bounds ) with gr.Column(): add_substrate_btn, remove_substrate_btn = create_model_inputs( "Sustrato", substrate_equations, substrate_params, substrate_bounds ) with gr.Column(): add_product_btn, remove_product_btn = create_model_inputs( "Producto", product_equations, product_params, product_bounds ) legend_position = gr.Radio( choices=["upper left", "upper right", "lower left", "lower right", "best"], label="Posición de la Leyenda", value="best" ) show_legend = gr.Checkbox(label="Mostrar Leyenda", value=True) show_params = gr.Checkbox(label="Mostrar Parámetros", value=True) simulate_btn = gr.Button("Simular") with gr.Row(): output_gallery = gr.Gallery(label="Resultados", columns=2, height='auto') analysis_output = gr.Textbox(label="Análisis de Yi-Coder", lines=15) biomass_eq_count = gr.Number(value=1, visible=False) substrate_eq_count = gr.Number(value=1, visible=False) product_eq_count = gr.Number(value=1, visible=False) def add_equation(equations_list, eq_count): eq_count = min(eq_count + 1, MAX_EQUATIONS) for i, (row, _) in enumerate(equations_list): row.visible = i < eq_count return [row.update(visible=row.visible) for row, _ in equations_list], eq_count def remove_equation(equations_list, eq_count): eq_count = max(eq_count - 1, 1) for i, (row, _) in enumerate(equations_list): row.visible = i < eq_count return [row.update(visible=row.visible) for row, _ in equations_list], eq_count add_biomass_btn.click( fn=lambda eq_count: add_equation(biomass_equations, eq_count), inputs=biomass_eq_count, outputs=[*[row for row, _ in biomass_equations], biomass_eq_count] ) remove_biomass_btn.click( fn=lambda eq_count: remove_equation(biomass_equations, eq_count), inputs=biomass_eq_count, outputs=[*[row for row, _ in biomass_equations], biomass_eq_count] ) add_substrate_btn.click( fn=lambda eq_count: add_equation(substrate_equations, eq_count), inputs=substrate_eq_count, outputs=[*[row for row, _ in substrate_equations], substrate_eq_count] ) remove_substrate_btn.click( fn=lambda eq_count: remove_equation(substrate_equations, eq_count), inputs=substrate_eq_count, outputs=[*[row for row, _ in substrate_equations], substrate_eq_count] ) add_product_btn.click( fn=lambda eq_count: add_equation(product_equations, eq_count), inputs=product_eq_count, outputs=[*[row for row, _ in product_equations], product_eq_count] ) remove_product_btn.click( fn=lambda eq_count: remove_equation(product_equations, eq_count), inputs=product_eq_count, outputs=[*[row for row, _ in product_equations], product_eq_count] ) simulate_inputs = [ file_input, *[eq_input for row, eq_input in biomass_equations], *biomass_params, *biomass_bounds, *[eq_input for row, eq_input in substrate_equations], *substrate_params, *substrate_bounds, *[eq_input for row, eq_input in product_equations], *product_params, *product_bounds, legend_position, show_legend, show_params, biomass_eq_count, substrate_eq_count, product_eq_count ] simulate_btn.click( fn=process_and_plot, inputs=simulate_inputs, outputs=[output_gallery, analysis_output] ) return demo