mirror of https://github.com/bmaltais/kohya_ss
95 lines
2.9 KiB
Python
95 lines
2.9 KiB
Python
import os
|
|
import gradio as gr
|
|
from easygui import msgbox
|
|
import subprocess
|
|
import time
|
|
import webbrowser
|
|
from .custom_logging import setup_logging
|
|
|
|
# Set up logging
|
|
log = setup_logging()
|
|
|
|
tensorboard_proc = None
|
|
TENSORBOARD = "tensorboard" if os.name == "posix" else "tensorboard.exe"
|
|
|
|
# Set the default tensorboard port
|
|
DEFAULT_TENSORBOARD_PORT = 6006
|
|
|
|
|
|
def start_tensorboard(headless, logging_dir, wait_time=5):
|
|
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
|
|
global tensorboard_proc
|
|
|
|
headless_bool = True if headless.get("label") == "True" else False
|
|
|
|
# Read the TENSORBOARD_PORT from the environment, or use the default
|
|
tensorboard_port = os.environ.get("TENSORBOARD_PORT", DEFAULT_TENSORBOARD_PORT)
|
|
|
|
# Check if logging directory exists and is not empty; if not, warn the user and exit
|
|
if not os.path.exists(logging_dir) or not os.listdir(logging_dir):
|
|
log.error("Error: logging folder does not exist or does not contain logs.")
|
|
msgbox(msg="Error: logging folder does not exist or does not contain logs.")
|
|
return # Exit the function with an error code
|
|
|
|
run_cmd = [
|
|
TENSORBOARD,
|
|
"--logdir",
|
|
logging_dir,
|
|
"--host",
|
|
"0.0.0.0",
|
|
"--port",
|
|
str(tensorboard_port),
|
|
]
|
|
|
|
log.info(run_cmd)
|
|
if tensorboard_proc is not None:
|
|
log.info(
|
|
"Tensorboard is already running. Terminating existing process before starting new one..."
|
|
)
|
|
stop_tensorboard()
|
|
|
|
# Start background process
|
|
log.info("Starting TensorBoard on port {}".format(tensorboard_port))
|
|
try:
|
|
# Copy the current environment
|
|
env = os.environ.copy()
|
|
|
|
# Set your specific environment variable
|
|
env["TF_ENABLE_ONEDNN_OPTS"] = "0"
|
|
|
|
tensorboard_proc = subprocess.Popen(run_cmd, env=env)
|
|
except Exception as e:
|
|
log.error("Failed to start Tensorboard:", e)
|
|
return
|
|
|
|
if not headless_bool:
|
|
# Wait for some time to allow TensorBoard to start up
|
|
time.sleep(wait_time)
|
|
|
|
# Open the TensorBoard URL in the default browser
|
|
tensorboard_url = f"http://localhost:{tensorboard_port}"
|
|
log.info(f"Opening TensorBoard URL in browser: {tensorboard_url}")
|
|
webbrowser.open(tensorboard_url)
|
|
|
|
|
|
def stop_tensorboard():
|
|
global tensorboard_proc
|
|
if tensorboard_proc is not None:
|
|
log.info("Stopping tensorboard process...")
|
|
try:
|
|
tensorboard_proc.terminate()
|
|
tensorboard_proc = None
|
|
log.info("...process stopped")
|
|
except Exception as e:
|
|
log.error("Failed to stop Tensorboard:", e)
|
|
else:
|
|
log.warning("Tensorboard is not running...")
|
|
|
|
|
|
def gradio_tensorboard():
|
|
with gr.Row():
|
|
button_start_tensorboard = gr.Button("Start tensorboard")
|
|
button_stop_tensorboard = gr.Button("Stop tensorboard")
|
|
|
|
return (button_start_tensorboard, button_stop_tensorboard)
|