Spaces:
Sleeping
Sleeping
File size: 5,339 Bytes
95db528 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
"""
Configuration settings for the validation module.
This module provides centralized configuration for the validation service,
including model paths, processing parameters, and validation thresholds.
"""
import os
from typing import Optional
class ValidationConfig:
"""
Configuration settings for the validation service.
This class provides centralized configuration management for all
validation-related settings, with sensible defaults and environment
variable overrides.
"""
def __init__(self):
"""Initialize configuration with default values and environment overrides."""
# Model paths
self.hand_detector_path = os.getenv(
"HAND_DETECTOR_PATH",
"models/hand_detector.onnx"
)
self.gesture_classifier_path = os.getenv(
"GESTURE_CLASSIFIER_PATH",
"models/crops_classifier.onnx"
)
# Processing parameters
self.frame_skip = int(os.getenv("FRAME_SKIP", "1"))
self.min_gesture_duration = int(os.getenv("MIN_GESTURE_DURATION", "5"))
self.confidence_threshold = float(os.getenv("CONFIDENCE_THRESHOLD", "0.7"))
# Validation parameters
self.default_error_margin = float(os.getenv("DEFAULT_ERROR_MARGIN", "0.33"))
self.require_all_gestures = os.getenv("REQUIRE_ALL_GESTURES", "true").lower() == "true"
self.confidence_threshold = float(os.getenv("CONFIDENCE_THRESHOLD", "0.7"))
self.min_gesture_duration = int(os.getenv("MIN_GESTURE_DURATION", "5"))
# Facial validation parameters
self.similarity_threshold = float(os.getenv("SIMILARITY_THRESHOLD", "0.7"))
self.frame_sample_rate = int(os.getenv("FRAME_SAMPLE_RATE", "10"))
# File size limits (in bytes)
self.max_photo_size = int(os.getenv("MAX_PHOTO_SIZE", str(50 * 1024 * 1024))) # 50MB
self.max_video_size = int(os.getenv("MAX_VIDEO_SIZE", str(200 * 1024 * 1024))) # 200MB
# Performance settings
self.max_processing_time = int(os.getenv("MAX_PROCESSING_TIME", "60")) # seconds
self.enable_detailed_logging = os.getenv("ENABLE_DETAILED_LOGGING", "false").lower() == "true"
# Security settings
self.allowed_image_types = os.getenv(
"ALLOWED_IMAGE_TYPES",
"image/jpeg,image/png,image/webp,application/pdf"
).split(",")
self.allowed_video_types = os.getenv(
"ALLOWED_VIDEO_TYPES",
"video/mp4,video/avi,video/mov,video/webm"
).split(",")
@property
def model_paths(self) -> dict:
"""Get model paths as a dictionary."""
return {
"hand_detector": self.hand_detector_path,
"gesture_classifier": self.gesture_classifier_path
}
@property
def processing_params(self) -> dict:
"""Get processing parameters as a dictionary."""
return {
"frame_skip": self.frame_skip,
"min_gesture_duration": self.min_gesture_duration,
"confidence_threshold": self.confidence_threshold
}
@property
def validation_params(self) -> dict:
"""Get validation parameters as a dictionary."""
return {
"default_error_margin": self.default_error_margin,
"require_all_gestures": self.require_all_gestures
}
def validate_file_type(self, content_type: str, file_type: str = "image") -> bool:
"""
Validate if a file type is allowed.
Parameters
----------
content_type : str
MIME content type of the file
file_type : str, optional
Type of file ("image" or "video"), by default "image"
Returns
-------
bool
True if file type is allowed, False otherwise
"""
if file_type == "image":
allowed_types = self.allowed_image_types
elif file_type == "video":
allowed_types = self.allowed_video_types
else:
return False
return content_type in allowed_types
def validate_file_size(self, file_size: int, file_type: str = "image") -> bool:
"""
Validate if a file size is within limits.
Parameters
----------
file_size : int
Size of the file in bytes
file_type : str, optional
Type of file ("image" or "video"), by default "image"
Returns
-------
bool
True if file size is within limits, False otherwise
"""
if file_type == "image":
max_size = self.max_photo_size
elif file_type == "video":
max_size = self.max_video_size
else:
return False
return file_size <= max_size
# Global configuration instance
config = ValidationConfig()
def get_config() -> ValidationConfig:
"""
Get the global configuration instance.
Returns
-------
ValidationConfig
Global configuration instance
"""
return config
def reload_config() -> ValidationConfig:
"""
Reload configuration from environment variables.
Returns
-------
ValidationConfig
New configuration instance with updated values
"""
global config
config = ValidationConfig()
return config
|