PixelNavigation commited on
Commit
974aa56
·
1 Parent(s): 9d4b8ac

Disabled download of pretrained weights

Browse files
backend/app.py CHANGED
@@ -8,8 +8,37 @@ import re
8
  import numpy as np
9
  from PIL import Image
10
  import gc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- # Add the dfdc_deepfake_challenge directory to the path
13
  sys.path.append('dfdc_model')
14
 
15
  from dfdc_model.kernel_utils import VideoReader, FaceExtractor, confident_strategy, predict_on_video
@@ -18,7 +47,7 @@ from enhanced_prediction import enhanced_predict_on_video, enhanced_predict_on_i
18
 
19
  app = Flask(__name__, static_folder='static', static_url_path='')
20
  CORS(app)
21
- import tempfile
22
 
23
  # Choose an upload folder that is writable in container environments.
24
  # Prefer environment override, then /tmp/uploads, then a tempdir fallback.
@@ -135,9 +164,21 @@ def initialize_preprocessing():
135
 
136
  # Load models and initialize preprocessing
137
  print("Loading models...")
138
- models = load_models()
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  face_extractor, input_size, frames_per_video = initialize_preprocessing()
140
- print(f"Loaded {len(models)} models")
141
 
142
  def predict_on_image(image_path):
143
  """Predict on a single image using face extraction and model inference"""
 
8
  import numpy as np
9
  from PIL import Image
10
  import gc
11
+ import tempfile
12
+ import traceback
13
+
14
+ def _ensure_writable_caches():
15
+ """Ensure ML framework caches use writable temp dirs to avoid PermissionError on /.cache."""
16
+ tmp = tempfile.gettempdir()
17
+ defaults = {
18
+ 'HF_HOME': os.path.join(tmp, 'hf_home'),
19
+ 'XDG_CACHE_HOME': os.path.join(tmp, '.cache'),
20
+ 'TRANSFORMERS_CACHE': os.path.join(tmp, 'hf_home'),
21
+ 'TORCH_HOME': os.path.join(tmp, 'torch'),
22
+ 'HF_DATASETS_CACHE': os.path.join(tmp, '.cache', 'huggingface', 'datasets'),
23
+ 'HF_MODULES_CACHE': os.path.join(tmp, '.cache', 'huggingface', 'modules'),
24
+ 'HF_HUB_CACHE': os.path.join(tmp, '.cache', 'huggingface', 'hub'),
25
+ 'NO_ALBUMENTATIONS_UPDATE': '1',
26
+ 'HF_HUB_OFFLINE': '1', # prevent network downloads during startup
27
+ }
28
+ for k, v in defaults.items():
29
+ if k not in os.environ:
30
+ os.environ[k] = v
31
+ # create a subset of directories best-effort
32
+ for path in [os.environ.get('HF_HOME'), os.environ.get('XDG_CACHE_HOME'), os.environ.get('TORCH_HOME')]:
33
+ try:
34
+ if path:
35
+ os.makedirs(path, exist_ok=True)
36
+ except Exception:
37
+ pass
38
+
39
+ _ensure_writable_caches()
40
 
41
+ # Add the model directory to path and import after cache env is set
42
  sys.path.append('dfdc_model')
43
 
44
  from dfdc_model.kernel_utils import VideoReader, FaceExtractor, confident_strategy, predict_on_video
 
47
 
48
  app = Flask(__name__, static_folder='static', static_url_path='')
49
  CORS(app)
50
+ # tempfile already imported above
51
 
52
  # Choose an upload folder that is writable in container environments.
53
  # Prefer environment override, then /tmp/uploads, then a tempdir fallback.
 
164
 
165
  # Load models and initialize preprocessing
166
  print("Loading models...")
167
+ models = []
168
+ try:
169
+ models = load_models()
170
+ print(f"Loaded {len(models)} models")
171
+ except Exception:
172
+ tb = traceback.format_exc()
173
+ log_path = os.path.join(tempfile.gettempdir(), 'startup_error.log')
174
+ try:
175
+ with open(log_path, 'w', encoding='utf-8') as f:
176
+ f.write('Model loading failed. Traceback:\n')
177
+ f.write(tb)
178
+ except Exception as write_err:
179
+ print(f"Failed writing startup error log: {write_err}")
180
+ print(f"Model loading failed; continuing with 0 models. Trace written to {log_path}")
181
  face_extractor, input_size, frames_per_video = initialize_preprocessing()
 
182
 
183
  def predict_on_image(image_path):
184
  """Predict on a single image using face extraction and model inference"""
backend/dfdc_model/training/zoo/classifiers.py CHANGED
@@ -42,9 +42,12 @@ encoder_params = {
42
  "features": 2304,
43
  "init_op": partial(tf_efficientnet_b6_ns, pretrained=True, drop_path_rate=0.2)
44
  },
 
 
 
45
  "tf_efficientnet_b7_ns": {
46
  "features": 2560,
47
- "init_op": partial(tf_efficientnet_b7_ns, pretrained=True, drop_path_rate=0.2)
48
  },
49
  "tf_efficientnet_b6_ns_04d": {
50
  "features": 2304,
 
42
  "features": 2304,
43
  "init_op": partial(tf_efficientnet_b6_ns, pretrained=True, drop_path_rate=0.2)
44
  },
45
+ # Set pretrained=False here to avoid timm attempting to download weights at startup in
46
+ # restricted environments (e.g. Hugging Face Space container without write perms to /.cache).
47
+ # Local checkpoint weights will still be loaded afterwards by our application code.
48
  "tf_efficientnet_b7_ns": {
49
  "features": 2560,
50
+ "init_op": partial(tf_efficientnet_b7_ns, pretrained=False, drop_path_rate=0.2)
51
  },
52
  "tf_efficientnet_b6_ns_04d": {
53
  "features": 2304,