Skip to content

Commit

Permalink
OLS-1229: GradioUI environment variables
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Tisnovsky <[email protected]>
  • Loading branch information
tisnik authored and jameswnl committed Nov 22, 2024
1 parent 01c50dc commit 4015b1a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
17 changes: 17 additions & 0 deletions ols/utils/environments.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 1 addition & 14 deletions runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import logging
import os
import tempfile
import threading
from pathlib import Path

Expand All @@ -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


Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/utils/test_environments.py
Original file line number Diff line number Diff line change
@@ -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) != ""

0 comments on commit 4015b1a

Please sign in to comment.