Skip to content

Commit

Permalink
feat(core): add Engine kwargs alternative to cli args
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulHax committed Dec 16, 2024
1 parent 9ca54f6 commit 2eb3d0d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
42 changes: 25 additions & 17 deletions src/nrtk_explorer/app/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from nrtk_explorer.library.filtering import FilterProtocol
from nrtk_explorer.library.dataset import get_dataset, expand_hugging_face_datasets
from nrtk_explorer.library.debounce import debounce
from nrtk_explorer.library.app_config import process_config


from nrtk_explorer.app.images.images import Images
from nrtk_explorer.app.embeddings import EmbeddingsApp
Expand Down Expand Up @@ -38,36 +40,42 @@
# Engine class
# ---------------------------------------------------------

config_options = {
"dataset": {
"flags": ["--dataset"],
"params": {
"nargs": "+",
"default": DEFAULT_DATASETS,
"help": "Path to the JSON file describing the image dataset",
},
},
"download": {
"flags": ["--download"],
"params": {
"action": "store_true",
"default": False,
"help": "Download Hugging Face Hub datasets instead of streaming them",
},
},
}


class Engine(Applet):
def __init__(self, server=None):
def __init__(self, server=None, **kwargs):
super().__init__(server)

self.server.cli.add_argument(
"--dataset",
nargs="+",
default=DEFAULT_DATASETS,
help="Path to the JSON file describing the image dataset",
)

self.server.cli.add_argument(
"--download",
action="store_true",
default=False,
help="Download Hugging Face Hub datasets instead of streaming them",
)
config = process_config(self.server.cli, config_options, **kwargs)

known_args, _ = self.server.cli.parse_known_args()
dataset_identifiers = expand_hugging_face_datasets(
known_args.dataset, not known_args.download
config["dataset"], not config["download"]
)
self.input_paths = dataset_identifiers
self.state.current_dataset = self.input_paths[0]

images = Images(server=self.server)

self._transforms_app = TransformsApp(
server=self.server.create_child_server(), images=images
server=self.server.create_child_server(), images=images, **kwargs
)

self._embeddings_app = EmbeddingsApp(
Expand Down
33 changes: 20 additions & 13 deletions src/nrtk_explorer/app/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import nrtk_explorer.library.nrtk_transforms as nrtk_trans
import nrtk_explorer.library.yaml_transforms as nrtk_yaml
from nrtk_explorer.library import object_detector
from nrtk_explorer.app.applet import Applet
from nrtk_explorer.app.parameters import ParametersApp
from nrtk_explorer.app.images.image_meta import update_image_meta, dataset_id_to_meta
from nrtk_explorer.library.app_config import process_config
from nrtk_explorer.library.scoring import (
compute_score,
)
from nrtk_explorer.app.trame_utils import change_checker, delete_state

from nrtk_explorer.app.applet import Applet
from nrtk_explorer.app.parameters import ParametersApp
from nrtk_explorer.app.images.image_meta import update_image_meta, dataset_id_to_meta
from nrtk_explorer.app.trame_utils import change_checker, delete_state
from nrtk_explorer.app.images.image_ids import (
dataset_id_to_image_id,
dataset_id_to_transformed_image_id,
Expand Down Expand Up @@ -86,6 +87,18 @@ def on_change_feature_enabled(self, **kwargs):
self.enabled_callback()


config_options = {
"models": {
"flags": ["--models"],
"params": {
"nargs": "+",
"default": INFERENCE_MODELS_DEFAULT,
"help": "Space separated list of inference models",
},
},
}


class TransformsApp(Applet):
def __init__(
self,
Expand All @@ -94,18 +107,12 @@ def __init__(
ground_truth_annotations=None,
original_detection_annotations=None,
transformed_detection_annotations=None,
**kwargs,
):
super().__init__(server)

self.server.cli.add_argument(
"--models",
nargs="+",
default=INFERENCE_MODELS_DEFAULT,
help="Space separated list of inference models",
)

known_args, _ = self.server.cli.parse_known_args()
self.state.inference_models = known_args.models
config = process_config(self.server.cli, config_options, **kwargs)
self.state.inference_models = config["models"]
self.state.inference_model = self.state.inference_models[0]
self.state.setdefault("image_list_ids", [])
self.state.setdefault("dataset_ids", [])
Expand Down
12 changes: 12 additions & 0 deletions src/nrtk_explorer/library/app_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def process_config(cli, config_options, **kwargs):
for opt in config_options.values():
cli.add_argument(*opt["flags"], **opt["params"])
known_args, _ = cli.parse_known_args()

config = {}
for name in config_options:
if name in kwargs:
config[name] = kwargs[name]
else:
config[name] = getattr(known_args, name)
return config

0 comments on commit 2eb3d0d

Please sign in to comment.