|
|
|
|
|
import gradio as gr |
|
|
|
def create_interface(process_and_plot): |
|
with gr.Blocks() as demo: |
|
gr.Markdown("# Bioprocess Modeling Application with Yi-Coder Integration") |
|
|
|
file_input = gr.File(label="Upload Excel File") |
|
|
|
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"### {model_name} Models") |
|
for i in range(MAX_EQUATIONS): |
|
with gr.Row(visible=(i == 0)) as row: |
|
equation_input = gr.Textbox( |
|
label=f"{model_name} Model {i+1} Equation", |
|
placeholder="Enter equation in terms of t and parameters", |
|
lines=1, |
|
value="" if i > 0 else "Predefined equation" |
|
) |
|
params_input = gr.Textbox( |
|
label=f"{model_name} Model {i+1} Parameters", |
|
placeholder="Comma-separated parameters", |
|
lines=1, |
|
value="" if i > 0 else "Parameters" |
|
) |
|
bounds_input = gr.Textbox( |
|
label=f"{model_name} Model {i+1} Bounds", |
|
placeholder="(lower, upper) for each parameter", |
|
lines=1 |
|
) |
|
equations_list.append((row, equation_input)) |
|
params_list.append(params_input) |
|
bounds_list.append(bounds_input) |
|
add_btn = gr.Button(f"Add {model_name} Equation") |
|
remove_btn = gr.Button(f"Remove {model_name} Equation") |
|
return add_btn, remove_btn |
|
|
|
with gr.Accordion("Model Definitions", open=True): |
|
with gr.Row(): |
|
with gr.Column(): |
|
add_biomass_btn, remove_biomass_btn = create_model_inputs( |
|
"Biomass", biomass_equations, biomass_params, biomass_bounds |
|
) |
|
with gr.Column(): |
|
add_substrate_btn, remove_substrate_btn = create_model_inputs( |
|
"Substrate", substrate_equations, substrate_params, substrate_bounds |
|
) |
|
with gr.Column(): |
|
add_product_btn, remove_product_btn = create_model_inputs( |
|
"Product", product_equations, product_params, product_bounds |
|
) |
|
|
|
legend_position = gr.Radio( |
|
choices=["upper left", "upper right", "lower left", "lower right", "best"], |
|
label="Legend Position", |
|
value="best" |
|
) |
|
show_legend = gr.Checkbox(label="Show Legend", value=True) |
|
show_params = gr.Checkbox(label="Show Parameters", value=True) |
|
simulate_btn = gr.Button("Simulate") |
|
|
|
with gr.Row(): |
|
output_gallery = gr.Gallery(label="Results", columns=2, height='auto') |
|
analysis_output = gr.Textbox(label="Yi-Coder Analysis", lines=15) |
|
|
|
biomass_eq_count = gr.State(1) |
|
substrate_eq_count = gr.State(1) |
|
product_eq_count = gr.State(1) |
|
|
|
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, |
|
biomass_equations, |
|
biomass_params, |
|
biomass_bounds, |
|
substrate_equations, |
|
substrate_params, |
|
substrate_bounds, |
|
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 |
|
|