Fix(tensorboard): Check for executable to prevent crash

I replaced the direct import of `tensorflow` with a check for the `tensorboard` executable. This resolves the `illegal hardware instruction` crash that occurred on systems with CPUs lacking AVX2 support.

The new implementation ensures that the TensorBoard feature remains available if the `tensorboard` command is in the system's PATH, without causing a fatal error when the `tensorflow` library is incompatible with the underlying hardware.
pull/3343/head
google-labs-jules[bot] 2025-07-13 13:50:08 +00:00
parent 4161d1d80a
commit f6f3f35329
2 changed files with 27 additions and 4 deletions

View File

@ -3,12 +3,15 @@ import gradio as gr
import subprocess
import time
import webbrowser
import shutil
try:
os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
import tensorflow # Attempt to import tensorflow to check if it is installed
# os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0"
# import tensorflow # Attempt to import tensorflow to check if it is installed
if shutil.which("tensorboard"):
visibility = True
else:
visibility = False
except ImportError:
visibility = False

View File

@ -0,0 +1,20 @@
import unittest
from unittest.mock import patch
import importlib
class TestTensorboardVisibility(unittest.TestCase):
@patch('shutil.which', return_value='/usr/bin/tensorboard')
def test_tensorboard_visibility_when_tensorboard_is_present(self, mock_which):
import kohya_gui.class_tensorboard
importlib.reload(kohya_gui.class_tensorboard)
self.assertTrue(kohya_gui.class_tensorboard.visibility)
@patch('shutil.which', return_value=None)
def test_tensorboard_visibility_when_tensorboard_is_absent(self, mock_which):
import kohya_gui.class_tensorboard
importlib.reload(kohya_gui.class_tensorboard)
self.assertFalse(kohya_gui.class_tensorboard.visibility)
if __name__ == '__main__':
unittest.main()