Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cache the Config #933

Merged
merged 10 commits into from
May 21, 2024
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@
"python.testing.cwd": "${workspaceFolder}/code",
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"pylint.path" : [ "${interpreter}", "-m", "pylint" ]
ross-p-smith marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import json
import logging
import functools
from string import Template

from ..azure_blob_storage_client import AzureBlobStorageClient
Expand Down Expand Up @@ -164,6 +165,7 @@ def _set_new_config_properties(config: dict, default_config: dict):
]

@staticmethod
@functools.cache
def get_active_config_or_default():
env_helper = EnvHelper()
config = ConfigHelper.get_default_config()
Expand Down Expand Up @@ -191,6 +193,7 @@ def save_config_as_active(config):
CONFIG_FILE_NAME,
content_type="application/json",
)
ConfigHelper.get_active_config_or_default.cache_clear()

@staticmethod
def validate_config(config: dict):
Expand All @@ -214,8 +217,8 @@ def get_default_config():

config_file_path = os.path.join(os.path.dirname(__file__), "default.json")

with open(config_file_path) as f:
logger.info(f"Loading default config from {config_file_path}")
with open(config_file_path, encoding="utf-8") as f:
logger.info("Loading default config from %s", config_file_path)
ConfigHelper._default_config = json.loads(
Template(f.read()).substitute(
ORCHESTRATION_STRATEGY=env_helper.ORCHESTRATION_STRATEGY
Expand All @@ -229,6 +232,7 @@ def get_default_config():
@staticmethod
def clear_config():
ConfigHelper._default_config = None
ConfigHelper.get_active_config_or_default.cache_clear()

@staticmethod
def _append_advanced_image_processors():
Expand Down
18 changes: 10 additions & 8 deletions code/backend/pages/04_Configuration.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import streamlit as st
import json
import jsonschema
import os
import traceback
import sys
import traceback
import json
import jsonschema
import streamlit as st
from batch.utilities.helpers.env_helper import EnvHelper
from batch.utilities.helpers.config.config_helper import ConfigHelper
from azure.core.exceptions import ResourceNotFoundError
Expand All @@ -18,14 +18,14 @@
menu_items=None,
)

mod_page_style = """
MOD_PAGE_STYLE = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
header {visibility: hidden;}
</style>
"""
st.markdown(mod_page_style, unsafe_allow_html=True)
st.markdown(MOD_PAGE_STYLE, unsafe_allow_html=True)

config = ConfigHelper.get_active_config_or_default()

Expand Down Expand Up @@ -379,9 +379,11 @@ def validate_documents():
),
}
ConfigHelper.save_config_as_active(current_config)
st.success("Configuration saved successfully!")
st.success(
"Configuration saved successfully! Please restart the chat service for these changes to take effect."
)

with st.popover(":red[Reset confiiguration to defaults]"):
with st.popover(":red[Reset configuration to defaults]"):
st.write(
ross-p-smith marked this conversation as resolved.
Show resolved Hide resolved
"**Resetting the configuration cannot be reversed, proceed with caution!**"
)
Expand Down
1 change: 1 addition & 0 deletions code/tests/utilities/helpers/test_config_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def env_helper_mock():
@pytest.fixture(autouse=True)
def reset_default_config():
ConfigHelper._default_config = None
ConfigHelper.get_active_config_or_default.cache_clear()
yield
ConfigHelper._default_config = None

Expand Down
Loading