AI Engine Architecture
Clean Architecture Implementation
This AI engine follows clean architecture principles with proper separation of concerns.
Module Structure
diagnosis/ai_engine/
βββ detect_stuttering.py # Main detector class (business logic)
βββ model_loader.py # Singleton pattern for model loading
βββ features.py # Feature extraction (ASR features)
Architecture Pattern
1. Model Loader (model_loader.py)
Responsibility: Singleton pattern for model instance management
- Ensures models are loaded only once
- Provides clean interface:
get_stutter_detector() - Handles initialization and error handling
- Used by API layer (
app.py)
Usage:
from diagnosis.ai_engine.model_loader import get_stutter_detector
detector = get_stutter_detector() # Singleton instance
2. Feature Extractor (features.py)
Responsibility: Feature extraction from audio using IndicWav2Vec Hindi
Class: ASRFeatureExtractor
Methods:
extract_audio_features()- Raw audio feature extractionget_transcription_features()- Transcription with confidence scoresget_word_level_features()- Word-level timestamps and confidence
Design Pattern:
- Takes pre-loaded model and processor as dependencies
- Single responsibility: feature extraction only
- Reusable across different use cases
Usage:
from .features import ASRFeatureExtractor
extractor = ASRFeatureExtractor(model, processor, device)
features = extractor.get_transcription_features(audio)
3. Detector (detect_stuttering.py)
Responsibility: High-level stutter detection orchestration
Class: AdvancedStutterDetector
Design:
- Uses feature extractor for transcription (composition)
- Orchestrates the analysis pipeline
- Returns structured results
Flow:
Audio Input
β
Feature Extractor (ASR)
β
Text Analysis
β
Results
Benefits of This Architecture
β Separation of Concerns
- Model Loading: Isolated in
model_loader.py - Feature Extraction: Isolated in
features.py - Business Logic: In
detect_stuttering.py
β Single Responsibility Principle
- Each module has one clear purpose
- Easy to test and maintain
- Easy to extend or replace components
β Dependency Injection
- Feature extractor receives model/processor as dependencies
- No tight coupling
- Easy to mock for testing
β Reusability
- Feature extractor can be used independently
- Model loader can be used by other modules
- Clean interfaces between layers
Data Flow
API Request (app.py)
β
get_stutter_detector() [model_loader.py]
β
AdvancedStutterDetector [detect_stuttering.py]
β
ASRFeatureExtractor [features.py]
β
IndicWav2Vec Hindi Model
β
Results back through layers
Comparison with Django App
Before (Django App):
- Model loading logic in Django app
- Feature extraction in Django app
- Tight coupling between web app and ML logic
After (AI Engine Service):
- β Model loading in AI engine service
- β Feature extraction in AI engine service
- β Django app only calls API (loose coupling)
- β ML logic isolated in dedicated service
Extension Points
Adding New Features
- Add method to
ASRFeatureExtractorinfeatures.py - Use in
AdvancedStutterDetectorvia composition - No changes needed to model loader
Adding New Models
- Update
detect_stuttering.pyto load new model - Create new feature extractor if needed
- Model loader remains unchanged
Testing
- Mock
ASRFeatureExtractorin tests - Mock model loader for integration tests
- Each component can be tested independently
Key Principles Applied
- Dependency Inversion: High-level modules don't depend on low-level modules
- Open/Closed: Open for extension, closed for modification
- Interface Segregation: Clean, focused interfaces
- Don't Repeat Yourself (DRY): Feature extraction logic centralized
- Single Source of Truth: Model instance managed by singleton
File Responsibilities
| File | Responsibility | Depends On |
|---|---|---|
model_loader.py |
Singleton model management | detect_stuttering.py |
features.py |
Feature extraction | transformers, torch |
detect_stuttering.py |
Business logic orchestration | features.py, model_loader.py |
app.py |
API layer | model_loader.py |
This architecture ensures the ML/AI logic stays in the AI engine service, not in the Django web application, following microservices best practices.