-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
184 lines (165 loc) · 5.31 KB
/
config.py
File metadata and controls
184 lines (165 loc) · 5.31 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
Configuration Management for Moshi TTS API
==========================================
Centralized configuration using pydantic-settings for type-safe,
validated environment variable management.
"""
from functools import lru_cache
from typing import Optional, List
from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
"""
Application settings loaded from environment variables or .env file.
All settings have sensible defaults and are validated on startup.
Override any setting by setting the corresponding environment variable.
"""
# ==========================================
# Server Configuration
# ==========================================
host: str = Field(
default="0.0.0.0",
description="Server host address"
)
port: int = Field(
default=8000,
description="Server port number"
)
log_level: str = Field(
default="info",
description="Logging level (debug, info, warning, error)"
)
workers: int = Field(
default=1,
description="Number of uvicorn worker processes"
)
# ==========================================
# API Configuration
# ==========================================
api_version: str = Field(
default="1.1.0",
description="API version number"
)
api_title: str = Field(
default="Moshi TTS API",
description="API title shown in documentation"
)
max_text_length: int = Field(
default=5000,
description="Maximum text length for synthesis (characters)"
)
# ==========================================
# Model Configuration
# ==========================================
default_tts_repo: str = Field(
default="kyutai/tts-1.6b-en_fr",
description="HuggingFace repository for TTS model"
)
default_voice_repo: str = Field(
default="kyutai/tts-voices",
description="HuggingFace repository for voice presets"
)
sample_rate: int = Field(
default=24000,
description="Audio sample rate in Hz"
)
model_device: Optional[str] = Field(
default=None,
description="Force device (cuda/cpu). Auto-detect if None"
)
model_dtype: str = Field(
default="auto",
description="Model dtype (auto/bfloat16/float32)"
)
model_n_q: int = Field(
default=32,
description="Number of codebooks for model"
)
model_temp: float = Field(
default=0.6,
description="Temperature for generation"
)
model_cfg_coef: float = Field(
default=2.0,
description="CFG coefficient for generation"
)
# ==========================================
# Performance Configuration
# ==========================================
max_workers: int = Field(
default=2,
description="Maximum thread pool workers for synthesis"
)
hf_home: str = Field(
default="/app/models",
description="HuggingFace cache directory"
)
transformers_cache: str = Field(
default="/app/models",
description="Transformers cache directory"
)
# ==========================================
# CORS Configuration
# ==========================================
cors_origins: str = Field(
default="*",
description="CORS allowed origins (comma-separated or *)"
)
cors_credentials: bool = Field(
default=True,
description="CORS allow credentials"
)
cors_methods: str = Field(
default="*",
description="CORS allowed methods"
)
cors_headers: str = Field(
default="*",
description="CORS allowed headers"
)
# ==========================================
# Environment Settings
# ==========================================
environment: str = Field(
default="production",
description="Environment name (development/staging/production)"
)
debug: bool = Field(
default=False,
description="Enable debug mode"
)
@property
def cors_origins_list(self) -> List[str]:
"""
Parse CORS_ORIGINS into a list.
Returns:
List of allowed origins. ["*"] if wildcard is used.
"""
if self.cors_origins == "*":
return ["*"]
return [origin.strip() for origin in self.cors_origins.split(",")]
@property
def is_production(self) -> bool:
"""Check if running in production environment"""
return self.environment.lower() == "production"
@property
def is_development(self) -> bool:
"""Check if running in development environment"""
return self.environment.lower() == "development"
model_config = SettingsConfigDict(
env_file=".env",
env_file_encoding="utf-8",
case_sensitive=False, # Allow HOST or host
extra="ignore", # Ignore extra environment variables
protected_namespaces=() # Allow model_* field names
)
@lru_cache()
def get_settings() -> Settings:
"""
Get cached settings instance.
Uses lru_cache to ensure settings are loaded only once.
This is the recommended way to access settings throughout the application.
Returns:
Settings instance with all configuration loaded
"""
return Settings()