Skip to content

factory

factory

Public construction paths for GLiNER-backed Anonymizer clients.

Classes:

Name Description
NativeGliner

Request the library-managed native GLiNER2 runtime.

GlinerEndpoint

Connect to an already-managed GLiNER-compatible HTTP endpoint.

NativeRuntimeSession

A native endpoint and client factory valid for its enclosing context.

Functions:

Name Description
create_anonymizer

Construct an Anonymizer with native or caller-managed GLiNER detection.

native_runtime

Run native GLiNER for the duration of a client session.

NativeGliner pydantic-model

Bases: BaseModel

Request the library-managed native GLiNER2 runtime.

Config:

  • extra: forbid
  • frozen: True

Fields:

  • device (DeviceRequest)
  • dtype (DtypeRequest)
  • attention (AttentionRequest)

GlinerEndpoint pydantic-model

Bases: BaseModel

Connect to an already-managed GLiNER-compatible HTTP endpoint.

Config:

  • extra: forbid
  • frozen: True

Fields:

  • url (str)
  • model (str)
  • api_key_env (str | None)

NativeRuntimeSession(endpoint) dataclass

A native endpoint and client factory valid for its enclosing context.

Methods:

Name Description
create_anonymizer

Construct a client connected to this session's endpoint.

create_anonymizer(*, model_configs=None, model_providers=None, artifact_path=None, data_designer_run_config=None)

Construct a client connected to this session's endpoint.

Source code in src/anonymizer/interface/factory.py
def create_anonymizer(
    self,
    *,
    model_configs: str | Path | None = None,
    model_providers: list[ModelProvider] | str | Path | None = None,
    artifact_path: str | Path | None = None,
    data_designer_run_config: RunConfig | None = None,
) -> Anonymizer:
    """Construct a client connected to this session's endpoint."""
    parsed, providers = _parse_gliner_model_inputs(model_configs=model_configs, model_providers=model_providers)
    return _create_endpoint_anonymizer(
        endpoint=self.endpoint,
        extra_body=NATIVE_DETECTION_DEFAULTS,
        parsed=parsed,
        providers=providers,
        artifact_path=artifact_path,
        data_designer_run_config=data_designer_run_config,
    )

create_anonymizer(*, gliner, model_configs=None, model_providers=None, artifact_path=None, data_designer_run_config=None)

Construct an Anonymizer with native or caller-managed GLiNER detection.

NativeGliner owns a library-managed singleton process. GlinerEndpoint only configures a client connection and never starts or stops the endpoint.

Source code in src/anonymizer/interface/factory.py
def create_anonymizer(
    *,
    gliner: NativeGliner | GlinerEndpoint,
    model_configs: str | Path | None = None,
    model_providers: list[ModelProvider] | str | Path | None = None,
    artifact_path: str | Path | None = None,
    data_designer_run_config: RunConfig | None = None,
) -> Anonymizer:
    """Construct an Anonymizer with native or caller-managed GLiNER detection.

    ``NativeGliner`` owns a library-managed singleton process. ``GlinerEndpoint``
    only configures a client connection and never starts or stops the endpoint.
    """
    if not isinstance(gliner, (NativeGliner, GlinerEndpoint)):
        raise TypeError("gliner must be NativeGliner or GlinerEndpoint")
    parsed, providers = _parse_gliner_model_inputs(model_configs=model_configs, model_providers=model_providers)
    match gliner:
        case NativeGliner():
            from anonymizer.engine.local_inference.runtime import singleton_runtime

            with singleton_runtime(gliner.device, gliner.dtype, gliner.attention) as runtime:
                return _create_endpoint_anonymizer(
                    endpoint=_native_endpoint(runtime),
                    parsed=parsed,
                    providers=providers,
                    artifact_path=artifact_path,
                    data_designer_run_config=data_designer_run_config,
                    extra_body=NATIVE_DETECTION_DEFAULTS,
                )
        case GlinerEndpoint():
            return _create_endpoint_anonymizer(
                endpoint=gliner,
                parsed=parsed,
                providers=providers,
                artifact_path=artifact_path,
                data_designer_run_config=data_designer_run_config,
                extra_body=None,
            )

native_runtime(*, device='auto', dtype='auto', attention='auto')

Run native GLiNER for the duration of a client session.

An existing singleton is borrowed and remains running after exit. Nested contexts share the process; clients of an owned process expire on exit.

Source code in src/anonymizer/interface/factory.py
@contextmanager
def native_runtime(
    *, device: DeviceRequest = "auto", dtype: DtypeRequest = "auto", attention: AttentionRequest = "auto"
) -> Generator[NativeRuntimeSession]:
    """Run native GLiNER for the duration of a client session.

    An existing singleton is borrowed and remains running after exit. Nested
    contexts share the process; clients of an owned process expire on exit.
    """
    from anonymizer.engine.local_inference.runtime import runtime_context

    request = NativeGliner(device=device, dtype=dtype, attention=attention)
    with runtime_context(request.device, request.dtype, request.attention) as runtime:
        yield NativeRuntimeSession(endpoint=_native_endpoint(runtime))