Spaces:
Building
Building
Update config_provider.py
Browse files- config_provider.py +40 -13
config_provider.py
CHANGED
@@ -20,6 +20,14 @@ class GlobalConfig(BaseModel):
|
|
20 |
|
21 |
def get_plain_token(self) -> Optional[str]:
|
22 |
return decrypt(self.cloud_token) if self.cloud_token else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# ---------------- Global -----------------
|
25 |
class UserConfig(BaseModel):
|
@@ -166,19 +174,38 @@ class ConfigProvider:
|
|
166 |
if cls._instance is None:
|
167 |
cls._instance = cls._load()
|
168 |
cls._instance.build_index()
|
|
|
169 |
return cls._instance
|
170 |
|
171 |
-
# -------- Internal helpers ------------
|
172 |
@classmethod
|
173 |
-
def
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def get_plain_token(self) -> Optional[str]:
|
22 |
return decrypt(self.cloud_token) if self.cloud_token else None
|
23 |
+
|
24 |
+
def is_cloud_mode(self) -> bool:
|
25 |
+
"""Check if running in cloud mode (hfcloud or cloud)"""
|
26 |
+
return self.work_mode in ("hfcloud", "cloud")
|
27 |
+
|
28 |
+
def is_on_premise(self) -> bool:
|
29 |
+
"""Check if running in on-premise mode"""
|
30 |
+
return self.work_mode == "on-premise"
|
31 |
|
32 |
# ---------------- Global -----------------
|
33 |
class UserConfig(BaseModel):
|
|
|
174 |
if cls._instance is None:
|
175 |
cls._instance = cls._load()
|
176 |
cls._instance.build_index()
|
177 |
+
cls._check_environment_setup()
|
178 |
return cls._instance
|
179 |
|
|
|
180 |
@classmethod
|
181 |
+
def _check_environment_setup(cls):
|
182 |
+
"""Check if environment is properly configured based on work_mode"""
|
183 |
+
config = cls._instance.global_config
|
184 |
+
|
185 |
+
if config.is_cloud_mode():
|
186 |
+
# Cloud mode - check for HuggingFace Secrets
|
187 |
+
missing_secrets = []
|
188 |
+
|
189 |
+
if not os.getenv("JWT_SECRET"):
|
190 |
+
missing_secrets.append("JWT_SECRET")
|
191 |
+
if not os.getenv("FLARE_TOKEN_KEY"):
|
192 |
+
missing_secrets.append("FLARE_TOKEN_KEY")
|
193 |
+
if not os.getenv("SPARK_TOKEN"):
|
194 |
+
missing_secrets.append("SPARK_TOKEN")
|
195 |
+
|
196 |
+
if missing_secrets:
|
197 |
+
log(f"β οΈ Running in {config.work_mode} mode. Missing secrets: {', '.join(missing_secrets)}")
|
198 |
+
log("π Add these in HuggingFace Space Settings β Repository secrets")
|
199 |
+
else:
|
200 |
+
log(f"β
Running in {config.work_mode} mode with all required secrets")
|
201 |
+
|
202 |
+
elif config.is_on_premise():
|
203 |
+
# On-premise mode - check for .env file
|
204 |
+
env_path = Path(".env")
|
205 |
+
if env_path.exists():
|
206 |
+
from dotenv import load_dotenv
|
207 |
+
load_dotenv()
|
208 |
+
log("β
Running in on-premise mode with .env file")
|
209 |
+
else:
|
210 |
+
log("β οΈ Running in on-premise mode but .env file not found")
|
211 |
+
log("π Copy .env.example to .env and configure it")
|