refactor: move model definitions into providers and extract resolve module
- Rename ImageProvider to BlackForestProvider, TextProvider to MistralProvider - Add get_provided_models() abstract method to Provider base class - Move model lists from models.py into each provider's get_provided_models() - Add providers/registry.py to aggregate models from all providers - Extract infer_required_capabilities and resolve_model from config.py to resolve.py - Update tests to use new names and import paths
This commit is contained in:
parent
dc6a75f5c4
commit
d0dac5b1bf
13 changed files with 432 additions and 390 deletions
|
|
@ -4,13 +4,15 @@ from __future__ import annotations
|
|||
|
||||
import enum
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Self
|
||||
from typing import Self
|
||||
|
||||
import yaml
|
||||
from pydantic import BaseModel, model_validator
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from bulkgen.providers.models import Capability, ModelInfo
|
||||
from bulkgen.providers.models import Capability
|
||||
|
||||
IMAGE_EXTENSIONS: frozenset[str] = frozenset({".png", ".jpg", ".jpeg", ".webp"})
|
||||
TEXT_EXTENSIONS: frozenset[str] = frozenset({".md", ".txt"})
|
||||
|
||||
|
||||
class TargetType(enum.Enum):
|
||||
|
|
@ -20,10 +22,6 @@ class TargetType(enum.Enum):
|
|||
TEXT = "text"
|
||||
|
||||
|
||||
IMAGE_EXTENSIONS: frozenset[str] = frozenset({".png", ".jpg", ".jpeg", ".webp"})
|
||||
TEXT_EXTENSIONS: frozenset[str] = frozenset({".md", ".txt"})
|
||||
|
||||
|
||||
class Defaults(BaseModel):
|
||||
"""Default model names, applied when a target does not specify its own."""
|
||||
|
||||
|
|
@ -57,36 +55,6 @@ class ProjectConfig(BaseModel):
|
|||
return self
|
||||
|
||||
|
||||
def infer_required_capabilities(
|
||||
target_name: str, target: TargetConfig
|
||||
) -> frozenset[Capability]:
|
||||
"""Infer the capabilities a model must have based on filename and config.
|
||||
|
||||
Raises :class:`ValueError` for unsupported file extensions.
|
||||
"""
|
||||
from bulkgen.providers.models import Capability
|
||||
|
||||
suffix = Path(target_name).suffix.lower()
|
||||
caps: set[Capability] = set()
|
||||
|
||||
if suffix in IMAGE_EXTENSIONS:
|
||||
caps.add(Capability.TEXT_TO_IMAGE)
|
||||
if target.reference_images:
|
||||
caps.add(Capability.REFERENCE_IMAGES)
|
||||
if target.control_images:
|
||||
caps.add(Capability.CONTROL_IMAGES)
|
||||
elif suffix in TEXT_EXTENSIONS:
|
||||
caps.add(Capability.TEXT_GENERATION)
|
||||
all_input_names = list(target.inputs) + list(target.reference_images)
|
||||
if any(Path(n).suffix.lower() in IMAGE_EXTENSIONS for n in all_input_names):
|
||||
caps.add(Capability.VISION)
|
||||
else:
|
||||
msg = f"Cannot infer target type for '{target_name}': unsupported extension '{suffix}'"
|
||||
raise ValueError(msg)
|
||||
|
||||
return frozenset(caps)
|
||||
|
||||
|
||||
def target_type_from_capabilities(capabilities: frozenset[Capability]) -> TargetType:
|
||||
"""Derive the target type from a set of required capabilities."""
|
||||
from bulkgen.providers.models import Capability
|
||||
|
|
@ -96,59 +64,6 @@ def target_type_from_capabilities(capabilities: frozenset[Capability]) -> Target
|
|||
return TargetType.TEXT
|
||||
|
||||
|
||||
def resolve_model(
|
||||
target_name: str, target: TargetConfig, defaults: Defaults
|
||||
) -> ModelInfo:
|
||||
"""Return the effective model for a target, validated against required capabilities.
|
||||
|
||||
If the target specifies an explicit model, it is validated to have all
|
||||
required capabilities. Otherwise the type-appropriate default is tried
|
||||
first; if it lacks a required capability the first capable model of the
|
||||
same type is selected.
|
||||
|
||||
Raises :class:`ValueError` if no suitable model can be found.
|
||||
"""
|
||||
from bulkgen.providers.models import ALL_MODELS
|
||||
|
||||
required = infer_required_capabilities(target_name, target)
|
||||
target_type = target_type_from_capabilities(required)
|
||||
|
||||
if target.model is not None:
|
||||
# Explicit model — look up and validate.
|
||||
for model in ALL_MODELS:
|
||||
if model.name == target.model:
|
||||
missing = required - frozenset(model.capabilities)
|
||||
if missing:
|
||||
names = ", ".join(sorted(missing))
|
||||
msg = f"Model '{target.model}' for target '{target_name}' lacks required capabilities: {names}"
|
||||
raise ValueError(msg)
|
||||
return model
|
||||
msg = f"Unknown model '{target.model}' for target '{target_name}'"
|
||||
raise ValueError(msg)
|
||||
|
||||
# No explicit model — try the default first, then fall back.
|
||||
default_name = (
|
||||
defaults.image_model if target_type is TargetType.IMAGE else defaults.text_model
|
||||
)
|
||||
for model in ALL_MODELS:
|
||||
if model.name == default_name:
|
||||
if required <= frozenset(model.capabilities):
|
||||
return model
|
||||
break
|
||||
|
||||
# Default lacks capabilities — find the first capable model of the same type.
|
||||
model_type = "image" if target_type is TargetType.IMAGE else "text"
|
||||
for model in ALL_MODELS:
|
||||
if model.type == model_type and required <= frozenset(model.capabilities):
|
||||
return model
|
||||
|
||||
names = ", ".join(sorted(required))
|
||||
msg = (
|
||||
f"No model found for target '{target_name}' with required capabilities: {names}"
|
||||
)
|
||||
raise ValueError(msg)
|
||||
|
||||
|
||||
def load_config(config_path: Path) -> ProjectConfig:
|
||||
"""Load and validate a ``.bulkgen.yaml`` file."""
|
||||
with config_path.open() as f:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue