Biotech2 / UI.py
C2MV's picture
Update UI.py
67e3976 verified
raw
history blame
6.55 kB
# UI.py
import gradio as gr
from interface import process_and_plot
def create_interface():
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"Limites 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