Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -30,11 +30,72 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
|
|
| 30 |
|
| 31 |
# Import configuration and security systems
|
| 32 |
from config.settings import get_config, is_production, is_development
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Import the real API chat system for Hugging Face deployment
|
| 40 |
from conversational_travel_chat_real_apis import RealAPIConversationalTravelChat
|
|
@@ -60,16 +121,23 @@ class ProductionChatApp:
|
|
| 60 |
# Load configuration
|
| 61 |
self.config = get_config()
|
| 62 |
|
| 63 |
-
# Initialize security systems
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
# Initialize real API chat system for Hugging Face
|
| 75 |
self.real_api_chat = RealAPIConversationalTravelChat()
|
|
|
|
| 30 |
|
| 31 |
# Import configuration and security systems
|
| 32 |
from config.settings import get_config, is_production, is_development
|
| 33 |
+
|
| 34 |
+
# Try to import security systems, with fallback for Hugging Face deployment
|
| 35 |
+
try:
|
| 36 |
+
from security.error_handler import (
|
| 37 |
+
InputValidator, RateLimiter, SessionManager, ErrorHandler,
|
| 38 |
+
handle_errors, validate_input, rate_limit, require_session,
|
| 39 |
+
ValidationError, RateLimitError, AuthenticationError
|
| 40 |
+
)
|
| 41 |
+
SECURITY_AVAILABLE = True
|
| 42 |
+
except ImportError as e:
|
| 43 |
+
logger.warning(f"Security modules not available: {e}")
|
| 44 |
+
logger.warning("Running in Hugging Face compatibility mode")
|
| 45 |
+
SECURITY_AVAILABLE = False
|
| 46 |
+
|
| 47 |
+
# Create fallback classes for Hugging Face deployment
|
| 48 |
+
class InputValidator:
|
| 49 |
+
def __init__(self, max_length=1000):
|
| 50 |
+
self.max_length = max_length
|
| 51 |
+
def validate(self, text):
|
| 52 |
+
return text[:self.max_length] if text else ""
|
| 53 |
+
|
| 54 |
+
class RateLimiter:
|
| 55 |
+
def __init__(self, requests_per_minute=60, requests_per_hour=1000):
|
| 56 |
+
pass
|
| 57 |
+
def check_rate_limit(self, user_id):
|
| 58 |
+
return True
|
| 59 |
+
|
| 60 |
+
class SessionManager:
|
| 61 |
+
def __init__(self, session_timeout_minutes=30):
|
| 62 |
+
pass
|
| 63 |
+
def create_session(self, user_id):
|
| 64 |
+
return f"session_{user_id}_{datetime.now().timestamp()}"
|
| 65 |
+
def validate_session(self, session_id):
|
| 66 |
+
return True
|
| 67 |
+
|
| 68 |
+
class ErrorHandler:
|
| 69 |
+
def handle_error(self, error, context=""):
|
| 70 |
+
logger.error(f"Error: {error} - Context: {context}")
|
| 71 |
+
return "An error occurred. Please try again."
|
| 72 |
+
|
| 73 |
+
# Fallback decorators
|
| 74 |
+
def handle_errors(func):
|
| 75 |
+
def wrapper(*args, **kwargs):
|
| 76 |
+
try:
|
| 77 |
+
return func(*args, **kwargs)
|
| 78 |
+
except Exception as e:
|
| 79 |
+
logger.error(f"Error in {func.__name__}: {e}")
|
| 80 |
+
return "An error occurred. Please try again."
|
| 81 |
+
return wrapper
|
| 82 |
+
|
| 83 |
+
def validate_input(func):
|
| 84 |
+
return func
|
| 85 |
+
|
| 86 |
+
def rate_limit(func):
|
| 87 |
+
return func
|
| 88 |
+
|
| 89 |
+
def require_session(func):
|
| 90 |
+
return func
|
| 91 |
+
|
| 92 |
+
# Fallback exceptions
|
| 93 |
+
class ValidationError(Exception):
|
| 94 |
+
pass
|
| 95 |
+
class RateLimitError(Exception):
|
| 96 |
+
pass
|
| 97 |
+
class AuthenticationError(Exception):
|
| 98 |
+
pass
|
| 99 |
|
| 100 |
# Import the real API chat system for Hugging Face deployment
|
| 101 |
from conversational_travel_chat_real_apis import RealAPIConversationalTravelChat
|
|
|
|
| 121 |
# Load configuration
|
| 122 |
self.config = get_config()
|
| 123 |
|
| 124 |
+
# Initialize security systems (with fallback for Hugging Face)
|
| 125 |
+
if SECURITY_AVAILABLE:
|
| 126 |
+
self.input_validator = InputValidator(max_length=self.config.security.max_input_length)
|
| 127 |
+
self.rate_limiter = RateLimiter(
|
| 128 |
+
requests_per_minute=self.config.api.rate_limit_per_minute,
|
| 129 |
+
requests_per_hour=self.config.api.rate_limit_per_hour
|
| 130 |
+
)
|
| 131 |
+
self.session_manager = SessionManager(
|
| 132 |
+
session_timeout_minutes=self.config.security.session_timeout_minutes
|
| 133 |
+
)
|
| 134 |
+
self.error_handler = ErrorHandler()
|
| 135 |
+
else:
|
| 136 |
+
# Use fallback security systems for Hugging Face
|
| 137 |
+
self.input_validator = InputValidator(max_length=1000)
|
| 138 |
+
self.rate_limiter = RateLimiter(requests_per_minute=60, requests_per_hour=1000)
|
| 139 |
+
self.session_manager = SessionManager(session_timeout_minutes=30)
|
| 140 |
+
self.error_handler = ErrorHandler()
|
| 141 |
|
| 142 |
# Initialize real API chat system for Hugging Face
|
| 143 |
self.real_api_chat = RealAPIConversationalTravelChat()
|