Spaces:
Running
Running
| import logging | |
| import threading | |
| from app.utils.config import settings | |
| logger = logging.getLogger(__name__) | |
| # Singleton instance and lock | |
| _analyzer_instance = None | |
| _initialization_lock = threading.Lock() | |
| _is_initializing = False | |
| def get_analyzer_instance(): | |
| """ | |
| Get the global analyzer instance (Lazy loading). | |
| Note: If not ready, this will still trigger a synchronous load if called from a route. | |
| Best used after background_init_analyzer has completed. | |
| """ | |
| global _analyzer_instance | |
| if _analyzer_instance is None: | |
| with _initialization_lock: | |
| # Double-check inside lock | |
| if _analyzer_instance is None: | |
| logger.info("π Initializing ProductionCertificateAnalyzer (blocking load)...") | |
| from app.analyzers.certificate_analyzer import ProductionCertificateAnalyzer | |
| _analyzer_instance = ProductionCertificateAnalyzer(use_ml=settings.use_ml) | |
| logger.info("β Analyzer instance initialized successfully") | |
| return _analyzer_instance | |
| def init_analyzer_background(): | |
| """ | |
| Initializes the analyzer in a background thread to avoid blocking the event loop. | |
| """ | |
| global _analyzer_instance, _is_initializing | |
| if _analyzer_instance is not None or _is_initializing: | |
| return | |
| def _target(): | |
| global _analyzer_instance, _is_initializing | |
| with _initialization_lock: | |
| try: | |
| _is_initializing = True | |
| logger.info("π§΅ Threaded initialization of ProductionCertificateAnalyzer started...") | |
| from app.analyzers.certificate_analyzer import ProductionCertificateAnalyzer | |
| _analyzer_instance = ProductionCertificateAnalyzer(use_ml=settings.use_ml) | |
| logger.info("β Threaded initialization complete") | |
| except Exception as e: | |
| logger.error(f"β Threaded initialization failed: {e}") | |
| finally: | |
| _is_initializing = False | |
| thread = threading.Thread(target=_target, daemon=True) | |
| thread.start() | |