diff --git a/ols/utils/environments.py b/ols/utils/environments.py new file mode 100644 index 00000000..79f3f3c4 --- /dev/null +++ b/ols/utils/environments.py @@ -0,0 +1,17 @@ +"""Environment variables handling.""" + +import os +import tempfile + + +def configure_gradio_ui_envs() -> None: + """Configure GradioUI framework environment variables.""" + # disable Gradio analytics, which calls home to https://api.gradio.app + os.environ["GRADIO_ANALYTICS_ENABLED"] = "false" + + # Setup config directory for Matplotlib. It will be used to store info + # about fonts (usually one JSON file) and it really is just temporary + # storage that can be deleted at any time and recreated later. + # Fixes: https://issues.redhat.com/browse/OLS-301 + tempdir = os.path.join(tempfile.gettempdir(), "matplotlib") + os.environ["MPLCONFIGDIR"] = tempdir diff --git a/runner.py b/runner.py index fd56bf97..032e645e 100644 --- a/runner.py +++ b/runner.py @@ -2,7 +2,6 @@ import logging import os -import tempfile import threading from pathlib import Path @@ -12,6 +11,7 @@ from ols.src.auth.auth import use_k8s_auth from ols.utils import ssl from ols.utils.certificates import generate_certificates_file +from ols.utils.environments import configure_gradio_ui_envs from ols.utils.logging_configurator import configure_logging @@ -29,19 +29,6 @@ def configure_hugging_face_envs(ols_config: config_model.OLSConfig) -> None: os.environ["TRANSFORMERS_OFFLINE"] = "1" -def configure_gradio_ui_envs() -> None: - """Configure GradioUI framework environment variables.""" - # disable Gradio analytics, which calls home to https://api.gradio.app - os.environ["GRADIO_ANALYTICS_ENABLED"] = "false" - - # Setup config directory for Matplotlib. It will be used to store info - # about fonts (usually one JSON file) and it really is just temporary - # storage that can be deleted at any time and recreated later. - # Fixes: https://issues.redhat.com/browse/OLS-301 - tempdir = os.path.join(tempfile.gettempdir(), "matplotlib") - os.environ["MPLCONFIGDIR"] = tempdir - - def load_index(): """Load the index.""" # accessing the config's rag_index property will trigger the loading diff --git a/tests/unit/utils/test_environments.py b/tests/unit/utils/test_environments.py new file mode 100644 index 00000000..357b36a9 --- /dev/null +++ b/tests/unit/utils/test_environments.py @@ -0,0 +1,21 @@ +"""Unit tests for functions defined in environments.py.""" + +import os +from unittest.mock import patch + +from ols.utils.environments import configure_gradio_ui_envs + + +@patch.dict(os.environ, {"GRADIO_ANALYTICS_ENABLED": "", "MPLCONFIGDIR": ""}) +def test_configure_gradio_ui_envs(): + """Test the function configure_gradio_ui_envs.""" + # setup before tested function is called + assert os.environ.get("GRADIO_ANALYTICS_ENABLED", None) == "" + assert os.environ.get("MPLCONFIGDIR", None) == "" + + # call the tested function + configure_gradio_ui_envs() + + # expected environment variables + assert os.environ.get("GRADIO_ANALYTICS_ENABLED", None) == "false" + assert os.environ.get("MPLCONFIGDIR", None) != ""