juanquy commited on
Commit
56af25f
·
1 Parent(s): 676a099

Disable PostgreSQL database connections on Hugging Face Spaces

Browse files
src/liveness/liveness_detector.py CHANGED
@@ -348,6 +348,8 @@ class LivenessDetector:
348
  def _log_metrics(self, is_live: bool, confidence: float, attack_type: Optional[str],
349
  processing_time_ms: float, image_hash: str, face_count: int, request_id: str):
350
  """Log liveness detection metrics to TimescaleDB if available"""
 
 
351
  try:
352
  import psycopg2
353
  import datetime
 
348
  def _log_metrics(self, is_live: bool, confidence: float, attack_type: Optional[str],
349
  processing_time_ms: float, image_hash: str, face_count: int, request_id: str):
350
  """Log liveness detection metrics to TimescaleDB if available"""
351
+ if os.environ.get('HF_SPACE', '0') == '1':
352
+ return
353
  try:
354
  import psycopg2
355
  import datetime
src/middleware/api_auth.py CHANGED
@@ -12,6 +12,8 @@ import os
12
  IS_HF_SPACE = os.environ.get('HF_SPACE', '0') == '1'
13
 
14
  def get_db_connection():
 
 
15
  if not HAS_POSTGRES:
16
  raise RuntimeError("PostgreSQL / psycopg2 is not installed or available.")
17
  # Helper to get DB connection (duplicated from telemetry for standalone usage)
 
12
  IS_HF_SPACE = os.environ.get('HF_SPACE', '0') == '1'
13
 
14
  def get_db_connection():
15
+ if IS_HF_SPACE:
16
+ raise RuntimeError("PostgreSQL database is disabled on HuggingFace Spaces.")
17
  if not HAS_POSTGRES:
18
  raise RuntimeError("PostgreSQL / psycopg2 is not installed or available.")
19
  # Helper to get DB connection (duplicated from telemetry for standalone usage)
src/routes/main.py CHANGED
@@ -161,16 +161,19 @@ def health_check():
161
 
162
  # Database status
163
  db_status = 'unknown'
164
- try:
165
- from src.utils.telemetry import get_db_connection
166
- conn = get_db_connection()
167
- cur = conn.cursor()
168
- cur.execute('SELECT 1')
169
- cur.close()
170
- conn.close()
171
- db_status = 'connected'
172
- except Exception as e:
173
- db_status = f'error: {str(e)[:80]}'
 
 
 
174
  health['database'] = db_status
175
 
176
  return jsonify(health)
@@ -180,6 +183,9 @@ def health_check():
180
  @main_bp.route('/api/v1/history', methods=['GET'])
181
  def get_history():
182
  """Fetch recent detection history"""
 
 
 
183
  try:
184
  from src.utils.telemetry import get_db_connection
185
  conn = get_db_connection()
 
161
 
162
  # Database status
163
  db_status = 'unknown'
164
+ if os.environ.get('HF_SPACE', '0') == '1':
165
+ db_status = 'disabled'
166
+ else:
167
+ try:
168
+ from src.utils.telemetry import get_db_connection
169
+ conn = get_db_connection()
170
+ cur = conn.cursor()
171
+ cur.execute('SELECT 1')
172
+ cur.close()
173
+ conn.close()
174
+ db_status = 'connected'
175
+ except Exception as e:
176
+ db_status = f'error: {str(e)[:80]}'
177
  health['database'] = db_status
178
 
179
  return jsonify(health)
 
183
  @main_bp.route('/api/v1/history', methods=['GET'])
184
  def get_history():
185
  """Fetch recent detection history"""
186
+ if os.environ.get('HF_SPACE', '0') == '1':
187
+ return jsonify([])
188
+
189
  try:
190
  from src.utils.telemetry import get_db_connection
191
  conn = get_db_connection()
src/utils/telemetry.py CHANGED
@@ -34,6 +34,8 @@ except (ImportError, AttributeError):
34
  }
35
 
36
  def get_db_connection():
 
 
37
  if not HAS_POSTGRES:
38
  raise RuntimeError("PostgreSQL / psycopg2 is not installed or available.")
39
  return psycopg2.connect(**DB_PARAMS)
 
34
  }
35
 
36
  def get_db_connection():
37
+ if os.environ.get('HF_SPACE', '0') == '1':
38
+ raise RuntimeError("PostgreSQL database is disabled on HuggingFace Spaces.")
39
  if not HAS_POSTGRES:
40
  raise RuntimeError("PostgreSQL / psycopg2 is not installed or available.")
41
  return psycopg2.connect(**DB_PARAMS)
src/verification/face_matcher.py CHANGED
@@ -489,6 +489,8 @@ class FaceMatcher:
489
 
490
  def _log_metrics(self, image1_path: str, image2_path: str, similarity: float, result: str, request_id: str):
491
  """Log face matching metrics to TimescaleDB if available"""
 
 
492
  try:
493
  import psycopg2
494
 
 
489
 
490
  def _log_metrics(self, image1_path: str, image2_path: str, similarity: float, result: str, request_id: str):
491
  """Log face matching metrics to TimescaleDB if available"""
492
+ if os.environ.get('HF_SPACE', '0') == '1':
493
+ return
494
  try:
495
  import psycopg2
496