128 lines
4.6 KiB
Python
128 lines
4.6 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from .local_whisper import LocalWhisperConfig
|
|
|
|
|
|
PROJECT_DIR = Path(__file__).resolve().parent.parent
|
|
ENV_PATH = PROJECT_DIR / ".env"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class DeepSeekConfig:
|
|
api_key: str
|
|
base_url: str
|
|
model: str
|
|
audio_model: str
|
|
provider: str
|
|
protocol: str
|
|
translation_batch_size: int
|
|
timeout_seconds: int
|
|
|
|
|
|
def read_env_file(path: Path = ENV_PATH) -> dict[str, str]:
|
|
if not path.exists():
|
|
return {}
|
|
|
|
values: dict[str, str] = {}
|
|
for raw_line in path.read_text(encoding="utf-8-sig").splitlines():
|
|
line = raw_line.strip()
|
|
if not line or line.startswith("#") or "=" not in line:
|
|
continue
|
|
key, value = line.split("=", 1)
|
|
values[key.strip().lstrip("\ufeff")] = value.strip().strip('"').strip("'")
|
|
return values
|
|
|
|
|
|
def get_config_value(key: str, default: str = "") -> str:
|
|
env_values = read_env_file()
|
|
return os.getenv(key, env_values.get(key, default)).strip()
|
|
|
|
|
|
def load_text_ai_config() -> DeepSeekConfig:
|
|
api_key = (
|
|
get_config_value("TEXT_AI_API_KEY")
|
|
or get_config_value("AI_API_KEY")
|
|
or get_config_value("DEEPSEEK_API_KEY")
|
|
)
|
|
base_url = (
|
|
get_config_value("TEXT_AI_BASE_URL")
|
|
or get_config_value("AI_BASE_URL")
|
|
or get_config_value("DEEPSEEK_BASE_URL", "https://api.deepseek.com")
|
|
)
|
|
model = (
|
|
get_config_value("TEXT_AI_MODEL")
|
|
or get_config_value("AI_TEXT_MODEL")
|
|
or get_config_value("DEEPSEEK_MODEL", "deepseek-chat")
|
|
)
|
|
return DeepSeekConfig(
|
|
api_key=api_key,
|
|
base_url=base_url.rstrip("/"),
|
|
model=model,
|
|
audio_model=get_config_value("TEXT_AI_AUDIO_MODEL") or get_config_value("AI_AUDIO_MODEL") or model,
|
|
provider=get_config_value("TEXT_AI_PROVIDER") or get_config_value("AI_PROVIDER", "deepseek") or "deepseek",
|
|
protocol=(get_config_value("TEXT_AI_PROTOCOL") or get_config_value("AI_API_PROTOCOL", "openai") or "openai").lower(),
|
|
translation_batch_size=parse_non_negative_int_config(get_config_value("TEXT_AI_BATCH_SIZE") or get_config_value("AI_TRANSLATION_BATCH_SIZE", "40"), 40),
|
|
timeout_seconds=parse_int_config(get_config_value("TEXT_AI_TIMEOUT_SECONDS") or get_config_value("AI_TIMEOUT_SECONDS", "120"), 120),
|
|
)
|
|
|
|
|
|
def load_local_whisper_config() -> LocalWhisperConfig:
|
|
return LocalWhisperConfig(
|
|
enabled=parse_bool_config(get_config_value("LOCAL_WHISPER_ENABLED", "false")),
|
|
model=get_config_value("LOCAL_WHISPER_MODEL", "small") or "small",
|
|
device=get_config_value("LOCAL_WHISPER_DEVICE", "cpu") or "cpu",
|
|
compute_type=get_config_value("LOCAL_WHISPER_COMPUTE_TYPE", "int8") or "int8",
|
|
)
|
|
|
|
|
|
def load_deepseek_config() -> DeepSeekConfig:
|
|
api_key = get_config_value("AI_API_KEY") or get_config_value("DEEPSEEK_API_KEY")
|
|
base_url = get_config_value("AI_BASE_URL") or get_config_value("DEEPSEEK_BASE_URL", "https://api.deepseek.com")
|
|
model = get_config_value("AI_TEXT_MODEL") or get_config_value("DEEPSEEK_MODEL", "deepseek-chat")
|
|
return DeepSeekConfig(
|
|
api_key=api_key,
|
|
base_url=base_url.rstrip("/"),
|
|
model=model,
|
|
audio_model=get_config_value("AI_AUDIO_MODEL") or model,
|
|
provider=get_config_value("AI_PROVIDER", "deepseek") or "deepseek",
|
|
protocol=(get_config_value("AI_API_PROTOCOL", "openai") or "openai").lower(),
|
|
translation_batch_size=parse_int_config(get_config_value("AI_TRANSLATION_BATCH_SIZE", "5"), 5),
|
|
timeout_seconds=parse_int_config(get_config_value("AI_TIMEOUT_SECONDS", "180"), 180),
|
|
)
|
|
|
|
|
|
def get_ai_status() -> dict[str, object]:
|
|
text_config = load_text_ai_config()
|
|
return {
|
|
"configured": bool(text_config.api_key),
|
|
"provider": text_config.provider,
|
|
"protocol": text_config.protocol,
|
|
"model": text_config.model,
|
|
"localWhisperEnabled": load_local_whisper_config().enabled,
|
|
"message": "AI API 已配置。" if text_config.api_key else "未配置 TEXT_AI_API_KEY,请在 .env 中填写。",
|
|
}
|
|
|
|
|
|
def parse_int_config(value: str, default: int) -> int:
|
|
try:
|
|
parsed = int(str(value).strip())
|
|
except (TypeError, ValueError):
|
|
return default
|
|
return parsed if parsed > 0 else default
|
|
|
|
|
|
def parse_non_negative_int_config(value: str, default: int) -> int:
|
|
try:
|
|
parsed = int(str(value).strip())
|
|
except (TypeError, ValueError):
|
|
return default
|
|
return parsed if parsed >= 0 else default
|
|
|
|
|
|
def parse_bool_config(value: str) -> bool:
|
|
return str(value or "").strip().lower() in {"1", "true", "yes", "on"}
|