File size: 6,258 Bytes
f3f77c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# UI.py

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