prthm11 commited on
Commit
9de2607
·
verified ·
1 Parent(s): 00ce374

Upload config.py

Browse files
Files changed (1) hide show
  1. scratch_vision/config.py +27 -1
scratch_vision/config.py CHANGED
@@ -2,6 +2,7 @@
2
  from __future__ import annotations
3
 
4
  import os
 
5
  from dataclasses import dataclass
6
  from functools import lru_cache
7
  from pathlib import Path
@@ -119,12 +120,37 @@ class Settings:
119
  raise RuntimeError(f"Asset library not found at {self.assets_dir}")
120
 
121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
  @lru_cache(maxsize=1)
123
  def get_settings() -> Settings:
124
  load_dotenv(PROJECT_ROOT / ".env")
125
  weights = tuple(float(w) for w in _env_str("MATCH_WEIGHTS", "0.5,0.3,0.2").split(","))
126
  return Settings(
127
- base_dir=Path(_env_str("APP_BASE_DIR") or PROJECT_ROOT).resolve(),
128
  assets_dir_name=_env_str("ASSETS_DIR", "Webashlar_kodomo_assets"),
129
  host=_env_str("HOST", "0.0.0.0"),
130
  port=_env_int("PORT", 7860),
 
2
  from __future__ import annotations
3
 
4
  import os
5
+ import warnings
6
  from dataclasses import dataclass
7
  from functools import lru_cache
8
  from pathlib import Path
 
120
  raise RuntimeError(f"Asset library not found at {self.assets_dir}")
121
 
122
 
123
+ def _resolve_base_dir() -> Path:
124
+ """APP_BASE_DIR if it is usable, else the project folder.
125
+
126
+ A stale APP_BASE_DIR (e.g. /app from an older deployment) must not stop the app from booting:
127
+ on Hugging Face Spaces the container runs as uid 1000 and cannot create directories under /.
128
+ """
129
+ configured = _env_str("APP_BASE_DIR")
130
+ if not configured:
131
+ return PROJECT_ROOT.resolve()
132
+ path = Path(configured).resolve()
133
+ try:
134
+ path.mkdir(parents=True, exist_ok=True)
135
+ if os.access(path, os.W_OK):
136
+ return path
137
+ except OSError:
138
+ pass
139
+ warnings.warn(
140
+ f"APP_BASE_DIR={configured} is not writable; using {PROJECT_ROOT} instead. "
141
+ "Remove or fix the APP_BASE_DIR variable.",
142
+ RuntimeWarning,
143
+ stacklevel=2,
144
+ )
145
+ return PROJECT_ROOT.resolve()
146
+
147
+
148
  @lru_cache(maxsize=1)
149
  def get_settings() -> Settings:
150
  load_dotenv(PROJECT_ROOT / ".env")
151
  weights = tuple(float(w) for w in _env_str("MATCH_WEIGHTS", "0.5,0.3,0.2").split(","))
152
  return Settings(
153
+ base_dir=_resolve_base_dir(),
154
  assets_dir_name=_env_str("ASSETS_DIR", "Webashlar_kodomo_assets"),
155
  host=_env_str("HOST", "0.0.0.0"),
156
  port=_env_int("PORT", 7860),