File size: 2,374 Bytes
7f9dfed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from __future__ import annotations

import os
from dataclasses import dataclass

LOCAL_DEPLOYMENT = "local"
SPACE_DEPLOYMENT = "space"
VALID_DEPLOYMENTS = {LOCAL_DEPLOYMENT, SPACE_DEPLOYMENT}
PLACEHOLDER_BACKEND = "placeholder"


@dataclass(frozen=True)
class DeploymentPolicy:
    mode: str = LOCAL_DEPLOYMENT

    @property
    def is_space(self) -> bool:
        return self.mode == SPACE_DEPLOYMENT

    @property
    def allow_placeholder_backend(self) -> bool:
        return not self.is_space

    @property
    def allow_demo_mode(self) -> bool:
        return not self.is_space


def deployment_mode(env_value: str | None = None) -> str:
    raw_value = env_value if env_value is not None else os.getenv("WORKBENCH_DEPLOYMENT", "")
    value = raw_value.strip().lower() or LOCAL_DEPLOYMENT
    if value not in VALID_DEPLOYMENTS:
        raise ValueError(
            "WORKBENCH_DEPLOYMENT must be 'local' or 'space'; "
            f"got {raw_value!r}."
        )
    return value


def current_policy() -> DeploymentPolicy:
    return DeploymentPolicy(deployment_mode())


def filter_backends_for_policy(backends: list[str], policy: DeploymentPolicy) -> list[str]:
    if policy.allow_placeholder_backend:
        return backends
    return [backend for backend in backends if backend != PLACEHOLDER_BACKEND]


def default_backend_for_policy(
    backends: list[str],
    preferred: str,
    policy: DeploymentPolicy,
) -> str:
    allowed = filter_backends_for_policy(backends, policy)
    if preferred in allowed:
        return preferred
    if allowed:
        return allowed[0]
    raise ValueError("No real backend is available for this deployment policy.")


def ensure_backend_allowed(backend: str, policy: DeploymentPolicy) -> None:
    if backend == PLACEHOLDER_BACKEND and not policy.allow_placeholder_backend:
        raise ValueError(
            "Placeholder inference is disabled in WORKBENCH_DEPLOYMENT=space. "
            "Select a real backend such as transformers, Ollama, llama.cpp, "
            "SGLang, or an OpenAI-compatible local server."
        )


def ensure_demo_mode_allowed(policy: DeploymentPolicy) -> None:
    if not policy.allow_demo_mode:
        raise ValueError(
            "Demo/no-model mode is disabled in WORKBENCH_DEPLOYMENT=space. "
            "Use a real OpenBMB model configuration for the deployed app."
        )