kkt-2002 commited on
Commit
54c600e
·
1 Parent(s): 83da10a

Complete fix for all permission and HTML entity errors

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import tempfile
3
 
4
- # FIX: Use system temp directory for DeepFace cache (guaranteed writable)
5
  deepface_cache = os.path.join(tempfile.gettempdir(), "deepface_cache")
6
  os.makedirs(deepface_cache, exist_ok=True)
7
  os.environ["DEEPFACE_HOME"] = deepface_cache
@@ -23,7 +23,6 @@ from deepface import DeepFace
23
  from sklearn.metrics.pairwise import cosine_similarity
24
  import tensorflow as tf
25
 
26
-
27
  # Optimize TensorFlow for HuggingFace Spaces
28
  tf.config.threading.set_intra_op_parallelism_threads(1)
29
  tf.config.threading.set_inter_op_parallelism_threads(1)
@@ -111,20 +110,24 @@ def download_file_from_google_drive(file_id, destination):
111
  return False
112
 
113
  def setup_models():
114
- """Download and setup all required models"""
115
- os.makedirs('models', exist_ok=True)
116
- os.makedirs('models/anti-spoofing', exist_ok=True)
 
 
 
 
117
 
118
  # Model configurations with your Google Drive IDs
119
  models_config = {
120
  'yolov5s-face.onnx': {
121
  'drive_id': '1sybYq9GGriXN6sY8YV1-RXMeVqYzhDrV',
122
- 'path': 'models/yolov5s-face.onnx',
123
  'required': True
124
  },
125
  'AntiSpoofing_bin_1.5_128.onnx': {
126
  'drive_id': '1nH5G7dAHFE2KlW_H65txc8GDKSB7Zpy4',
127
- 'path': 'models/anti-spoofing/AntiSpoofing_bin_1.5_128.onnx',
128
  'required': True
129
  }
130
  }
@@ -150,11 +153,11 @@ def setup_models():
150
  print(f"Anti-Spoof Model: {'✅ Available' if antispoof_downloaded else '❌ Failed'}")
151
  print("="*50 + "\n")
152
 
153
- return yolo_downloaded, antispoof_downloaded
154
 
155
  # Initialize models on startup
156
  print("Setting up models...")
157
- yolo_available, antispoof_available = setup_models()
158
 
159
  # ---------------- YOLO Face Detection (FIXED) ----------------
160
 
@@ -372,9 +375,9 @@ def decode_image(base64_image):
372
  image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
373
  return image
374
 
375
- # Model paths
376
- YOLO_FACE_MODEL_PATH = "models/yolov5s-face.onnx"
377
- ANTI_SPOOF_BIN_MODEL_PATH = "models/anti-spoofing/AntiSpoofing_bin_1.5_128.onnx"
378
 
379
  # Initialize models with timeout protection
380
  yolo_face = None
 
1
  import os
2
  import tempfile
3
 
4
+ # FIX: Use system temp directory for DeepFace and models (guaranteed writable)
5
  deepface_cache = os.path.join(tempfile.gettempdir(), "deepface_cache")
6
  os.makedirs(deepface_cache, exist_ok=True)
7
  os.environ["DEEPFACE_HOME"] = deepface_cache
 
23
  from sklearn.metrics.pairwise import cosine_similarity
24
  import tensorflow as tf
25
 
 
26
  # Optimize TensorFlow for HuggingFace Spaces
27
  tf.config.threading.set_intra_op_parallelism_threads(1)
28
  tf.config.threading.set_inter_op_parallelism_threads(1)
 
110
  return False
111
 
112
  def setup_models():
113
+ """Download and setup all required models - FIXED VERSION"""
114
+ # FIX: Use temp directory for models (guaranteed writable)
115
+ models_dir = os.path.join(tempfile.gettempdir(), 'models')
116
+ antispoof_dir = os.path.join(models_dir, 'anti-spoofing')
117
+
118
+ os.makedirs(models_dir, exist_ok=True)
119
+ os.makedirs(antispoof_dir, exist_ok=True)
120
 
121
  # Model configurations with your Google Drive IDs
122
  models_config = {
123
  'yolov5s-face.onnx': {
124
  'drive_id': '1sybYq9GGriXN6sY8YV1-RXMeVqYzhDrV',
125
+ 'path': os.path.join(models_dir, 'yolov5s-face.onnx'),
126
  'required': True
127
  },
128
  'AntiSpoofing_bin_1.5_128.onnx': {
129
  'drive_id': '1nH5G7dAHFE2KlW_H65txc8GDKSB7Zpy4',
130
+ 'path': os.path.join(antispoof_dir, 'AntiSpoofing_bin_1.5_128.onnx'),
131
  'required': True
132
  }
133
  }
 
153
  print(f"Anti-Spoof Model: {'✅ Available' if antispoof_downloaded else '❌ Failed'}")
154
  print("="*50 + "\n")
155
 
156
+ return yolo_downloaded, antispoof_downloaded, models_config
157
 
158
  # Initialize models on startup
159
  print("Setting up models...")
160
+ yolo_available, antispoof_available, model_paths = setup_models()
161
 
162
  # ---------------- YOLO Face Detection (FIXED) ----------------
163
 
 
375
  image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
376
  return image
377
 
378
+ # Model paths (using temp directory)
379
+ YOLO_FACE_MODEL_PATH = model_paths['yolov5s-face.onnx']['path']
380
+ ANTI_SPOOF_BIN_MODEL_PATH = model_paths['AntiSpoofing_bin_1.5_128.onnx']['path']
381
 
382
  # Initialize models with timeout protection
383
  yolo_face = None