Repository navigation
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
261 lines (244 loc) · 14.5 KB
/
Copy pathstreamlit_app.py
File metadata and controls
261 lines (244 loc) · 14.5 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# Copyright (c) 2025 Robert Bosch GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module is part of the SUNRISE demonstrator and creates a web-based user interface that serves as front-end.
The Streamlit framework is used to run the web server and infrastructure, the content of the page is defined here.
"""
import os
import logging
import streamlit as st
import pydantic
import ui_utils as uiu
import runtime_manager_interface as rmi
import dataformats
ui_logger = logging.getLogger("ui")
ui_logger.setLevel(logging.INFO)
ui_logger.propagate = False
# Streamlit reruns this script; keep a single console handler.
if not ui_logger.handlers:
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter(logging.BASIC_FORMAT))
ui_logger.addHandler(handler)
logger = ui_logger.getChild("streamlit_app")
# LOCAL TYPES ##########################################################################################################
class MinUiCfg(pydantic.BaseModel):
"""Definition of System to be used in the Minimal UI Context
system_name: Name of the system to be used
system_version: the version of 'system_name'. If omitted, the first available version on the RM is used.
display_name, creator, description: Information that is added to the experiment
*_parameters: list of parameter names that should be presented to the user for modification
results:list of result object names that should be presented. If 'None', all available are displayed.
"""
system_name: str
system_version: str | None = None
display_name: str = "From Demo UI"
creator: str = "UI User"
description: str = ""
common_parameters: list[str] = []
build_parameters: list[str] = []
run_parameters: list[str] = []
results: list[str] | None = None
def get_param_keys_to_modify(self, group: dataformats.ParameterGroup) -> list[str]:
"""Return the parameter type to be used in the SUNRISE Client API"""
if group is dataformats.ParameterGroup.COMMON:
return self.common_parameters
if group is dataformats.ParameterGroup.BUILD:
return self.build_parameters
if group is dataformats.ParameterGroup.RUN:
return self.run_parameters
raise KeyError(f"Unknown group type: {group}")
# STREAMLIT STATE INITIALIZATION #######################################################################################
if "mnl_config" not in st.session_state:
st.session_state.mnl_config = MinUiCfg(system_name="Demo System", build_parameters=["tracing"],
run_parameters=["frequency"], results=["signal_trace"])
if "mnl_session_id" not in st.session_state:
st.session_state.mnl_session_id = None
if "mnl_active_system" not in st.session_state:
st.session_state.mnl_active_system = uiu.SystemHandler()
if "mnl_build_log" not in st.session_state:
st.session_state["mnl_build_log"] = None
if "mnl_run_log" not in st.session_state:
st.session_state["mnl_run_log"] = None
if "mnl_params_locked" not in st.session_state:
st.session_state["mnl_params_locked"] = False
# BUILD GUI ELEMENTS ###################################################################################################
st.set_page_config(page_title="SUNRISE Front-End Demo", page_icon="img/sunrise_icon.ico",
initial_sidebar_state="collapsed")
st.image("img/sunrise_logo.svg", width=175)
st.title("SUNRISE Workflow")
# Runtime Manager Connection
rm_address = os.getenv("RUNTIME_MANAGER_URL", "http://localhost") + ":" + os.getenv("RUNTIME_MANAGER_PORT", "8000")
st.caption(f"Runtime Manager address: *{rm_address}*")
connected, version_message = rmi.get_version(rm_address)
if not connected:
logger.error(f"Could not connect Runtime Manager '{rm_address}'")
st.error("Runtime Manager not connected (unreachable)")
# Main Part
if connected:
st.caption(f"Runtime Manager version: v{version_message}")
if not st.session_state.mnl_active_system.is_available():
logger.debug(f"Getting system details from RM ({rm_address})")
available_systems = rmi.get_systems(rm_address)
if st.session_state.mnl_config.system_version is None:
system = next((sys for sys in available_systems if st.session_state.mnl_config.system_name in sys),
None)
if system is None:
raise ValueError(f"Selected system not found: '{st.session_state.mnl_config.system_name}'")
else:
system = st.session_state.mnl_config.system_name + ":" + st.session_state.mnl_config.system_version
if system not in available_systems:
raise ValueError(f"Selected system not found: '{system}'")
sysdef = rmi.get_system_info(rm_address, system)
st.session_state.mnl_active_system.from_sysdef(sysdef)
st.session_state.mnl_active_system.session_parameters["display_name"].val_user =\
st.session_state.mnl_config.display_name
st.session_state.mnl_active_system.session_parameters["creator"].val_user =\
st.session_state.mnl_config.creator
st.session_state.mnl_active_system.session_parameters["description"].val_user =\
st.session_state.mnl_config.description
st.markdown(f"Using System **`{st.session_state.mnl_active_system.name}`**")
ses_stat = None
if st.session_state.mnl_session_id is not None:
try:
ses_stat = rmi.session_status(rm_address, st.session_state.mnl_session_id)
except rmi.SunriseClientError as exc:
raise RuntimeError("Session Status cannot be retrieved") from exc
session_locked = ses_stat is not None and ses_stat != dataformats.State.CREATED
# Lock parameter inputs on click without disabling the button on the same rerun.
st.session_state.mnl_params_locked = session_locked or st.session_state.get("but_workflow", False)
st.header("Parameters :wrench:")
for param_group in dataformats.ParameterGroup:
for param_key in st.session_state.mnl_config.get_param_keys_to_modify(param_group):
if not st.session_state.mnl_active_system.has_parameter(param_group, param_key):
raise ValueError(f"Parameter '{param_group}:{param_key}' not found in system!")
param_obj = st.session_state.mnl_active_system[param_group][param_key]
param_obj.init_widget_key("mnl_" + param_key)
uiu.visualize_parameter(param_obj, disabled=st.session_state.mnl_params_locked)
st.text("") # Add some vertical space
st.header("Experiment :microscope:")
if st.button(":play_or_pause_button: Run Workflow",
disabled=session_locked, key='but_workflow'):
with st.status("Workflow Execution", expanded=True) as stat:
logger.debug("Starting Workflow execution")
st.session_state["mnl_build_log"] = None
st.session_state["mnl_run_log"] = None
stat.update(label="Creating... :building_construction:", state="running")
# Consider modified parameters
for param_group in dataformats.ParameterGroup:
for param_key in st.session_state.mnl_config.get_param_keys_to_modify(param_group):
param_obj = st.session_state.mnl_active_system[param_group][param_key]
if st.session_state[param_obj.ui_widget_key] != param_obj.val_user:
logger.debug(f"Parameter '{param_obj.key}' was modified")
param_obj.val_user = st.session_state[param_obj.ui_widget_key]
logger.debug("Starting Session creation")
st.session_state.mnl_session_id, res = rmi.session_create(
rm_address, st.session_state.mnl_active_system.to_sescfg())
if res.is_good():
st.write(f":heavy_check_mark: Created Experiment *'{st.session_state.mnl_session_id}'*")
logger.debug(f"Session ID is: {st.session_state.mnl_session_id}")
else:
stat.update(label="Create Failed", state="error")
logger.error("Session create failed")
raise RuntimeError(f"Could not create Experiment: {res.get_message()}")
stat.update(label="Uploading File Parameters... :outbox_tray:", state="running")
for param_group in dataformats.ParameterGroup:
if st.session_state.mnl_active_system.has_parameter_group(param_group):
for param_key in st.session_state.mnl_config.get_param_keys_to_modify(param_group):
if st.session_state.mnl_active_system[param_group][param_key].is_fileparam():
param_obj = st.session_state.mnl_active_system[param_group][param_key]
uploader_value = st.session_state[param_obj.ui_widget_key]
if isinstance(uploader_value, st.runtime.uploaded_file_manager.UploadedFile):
logger.debug(f"Uploading file parameter {param_key}")
success = rmi.session_set_fileparam(rm_address, st.session_state.mnl_session_id,
param_obj, uploader_value.getvalue(),
uploader_value.name)
if success:
st.write(f":heavy_check_mark: File *'{param_key}'* uploaded")
else:
st.error(f"File *'{param_key}'* was not uploaded.")
logger.error("File Upload failed")
stat.update(label="Building... :hammer_and_pick:", state="running")
res = rmi.session_build(rm_address, st.session_state.mnl_session_id)
st.session_state.mnl_build_log = res.get_message()
if res.is_good():
logger.debug("Build finished successfully")
st.write(":heavy_check_mark: Build completed")
else:
logger.error(f"Build failed: {res.get_message()}")
st.error(f"Build failed: {res.get_message()}")
stat.update(label="Build Failed", state="error")
stat.update(label="Running... :repeat:", state="running")
res = rmi.session_run(rm_address, st.session_state.mnl_session_id)
st.session_state.mnl_run_log = res.get_message()
if res.is_good():
logger.debug("Run finished successfully")
st.write(":heavy_check_mark: Run completed")
stat.update(label="Finished", state="complete")
else:
logger.error(f"Run failed: {res.get_message()}")
st.error(f"Run failed: {res.get_message()}")
stat.update(label="Run Failed", state="error")
st.rerun()
if st.session_state.mnl_session_id is not None:
st.markdown(f"Experiment ID: **`{st.session_state.mnl_session_id}`**")
if ses_stat == dataformats.State.RAN:
st.status("**Workflow Completed**", state="complete").markdown(f"Experiment Status: `{ses_stat.value}`")
st.text("") # Add some vertical space
st.header("Result Analysis :bar_chart:")
results = rmi.session_result_list(rm_address, st.session_state.mnl_session_id)
if st.session_state.mnl_config.results is None:
# No specific results selected, display all available
results_to_display = [res for res in results if res.is_available]
else:
system_results = {res.name: res for res in results}
results_to_display = []
for res_name in st.session_state.mnl_config.results:
if res_name in system_results:
if system_results[res_name].is_available:
results_to_display.append(system_results[res_name])
else:
st.warning(f"Requested result object '{res_name}' is not available in this experiment")
else:
st.error(f"Result object '{res_name}' is not known to this system")
for rtd in results_to_display:
logger.debug(f"Displaying result '{rtd.name}'")
result = rmi.fetch_results(rm_address, st.session_state.mnl_session_id, rtd.name)
if result.type == dataformats.resultformats.ResultTypes.GENERIC_TEXT:
uiu.display_result_gentext(result.data)
elif result.type == dataformats.resultformats.ResultTypes.PERFORMANCE:
uiu.display_result_performance(result.data)
elif result.type == dataformats.resultformats.ResultTypes.SIM_SPEED:
uiu.display_result_simspeed(result.data)
else:
st.info(f"Embedded visualization of result '{result.name}' is not implemented.")
elif ses_stat in [dataformats.State.FAILED_BUILD, dataformats.State.FAILED_RUN]:
st.status("**Workflow execution failed**",
state="error").markdown(f"Experiment Status: `{ses_stat.value}`")
st.divider()
if st.session_state.mnl_build_log is not None:
with st.expander("Build Log"):
st.text(st.session_state.mnl_build_log)
if st.session_state.mnl_run_log is not None:
with st.expander("Run Log"):
st.text(st.session_state.mnl_run_log)
st.divider()
if st.button(":wastebasket: Delete Experiment", key="but_remove"):
res = rmi.session_remove(rm_address, st.session_state.mnl_session_id)
if res.is_good():
logger.debug("Experiment successfully removed")
st.session_state.mnl_session_id = None
st.session_state.mnl_params_locked = False
st.rerun()
else:
logger.error(f"Could not remove experiment: {res.get_message()}")
st.error(f"Could not remove experiment '{st.session_state.mnl_session_id}': {res.get_message()}")