mjpsm's picture
Deploy MyVillage Activity Generation API
9c9ef1d verified
Raw History Blame Contribute Delete
2.99 kB
"""Environment-based API configuration.
A service URL can be either a base URL (discovered via /openapi.json)
or a full POST URL if the endpoint is known.
"""
from dataclasses import dataclass
import os
@dataclass(frozen=True)
class Settings:
progression_api_url: str = "https://mjpsm-activity-progression-api.hf.space"
composition_api_url: str = "https://mjpsm-activity-composition-api.hf.space"
instruction_api_url: str = "https://mjpsm-activity-instruction-api.hf.space"
hf_token: str = ""
api_key: str = ""
read_timeout_seconds: float = 240.0
max_attempts: int = 2
# Body-shape switches are for differences between separately deployed APIs.
# direct: {"progression": {...}}
# input: {"input": {"progression": {...}}}
# bare: just the progression object
composition_input_style: str = "direct"
# direct: {"progression": {...}, "composition": {"activity_title": ..., ...}}
# nested_output: {"progression": {...}, "composition": {"output": {...}}}
# input: {"input": {"progression": {...}, "composition": {...}}}
instruction_input_style: str = "direct"
@classmethod
def from_env(cls) -> "Settings":
return cls(
progression_api_url=os.getenv("PROGRESSION_API_URL", cls.progression_api_url),
composition_api_url=os.getenv("COMPOSITION_API_URL", cls.composition_api_url),
instruction_api_url=os.getenv("INSTRUCTION_API_URL", cls.instruction_api_url),
hf_token=os.getenv("HF_TOKEN", ""),
api_key=os.getenv("API_KEY", ""),
read_timeout_seconds=float(os.getenv("READ_TIMEOUT_SECONDS", "240")),
max_attempts=int(os.getenv("MAX_ATTEMPTS", "2")),
composition_input_style=os.getenv("COMPOSITION_INPUT_STYLE", "direct"),
instruction_input_style=os.getenv("INSTRUCTION_INPUT_STYLE", "direct"),
)
def validate(self) -> None:
if self.composition_input_style not in {"direct", "input", "bare"}:
raise ValueError("COMPOSITION_INPUT_STYLE must be direct, input, or bare")
if self.instruction_input_style not in {"direct", "nested_output", "input", "input_nested_output"}:
raise ValueError("INSTRUCTION_INPUT_STYLE must be direct, nested_output, input, or input_nested_output")
if not 1 <= self.max_attempts <= 5:
raise ValueError("MAX_ATTEMPTS must be between 1 and 5")
if self.read_timeout_seconds <= 0:
raise ValueError("READ_TIMEOUT_SECONDS must be positive")
for url in (self.progression_api_url, self.composition_api_url, self.instruction_api_url):
if not url.startswith("https://") and not url.startswith("http://"):
raise ValueError("Every upstream API URL must begin with http:// or https://")
if url.rstrip("/").endswith(("/docs", "/redoc", "/openapi.json")):
raise ValueError("Use a base URL or the actual POST endpoint, not /docs or /openapi.json")