Skip to content

ux: move translator load msg into translator instantiation #1184

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

Merged
merged 9 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions garak/harnesses/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
inherit from.
"""


import importlib
import json
import logging
import types
Expand All @@ -28,18 +28,22 @@ def _initialize_runtime_services():
"""Initialize and validate runtime services required for a successful test"""

from garak.exception import GarakException
import garak.langservice

# TODO: this block may be gated in the future to ensure it is only run once. At this time
# only one harness will execute per run so the output here is reasonable.
try:
msg = "🌐 Loading Language services if required."
logging.info(msg)
print(msg)
garak.langservice.load()
except GarakException as e:
logging.critical("❌ Language setup failed! ❌", exc_info=e)
raise e
service_names = ["garak.langservice"]
for service_name in service_names:
logging.info("service import: " + service_name)
service = importlib.import_module(service_name)
try:
if service.enabled():
symbol, msg = service.start_msg()
if len(msg):
logging.info(msg)
print(f"{symbol} {msg}")
service.load()
except GarakException as e:
logging.critical(f"❌ {service.name} setup failed! ❌", exc_info=e)


class Harness(Configurable):
Expand Down
3 changes: 2 additions & 1 deletion garak/langproviders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class LangProvider(Configurable):
"""Base class for objects that provision language"""

def __init__(self, config_root: dict = {}) -> None:

self._load_config(config_root=config_root)

self.source_lang, self.target_lang = self.language.split(",")
Expand All @@ -149,7 +150,7 @@ def __init__(self, config_root: dict = {}) -> None:
self._load_langprovider()

def _load_langprovider(self):
raise NotImplementedError
pass

def _translate(self, text: str) -> str:
raise NotImplementedError
Expand Down
4 changes: 3 additions & 1 deletion garak/langproviders/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
class Passthru(LangProvider):
"""Stand-in language provision for pass through / noop"""

def _load_langprovider(self):
load_prefix = None

def _load_translator(self):
pass

def _translate(self, text: str) -> str:
Expand Down
23 changes: 22 additions & 1 deletion garak/langservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@


import logging
from garak import _config, _plugins
from typing import List

from garak import _config, _plugins
from garak.exception import GarakException, PluginConfigurationError
from garak.langproviders.base import LangProvider
from garak.langproviders.local import Passthru
Expand All @@ -16,6 +17,26 @@
native_langprovider = None


def tasks() -> List[str]:
"""number of translators to deal with, minus the no-op one"""
models_to_init = [
f"{t['model_name']}:{t['language']}" for t in _config.run.translators
]
return models_to_init


def enabled() -> bool:
"""are all requirements met for language service to be enabled"""
if hasattr(_config.run, "translators"):
return len(_config.run.translators) > 1
return False


def start_msg() -> str:
"""return a start message, assumes enabled"""
return "🌐", "loading language services:" + " ".join(tasks())


def _load_langprovider(language_service: dict = {}) -> LangProvider:
"""Load a single language provider based on the configuration provided."""
langprovider_instance = None
Expand Down