-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
66 lines (50 loc) · 1.91 KB
/
config.py
File metadata and controls
66 lines (50 loc) · 1.91 KB
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
import os
import yaml
from dataclasses import dataclass, field
from pathlib import Path
@dataclass
class VisionConfig:
base_url: str = "https://api.openai.com/v1"
api_key: str = ""
model: str = "gpt-4o"
max_tokens: int = 512
image_max_size: int = 1920
timeout: float = 600.0
@dataclass
class PromptsConfig:
system: str = (
"You are an expert photo metadata specialist. Analyze images carefully "
"and return accurate, descriptive tags and captions. Always respond with valid JSON only."
)
user: str = (
'Analyze this image and return a JSON object with exactly two fields: '
'"tags" (array of concise keyword strings) and "caption" (one sentence description).'
)
@dataclass
class ServerConfig:
host: str = "0.0.0.0"
port: int = 8000
@dataclass
class Config:
vision: VisionConfig = field(default_factory=VisionConfig)
prompts: PromptsConfig = field(default_factory=PromptsConfig)
server: ServerConfig = field(default_factory=ServerConfig)
def load_config(path: str = "config.yaml") -> Config:
cfg = Config()
if Path(path).exists():
with open(path) as f:
data = yaml.safe_load(f) or {}
if v := data.get("vision"):
cfg.vision = VisionConfig(**{k: val for k, val in v.items() if hasattr(VisionConfig, k)})
if p := data.get("prompts"):
cfg.prompts = PromptsConfig(**{k: val for k, val in p.items() if hasattr(PromptsConfig, k)})
if s := data.get("server"):
cfg.server = ServerConfig(**{k: val for k, val in s.items() if hasattr(ServerConfig, k)})
# Environment variable overrides (useful for secrets)
if key := os.environ.get("VISION_API_KEY"):
cfg.vision.api_key = key
if url := os.environ.get("VISION_BASE_URL"):
cfg.vision.base_url = url
if model := os.environ.get("VISION_MODEL"):
cfg.vision.model = model
return cfg