Spaces:
Sleeping
Sleeping
Disable PostgreSQL database connections on Hugging Face Spaces
Browse files- src/liveness/liveness_detector.py +2 -0
- src/middleware/api_auth.py +2 -0
- src/routes/main.py +16 -10
- src/utils/telemetry.py +2 -0
- src/verification/face_matcher.py +2 -0
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 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
|