|
| 1 | +import streamlit as st |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import time |
| 5 | +import psutil |
| 6 | +import tempfile |
| 7 | +from comet_ml import API |
| 8 | +import streamlit.components.v1 as components |
| 9 | +import zipfile |
| 10 | +import argparse |
| 11 | +import urllib.parse |
| 12 | + |
| 13 | +st.set_page_config(layout="wide") |
| 14 | + |
| 15 | +if "current_profile" not in st.session_state: |
| 16 | + st.session_state["current_profile"] = None |
| 17 | + |
| 18 | +@st.cache_data(show_spinner="Installing Python Packages...", persist="disk") |
| 19 | +def install_packages(*packages): |
| 20 | + packages_str = " ".join([("%s" % package) for package in packages]) |
| 21 | + os.system('pip install %s' % packages_str) |
| 22 | + |
| 23 | +install_packages( |
| 24 | + "tensorboard", "tensorflow", "tensorboard_plugin_profile", "streamlit_js_eval" |
| 25 | +) |
| 26 | + |
| 27 | +# After installations |
| 28 | +from streamlit_js_eval import get_page_location |
| 29 | + |
| 30 | +def lookup_proc(name): |
| 31 | + for pid in psutil.pids(): |
| 32 | + try: |
| 33 | + process = psutil.Process(pid) |
| 34 | + except Exception: |
| 35 | + continue |
| 36 | + |
| 37 | + if ( |
| 38 | + process.name().startswith(name) |
| 39 | + ): |
| 40 | + return process |
| 41 | + |
| 42 | +api = API() |
| 43 | +experiments = api.get_panel_experiments() |
| 44 | + |
| 45 | +class EmptyExperiment: |
| 46 | + id = None |
| 47 | + name = "" |
| 48 | + |
| 49 | +experiments_with_log = [EmptyExperiment()] |
| 50 | +for experiment in experiments: |
| 51 | + asset_list = experiment.get_asset_list() |
| 52 | + for asset in asset_list: |
| 53 | + if asset["type"] == "tensorflow-file": |
| 54 | + experiments_with_log.append(experiment) |
| 55 | + break |
| 56 | + |
| 57 | +if len(experiments_with_log) == 1: |
| 58 | + st.write("No experiments with log") |
| 59 | + st.stop() |
| 60 | +elif len(experiments_with_log) == 2: |
| 61 | + selected_experiment = experiments_with_log[1] |
| 62 | +else: |
| 63 | + selected_experiment = st.selectbox( |
| 64 | + "Select Experiment with log:", |
| 65 | + experiments_with_log, |
| 66 | + format_func=lambda aexp: aexp.name |
| 67 | + ) |
| 68 | + |
| 69 | +if selected_experiment.id: |
| 70 | + page_location = get_page_location() |
| 71 | + if page_location is not None: |
| 72 | + download_path = selected_experiment.id |
| 73 | + |
| 74 | + if not os.path.exists(download_path): |
| 75 | + bar = st.progress(0, "Downloading log files...") |
| 76 | + selected_experiment.download_tensorflow_folder(download_path) |
| 77 | + bar.empty() |
| 78 | + |
| 79 | + selected_log = st.selectbox( |
| 80 | + "Select Profile Run:", |
| 81 | + [""] + os.listdir("./%s/logs/" % selected_experiment.id) |
| 82 | + ) |
| 83 | + if selected_log: |
| 84 | + if st.session_state["current_profile"] != (selected_experiment.id, selected_log): |
| 85 | + proc = lookup_proc("tensorboard") |
| 86 | + if proc: |
| 87 | + #print("Killing!") |
| 88 | + proc.terminate() |
| 89 | + proc.kill() |
| 90 | + proc.wait() |
| 91 | + |
| 92 | + st.session_state["current_profile"] = (selected_experiment.id, selected_log) |
| 93 | + log_dir = "./%s/logs/%s" % (selected_experiment.id, selected_log) |
| 94 | + parsed_args = [ |
| 95 | + "--logdir", log_dir, |
| 96 | + "--port", "6007", |
| 97 | + #"--reuse_port", "True", |
| 98 | + ] |
| 99 | + (stdout_fd, stdout_path) = tempfile.mkstemp(prefix=".tensorboard-stdout-") |
| 100 | + (stderr_fd, stderr_path) = tempfile.mkstemp(prefix=".tensorboard-stderr-") |
| 101 | + try: |
| 102 | + p = subprocess.Popen( |
| 103 | + ["tensorboard"] + parsed_args, |
| 104 | + stdout=stdout_fd, |
| 105 | + stderr=stderr_fd, |
| 106 | + ) |
| 107 | + except OSError as e: |
| 108 | + raise |
| 109 | + finally: |
| 110 | + os.close(stdout_fd) |
| 111 | + os.close(stderr_fd) |
| 112 | + |
| 113 | + seconds = 5 |
| 114 | + bar = st.progress(0, "Starting Tensorboard...") |
| 115 | + for i in range(seconds): |
| 116 | + bar.progress(((i + 1) / seconds), "Starting Tensorboard...") |
| 117 | + time.sleep(1) |
| 118 | + bar.empty() |
| 119 | + |
| 120 | + path, _ = page_location["pathname"].split("/component") |
| 121 | + url = page_location["origin"] + path + "/port/6007/server#profile" |
| 122 | + st.markdown('<a href="%s" style="text-decoration: auto;">⛶ Open in tab</a>' % url, unsafe_allow_html=True) |
| 123 | + components.iframe(src=url, height=700) |
0 commit comments