__init__.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. """
  2. Machine Learning module for Trixy voice assistant.
  3. This module provides comprehensive ML components for client-side AI processing
  4. including wakeword detection, voice recognition, audio pipeline management,
  5. workflow orchestration, performance monitoring, and application integration.
  6. Key Features:
  7. - Unified ML coordination for client-side deployment
  8. - Complete wakeword -> voice recognition workflow
  9. - Real-time audio processing pipeline
  10. - Performance monitoring and optimization
  11. - Application container integration
  12. - Event system integration
  13. - Comprehensive error handling
  14. Usage:
  15. from trixy_core.ml import MLManager, MLConfig, create_client_ml_system
  16. # Create complete ML system for client
  17. ml_system = create_client_ml_system(
  18. application_container=app_container,
  19. enable_wakeword=True,
  20. enable_voice_recognition=True
  21. )
  22. # Start ML processing
  23. ml_system.start()
  24. """
  25. __version__ = "1.0.0"
  26. __author__ = "Trixy Team"
  27. # Import core ML components
  28. from .wakeword import WakewordDetector, WakewordConfig
  29. from .voice_recognition import VoiceRecognitionDetector, VoiceRecognitionConfig
  30. # Import unified ML system
  31. from .ml_manager import (
  32. MLManager, MLConfig, MLSystemMode as MLMode, MLProcessingState as MLState,
  33. create_ml_manager, create_default_ml_config, create_optimized_ml_config,
  34. create_debug_ml_config
  35. )
  36. # Import audio pipeline
  37. from .audio_pipeline import (
  38. AudioPipeline, AudioPipelineConfig, AudioFormat, AudioPipelineState as AudioState,
  39. create_audio_pipeline, create_default_audio_config
  40. )
  41. # Import new comprehensive modules
  42. # from .config import (
  43. # MLConfiguration, ConfigProfile, MLManagerConfig,
  44. # create_default_ml_configuration, create_production_ml_configuration,
  45. # create_debug_ml_configuration, load_ml_configuration
  46. # )
  47. from .event_integration import (
  48. MLEventManager, EventType as MLEventType, EventPriority,
  49. ConversationContext as MLEventData,
  50. create_ml_event_manager
  51. )
  52. from .performance_monitor import (
  53. PerformanceMonitor as MLPerformanceMonitor, MetricType, AlertLevel,
  54. PerformanceReport as PerformanceConfig, PerformanceThreshold as PerformanceLevel
  55. )
  56. from .container_integration import (
  57. MLContainerManager as MLContainerIntegration
  58. )
  59. # from .integration import (
  60. # MLIntegrationConfig,
  61. # create_ml_integration
  62. # )
  63. # Temporary minimal implementations for basic functionality
  64. class MLIntegrationConfig:
  65. def __init__(self, **kwargs):
  66. self.enable_ml_manager = kwargs.get('enable_ml_manager', True)
  67. self.enable_audio_pipeline = kwargs.get('enable_audio_pipeline', True)
  68. self.enable_workflow_manager = kwargs.get('enable_workflow_manager', True)
  69. self.enable_event_integration = kwargs.get('enable_event_integration', True)
  70. self.enable_performance_monitoring = kwargs.get('enable_performance_monitoring', True)
  71. self.auto_start_components = kwargs.get('auto_start_components', True)
  72. def create_ml_integration(application_container, integration_config):
  73. """Temporary minimal ML integration."""
  74. # Get the config from the application container
  75. config_manager = application_container.get_config_manager()
  76. app_config = config_manager.get_config()
  77. event_handler = application_container.get_event_handler()
  78. # Convert config.ml (from config_models) to MLConfig (from ml_manager)
  79. if hasattr(app_config, 'ml'):
  80. ml_section = app_config.ml
  81. ml_config = MLConfig(
  82. wakeword_model_path=ml_section.wakeword_model_path,
  83. wakeword_model_password=getattr(ml_section, 'wakeword_model_password', ''),
  84. voice_recognition_model_path=ml_section.voice_recognition_model_path,
  85. voice_recognition_model_password=getattr(ml_section, 'voice_recognition_model_password', ''),
  86. wakeword_confidence_threshold=ml_section.wakeword_threshold,
  87. voice_recognition_confidence_threshold=ml_section.voice_recognition_threshold,
  88. enable_optimization=ml_section.enable_gpu, # Map enable_gpu to enable_optimization
  89. audio_sample_rate=getattr(app_config, 'audio', None).sample_rate if hasattr(app_config, 'audio') else 16000,
  90. audio_channels=getattr(app_config, 'audio', None).channels if hasattr(app_config, 'audio') else 1
  91. )
  92. else:
  93. ml_config = create_default_ml_config()
  94. # Return the ML manager with the actual config
  95. ml_manager = create_ml_manager(ml_config, event_handler, application_container)
  96. print(f"DEBUG: create_ml_integration returning type: {type(ml_manager)}, has start: {hasattr(ml_manager, 'start')}")
  97. return ml_manager
  98. from .error_handling import (
  99. MLErrorHandler, ErrorSeverity, RecoveryStrategy as ErrorHandlingConfig,
  100. create_ml_error_handler as create_error_handler
  101. )
  102. from .conversation_workflow import (
  103. ConversationWorkflow as MLWorkflowManager,
  104. WorkflowConfig, ConversationState as WorkflowState,
  105. create_conversation_workflow as create_workflow_manager,
  106. create_default_workflow_config
  107. )
  108. __all__ = [
  109. # Legacy compatibility
  110. "WakewordDetector",
  111. "WakewordConfig",
  112. "VoiceRecognitionDetector",
  113. "VoiceRecognitionConfig",
  114. # Core ML Management
  115. "MLManager",
  116. "MLConfig",
  117. "MLMode",
  118. "MLState",
  119. "create_ml_manager",
  120. "create_default_ml_config",
  121. # Audio Pipeline
  122. "AudioPipeline",
  123. "AudioPipelineConfig",
  124. "AudioFormat",
  125. "AudioState",
  126. "create_audio_pipeline",
  127. "create_default_audio_config",
  128. # Workflow Management
  129. "MLWorkflowManager",
  130. "WorkflowConfig",
  131. "WorkflowState",
  132. "create_workflow_manager",
  133. "create_default_workflow_config",
  134. # Event Integration
  135. "MLEventManager",
  136. "MLEventType",
  137. "MLEventData",
  138. "create_ml_event_manager",
  139. # Performance Monitoring
  140. "MLPerformanceMonitor",
  141. "PerformanceConfig",
  142. "PerformanceLevel",
  143. "create_performance_monitor",
  144. # Configuration Management
  145. "MLConfigManager",
  146. "MLSystemConfig",
  147. "PerformanceSettings",
  148. "create_ml_config",
  149. "create_performance_profile",
  150. # Application Integration
  151. "MLContainerIntegration",
  152. "MLIntegrationConfig",
  153. "create_ml_integration",
  154. # Error Handling
  155. "MLErrorHandler",
  156. "ErrorHandlingConfig",
  157. "ErrorSeverity",
  158. "create_error_handler",
  159. # High-level factory functions
  160. "create_client_ml_system",
  161. "create_standalone_ml_system",
  162. ]
  163. def create_client_ml_system(
  164. application_container,
  165. enable_wakeword: bool = True,
  166. enable_voice_recognition: bool = True,
  167. enable_performance_monitoring: bool = True,
  168. **kwargs
  169. ) -> MLContainerIntegration:
  170. """
  171. Create a complete ML system for client deployment.
  172. This is the main factory function for setting up ML processing
  173. in client mode with all necessary components.
  174. Args:
  175. application_container: Trixy application container
  176. enable_wakeword: Enable wakeword detection
  177. enable_voice_recognition: Enable voice recognition
  178. enable_performance_monitoring: Enable performance monitoring
  179. **kwargs: Additional configuration options
  180. Returns:
  181. Configured ML container integration
  182. """
  183. # Create integration configuration
  184. integration_config = MLIntegrationConfig(
  185. enable_ml_manager=True,
  186. enable_audio_pipeline=True,
  187. enable_workflow_manager=True,
  188. enable_event_integration=True,
  189. enable_performance_monitoring=enable_performance_monitoring,
  190. auto_start_components=True,
  191. **kwargs
  192. )
  193. # Create ML integration
  194. ml_integration = create_ml_integration(
  195. application_container=application_container,
  196. integration_config=integration_config
  197. )
  198. return ml_integration
  199. def create_standalone_ml_system(
  200. application_container,
  201. enable_wakeword: bool = True,
  202. enable_voice_recognition: bool = True,
  203. enable_continuous_recognition: bool = True,
  204. enable_performance_monitoring: bool = True,
  205. **kwargs
  206. ) -> MLContainerIntegration:
  207. """
  208. Create a complete ML system for standalone deployment.
  209. Args:
  210. application_container: Trixy application container
  211. enable_wakeword: Enable wakeword detection
  212. enable_voice_recognition: Enable voice recognition
  213. enable_continuous_recognition: Enable continuous recognition
  214. enable_performance_monitoring: Enable performance monitoring
  215. **kwargs: Additional configuration options
  216. Returns:
  217. Configured ML container integration
  218. """
  219. # Create integration configuration for standalone mode
  220. integration_config = MLIntegrationConfig(
  221. enable_ml_manager=True,
  222. enable_audio_pipeline=True,
  223. enable_workflow_manager=True,
  224. enable_event_integration=True,
  225. enable_performance_monitoring=enable_performance_monitoring,
  226. auto_start_components=True,
  227. **kwargs
  228. )
  229. # Create ML integration
  230. ml_integration = create_ml_integration(
  231. application_container=application_container,
  232. integration_config=integration_config
  233. )
  234. return ml_integration