Refactor detector and model management (#23995)

* Refactor detector and model management

* Fix model resolution field
This commit is contained in:
Nicolas Mowen
2026-08-18 09:11:50 -05:00
committed by GitHub
parent 39ac0b4712
commit 7576c49be8
63 changed files with 2052 additions and 1152 deletions
+6 -85
View File
@@ -210,78 +210,6 @@ def generate_section_translation(config_class: type) -> dict[str, Any]:
return extract_translations_from_schema(schema)
def get_detector_translations(
config_schema: dict[str, Any],
) -> tuple[dict[str, Any], dict[str, Any], set[str]]:
"""Build detector type translations with nested fields based on schema definitions.
Returns a tuple of (type_translations, shared_fields, nested_field_keys).
Shared fields (identical across all detector types) are returned separately
to avoid duplication in the output.
"""
defs = config_schema.get("$defs", {})
detector_schema = defs.get("DetectorConfig", {})
discriminator = detector_schema.get("discriminator", {})
mapping = discriminator.get("mapping", {})
# First pass: collect all nested fields per detector type
all_nested: dict[str, dict[str, Any]] = {}
type_meta: dict[str, dict[str, str]] = {}
for detector_type, ref in mapping.items():
if not isinstance(ref, str) or not ref.startswith("#/$defs/"):
continue
ref_name = ref.split("/")[-1]
ref_schema = defs.get(ref_name, {})
if not ref_schema:
continue
meta: dict[str, str] = {}
title = ref_schema.get("title")
description = ref_schema.get("description")
if title:
meta["label"] = title
if description:
meta["description"] = description
type_meta[detector_type] = meta
nested = extract_translations_from_schema(ref_schema, defs=defs)
all_nested[detector_type] = {
k: v for k, v in nested.items() if k not in ("label", "description")
}
# Find fields that are identical across all types that have them
shared_fields: dict[str, Any] = {}
if all_nested:
# Collect all field keys across all types
all_keys: set[str] = set()
for nested in all_nested.values():
all_keys.update(nested.keys())
for key in all_keys:
values = [nested[key] for nested in all_nested.values() if key in nested]
if len(values) == len(all_nested) and all(v == values[0] for v in values):
shared_fields[key] = values[0]
# Build per-type translations with only unique (non-shared) fields
type_translations: dict[str, Any] = {}
nested_field_keys: set[str] = set()
for detector_type, nested in all_nested.items():
type_entry: dict[str, Any] = {}
type_entry.update(type_meta.get(detector_type, {}))
unique_fields = {k: v for k, v in nested.items() if k not in shared_fields}
if unique_fields:
type_entry.update(unique_fields)
nested_field_keys.update(unique_fields.keys())
if type_entry:
type_translations[detector_type] = type_entry
return type_translations, shared_fields, nested_field_keys
def main():
"""Main function to generate config translations."""
@@ -337,6 +265,12 @@ def main():
if args and len(args) > 1:
field_type = args[1] # Get value type from Dict[key, value]
# Handle List[SomeModel] - extract the item type
if origin is list:
args = get_args(field_type)
if args:
field_type = args[0]
# Start with field's top-level metadata (label, description)
section_data = get_field_translations(field_info)
@@ -351,19 +285,6 @@ def main():
}
section_data.update(nested_without_root)
if field_name == "detectors":
detector_types, shared_fields, detector_field_keys = (
get_detector_translations(config_schema)
)
# Add shared fields at the base detectors level
section_data.update(shared_fields)
# Add per-type translations (only unique fields per type)
section_data.update(detector_types)
for key in detector_field_keys:
if key == "type":
continue
section_data.pop(key, None)
if field_name == "objects":
# Produce a parallel `filters_attribute` block alongside `filters`,
# with object-wording rewritten for attribute filters (face,