Skip to content

Commit 25d8186

Browse files
committed
ux: move translator load msg into translator instantiation (#1184)
2 parents aef7f1e + 8ba91b0 commit 25d8186

File tree

4 files changed

+48
-16
lines changed

4 files changed

+48
-16
lines changed

docs/source/translation.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ Configuration file
5959
Translation function is configured in the ``run`` section of a configuration with the following keys:
6060

6161
``target_lang`` - A single ``BCP47`` entry designating the language of the target under test. "ja", "fr", "jap" etc.
62-
translators - A list of language pair designated translator configurations.
62+
``langproviders`` - A list of language pair designated translator configurations.
6363

6464
* Note: The `Helsinki-NLP/opus-mt-{source},{target}` case uses different language formats. The language codes used to name models are inconsistent.
6565
Two-letter codes can usually be found `here <https://developers.google.com/admin-sdk/directory/v1/languages>`_, while three-letter codes require
6666
a search such as “language code {code}". More details can be found `here <https://github.com/Helsinki-NLP/OPUS-MT-train/tree/master/models>`_.
6767

68-
A translator configuration is provided using the project's configurable pattern with the following required keys:
68+
A language provider configuration is provided using the project's configurable pattern with the following keys:
6969

70-
* ``language`` - A ``,`` separated pair of ``BCP47`` entires describing translation format provided by the configuration
71-
* ``model_type`` - the module and optional instance class to be instantiated. local, remote, remote.DeeplTranslator etc.
72-
* ``model_name`` - (optional) the model name loaded for translation, required for ``local`` translator model_type
70+
* ``language`` - (required) A ``,`` separated pair of ``BCP47`` entires describing translation format provided by the configuration
71+
* ``model_type`` - (required) the ``langproviders`` module and optional instance class to be instantiated; ``local``, ``remote``, ``remote.DeeplTranslator`` etc.
72+
* ``model_name`` - (conditional) the model name loaded for translation. This field is required for ``local`` translator ``model_type``
7373

7474
(Optional) Model specific parameters defined by the translator model type may exist.
7575

garak/harnesses/base.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
inherit from.
1111
"""
1212

13-
13+
import importlib
1414
import json
1515
import logging
1616
import types
@@ -28,18 +28,23 @@ def _initialize_runtime_services():
2828
"""Initialize and validate runtime services required for a successful test"""
2929

3030
from garak.exception import GarakException
31-
import garak.langservice
3231

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

4449

4550
class Harness(Configurable):

garak/langproviders/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class LangProvider(Configurable):
140140
"""Base class for objects that provision language"""
141141

142142
def __init__(self, config_root: dict = {}) -> None:
143+
143144
self._load_config(config_root=config_root)
144145

145146
self.source_lang, self.target_lang = self.language.split(",")

garak/langservice.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77

88
import logging
9-
from garak import _config, _plugins
9+
from typing import List
1010

11+
from garak import _config, _plugins
1112
from garak.exception import GarakException, PluginConfigurationError
1213
from garak.langproviders.base import LangProvider
1314
from garak.langproviders.local import Passthru
@@ -16,6 +17,31 @@
1617
native_langprovider = None
1718

1819

20+
def tasks() -> List[str]:
21+
"""number of translators to deal with, minus the no-op one"""
22+
models_to_init = []
23+
for t in _config.run.langproviders:
24+
if t["model_type"] == "local.Passthru": # extra guard
25+
continue
26+
model_descr = f"{t['language']}->{t['model_type']}"
27+
if "model_name" in t:
28+
model_descr += f"[{t['model_name']}]"
29+
models_to_init.append(model_descr)
30+
return models_to_init
31+
32+
33+
def enabled() -> bool:
34+
"""are all requirements met for language service to be enabled"""
35+
if hasattr(_config.run, "langproviders"):
36+
return len(_config.run.langproviders) > 1
37+
return False
38+
39+
40+
def start_msg() -> str:
41+
"""return a start message, assumes enabled"""
42+
return "🌐", "loading language services: " + " ".join(tasks())
43+
44+
1945
def _load_langprovider(language_service: dict = {}) -> LangProvider:
2046
"""Load a single language provider based on the configuration provided."""
2147
langprovider_instance = None

0 commit comments

Comments
 (0)