C2MV commited on
Commit
f3f77c0
·
verified ·
1 Parent(s): eac6bfb

Create UI.py

Browse files
Files changed (1) hide show
  1. UI.py +153 -0
UI.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # UI.py
2
+
3
+ import gradio as gr
4
+
5
+ def create_interface(process_and_plot):
6
+ with gr.Blocks() as demo:
7
+ gr.Markdown("# Bioprocess Modeling Application with Yi-Coder Integration")
8
+
9
+ file_input = gr.File(label="Upload Excel File")
10
+
11
+ MAX_EQUATIONS = 3
12
+ biomass_equations = []
13
+ biomass_params = []
14
+ biomass_bounds = []
15
+ substrate_equations = []
16
+ substrate_params = []
17
+ substrate_bounds = []
18
+ product_equations = []
19
+ product_params = []
20
+ product_bounds = []
21
+
22
+ def create_model_inputs(model_name, equations_list, params_list, bounds_list):
23
+ with gr.Column():
24
+ gr.Markdown(f"### {model_name} Models")
25
+ for i in range(MAX_EQUATIONS):
26
+ with gr.Row(visible=(i == 0)) as row:
27
+ equation_input = gr.Textbox(
28
+ label=f"{model_name} Model {i+1} Equation",
29
+ placeholder="Enter equation in terms of t and parameters",
30
+ lines=1,
31
+ value="" if i > 0 else "Predefined equation"
32
+ )
33
+ params_input = gr.Textbox(
34
+ label=f"{model_name} Model {i+1} Parameters",
35
+ placeholder="Comma-separated parameters",
36
+ lines=1,
37
+ value="" if i > 0 else "Parameters"
38
+ )
39
+ bounds_input = gr.Textbox(
40
+ label=f"{model_name} Model {i+1} Bounds",
41
+ placeholder="(lower, upper) for each parameter",
42
+ lines=1
43
+ )
44
+ equations_list.append((row, equation_input))
45
+ params_list.append(params_input)
46
+ bounds_list.append(bounds_input)
47
+ add_btn = gr.Button(f"Add {model_name} Equation")
48
+ remove_btn = gr.Button(f"Remove {model_name} Equation")
49
+ return add_btn, remove_btn
50
+
51
+ with gr.Accordion("Model Definitions", open=True):
52
+ with gr.Row():
53
+ with gr.Column():
54
+ add_biomass_btn, remove_biomass_btn = create_model_inputs(
55
+ "Biomass", biomass_equations, biomass_params, biomass_bounds
56
+ )
57
+ with gr.Column():
58
+ add_substrate_btn, remove_substrate_btn = create_model_inputs(
59
+ "Substrate", substrate_equations, substrate_params, substrate_bounds
60
+ )
61
+ with gr.Column():
62
+ add_product_btn, remove_product_btn = create_model_inputs(
63
+ "Product", product_equations, product_params, product_bounds
64
+ )
65
+
66
+ legend_position = gr.Radio(
67
+ choices=["upper left", "upper right", "lower left", "lower right", "best"],
68
+ label="Legend Position",
69
+ value="best"
70
+ )
71
+ show_legend = gr.Checkbox(label="Show Legend", value=True)
72
+ show_params = gr.Checkbox(label="Show Parameters", value=True)
73
+ simulate_btn = gr.Button("Simulate")
74
+
75
+ with gr.Row():
76
+ output_gallery = gr.Gallery(label="Results", columns=2, height='auto')
77
+ analysis_output = gr.Textbox(label="Yi-Coder Analysis", lines=15)
78
+
79
+ biomass_eq_count = gr.State(1)
80
+ substrate_eq_count = gr.State(1)
81
+ product_eq_count = gr.State(1)
82
+
83
+ def add_equation(equations_list, eq_count):
84
+ eq_count = min(eq_count + 1, MAX_EQUATIONS)
85
+ for i, (row, _) in enumerate(equations_list):
86
+ row.visible = i < eq_count
87
+ return [row.update(visible=row.visible) for row, _ in equations_list], eq_count
88
+
89
+ def remove_equation(equations_list, eq_count):
90
+ eq_count = max(eq_count - 1, 1)
91
+ for i, (row, _) in enumerate(equations_list):
92
+ row.visible = i < eq_count
93
+ return [row.update(visible=row.visible) for row, _ in equations_list], eq_count
94
+
95
+ add_biomass_btn.click(
96
+ fn=lambda eq_count: add_equation(biomass_equations, eq_count),
97
+ inputs=biomass_eq_count,
98
+ outputs=[*[row for row, _ in biomass_equations], biomass_eq_count]
99
+ )
100
+ remove_biomass_btn.click(
101
+ fn=lambda eq_count: remove_equation(biomass_equations, eq_count),
102
+ inputs=biomass_eq_count,
103
+ outputs=[*[row for row, _ in biomass_equations], biomass_eq_count]
104
+ )
105
+
106
+ add_substrate_btn.click(
107
+ fn=lambda eq_count: add_equation(substrate_equations, eq_count),
108
+ inputs=substrate_eq_count,
109
+ outputs=[*[row for row, _ in substrate_equations], substrate_eq_count]
110
+ )
111
+ remove_substrate_btn.click(
112
+ fn=lambda eq_count: remove_equation(substrate_equations, eq_count),
113
+ inputs=substrate_eq_count,
114
+ outputs=[*[row for row, _ in substrate_equations], substrate_eq_count]
115
+ )
116
+
117
+ add_product_btn.click(
118
+ fn=lambda eq_count: add_equation(product_equations, eq_count),
119
+ inputs=product_eq_count,
120
+ outputs=[*[row for row, _ in product_equations], product_eq_count]
121
+ )
122
+ remove_product_btn.click(
123
+ fn=lambda eq_count: remove_equation(product_equations, eq_count),
124
+ inputs=product_eq_count,
125
+ outputs=[*[row for row, _ in product_equations], product_eq_count]
126
+ )
127
+
128
+ simulate_inputs = [
129
+ file_input,
130
+ biomass_equations,
131
+ biomass_params,
132
+ biomass_bounds,
133
+ substrate_equations,
134
+ substrate_params,
135
+ substrate_bounds,
136
+ product_equations,
137
+ product_params,
138
+ product_bounds,
139
+ legend_position,
140
+ show_legend,
141
+ show_params,
142
+ biomass_eq_count,
143
+ substrate_eq_count,
144
+ product_eq_count
145
+ ]
146
+
147
+ simulate_btn.click(
148
+ fn=process_and_plot,
149
+ inputs=simulate_inputs,
150
+ outputs=[output_gallery, analysis_output]
151
+ )
152
+
153
+ return demo