-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
93 lines (71 loc) · 2.87 KB
/
config.py
File metadata and controls
93 lines (71 loc) · 2.87 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import yaml
from pathlib import Path
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# ================= Path Configuration =================
# Project Root: Parent directory of this config file
BASE_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = BASE_DIR
# Path to the YAML configuration file
CONFIG_YAML_PATH = PROJECT_ROOT / "config.yaml"
# Default fallback path for audio and image
DEFAULT_AUDIO_PATH = "data/audio_cache"
DEFAULT_IMAGE_PATH = "data/image_cache"
# Load YAML configuration
config_data = {}
if CONFIG_YAML_PATH.exists():
try:
with open(CONFIG_YAML_PATH, "r", encoding="utf-8") as f:
config_data = yaml.safe_load(f) or {}
except Exception as e:
print(f"Warning: Failed to load config.yaml: {e}")
# --- 1. Audio Cache Directory Setup ---
storage_conf = config_data.get("storage", {})
raw_audio_path = storage_conf.get("audio_cache_path", DEFAULT_AUDIO_PATH)
if os.path.isabs(raw_audio_path):
AUDIO_CACHE_DIR = Path(raw_audio_path)
else:
AUDIO_CACHE_DIR = PROJECT_ROOT / raw_audio_path
try:
AUDIO_CACHE_DIR.mkdir(parents=True, exist_ok=True)
except Exception as e:
print(f"Error creating audio directory at {AUDIO_CACHE_DIR}: {e}")
AUDIO_CACHE_DIR = PROJECT_ROOT / "data" / "audio_cache"
AUDIO_CACHE_DIR.mkdir(parents=True, exist_ok=True)
# --- 1.1 Image Cache Directory Setup ---
raw_image_path = storage_conf.get("image_cache_path", DEFAULT_IMAGE_PATH)
if os.path.isabs(raw_image_path):
IMAGE_CACHE_DIR = Path(raw_image_path)
else:
IMAGE_CACHE_DIR = PROJECT_ROOT / raw_image_path
try:
IMAGE_CACHE_DIR.mkdir(parents=True, exist_ok=True)
except Exception as e:
print(f"Error creating image directory at {IMAGE_CACHE_DIR}: {e}")
IMAGE_CACHE_DIR = PROJECT_ROOT / "data" / "image_cache"
IMAGE_CACHE_DIR.mkdir(parents=True, exist_ok=True)
# --- 2. General Data Directory ---
DATA_DIR = PROJECT_ROOT / "data"
DATA_DIR.mkdir(parents=True, exist_ok=True)
# ================= Model & Network Configuration =================
model_conf = config_data.get("models", {})
# Models from YAML (or defaults)
LLM_MODEL = model_conf.get("llm", "o3-mini")
TTS_MODEL = model_conf.get("tts", "tts-1-hd")
TTS_VOICE = model_conf.get("tts_voice", "alloy")
# --- LLM API Configuration ---
# 1. API Key: Priority -> LLM_API_KEY > OPENAI_API_KEY
LLM_API_KEY = os.getenv("LLM_API_KEY") or os.getenv("OPENAI_API_KEY")
# 2. Base URL: Priority -> LLM_BASE_URL > OPENAI_BASE_URL > DEEPSEEK_BASE_URL > Default
LLM_BASE_URL = (
os.getenv("LLM_BASE_URL")
or os.getenv("OPENAI_BASE_URL")
or os.getenv("DEEPSEEK_BASE_URL")
or "https://api.openai.com/v1"
)
# --- TTS API Configuration ---
# 独立配置 TTS 的接口和密钥。如果未设置,则默认使用 LLM 的配置
TTS_API_KEY = os.getenv("TTS_API_KEY") or LLM_API_KEY
TTS_BASE_URL = os.getenv("TTS_BASE_URL") or LLM_BASE_URL