initialize method

Future<bool> initialize()

Initialize the toxicity detection model Call this early in your app lifecycle (e.g., in main() or app startup)

Implementation

Future<bool> initialize() async {
  if (_isInitialized) return true;
  if (_isInitializing) {
    // Wait for ongoing initialization
    while (_isInitializing) {
      await Future.delayed(const Duration(milliseconds: 100));
    }
    return _isInitialized;
  }

  _isInitializing = true;
  try {
    await ToxicityGuard.initialize();
    _isInitialized = true;
    if (kDebugMode) {
      print('ToxicityService: Model initialized successfully');
    }
    return true;
  } catch (e) {
    if (kDebugMode) {
      print('ToxicityService: Failed to initialize model: $e');
    }
    _isInitialized = false;
    return false;
  } finally {
    _isInitializing = false;
  }
}