Refactor: ASR Engine Abstraction and Service Layer Cleanup
🚀 Refactor: Engine Logic Abstraction
📌 Overview
This MR refactors the ASR engine architecture to improve modularity, remove circular dependencies, and enforce a clean separation of concerns. The changes align the system with scalable design principles, making it easier to maintain and extend with new ASR engines.
❗ Problem
The previous architecture had several issues:
-
Circular Dependency
-
SwechaGonthukaEnginedepended onasr_service.py, whileasr_service.pyalso depended on engines.
-
-
God Object
-
asr_service.pyhandled multiple responsibilities:- Transcription logic
- Audio conversion
- Punctuation
- Subtitle generation
-
-
Inconsistent Engine Design
- Swecha engine was not self-contained
- Whisper engine followed a cleaner structure
✅ Solution
This MR introduces a structured refactor:
-
Engine Self-Containment
- Moved all Swecha-specific logic into
SwechaGonthukaEngine - Removed dependency on
asr_service.py
- Moved all Swecha-specific logic into
-
Abstract Engine Interface
-
Introduced
AsrEnginebase class (app/asr/types.py) -
Standardized engine contract:
transcribetranscribe_pcm16namesupported_languages
-
-
Service Layer Cleanup
- Removed engine-specific logic from
asr_service.py - Retained only cross-cutting responsibilities
- Removed engine-specific logic from
-
Router Simplification
- Router now directly calls engine methods
- Removed dynamic mutation of engine configuration
-
Whisper Engine Refactor
- Supports constructor-based configuration
- Improved consistency with Swecha engine
-
Service Extraction
-
Introduced:
-
JobManager→ handles async job lifecycle -
StreamingService→ handles WebSocket streaming
-
-
Reduced responsibility of
main.py
-
🔧 Key Changes
Modified Files
app/asr/engines/swecha_gonthuka.pyapp/asr/engines/whisper.pyapp/asr/router.pyapp/asr/types.pyapp/asr_service.pyapp/main.py
New Files
app/services/job_manager.pyapp/services/streaming_service.py
🧪 Testing
-
Existing test suite reviewed and executed
-
Verified:
- Engine routing behavior
- Batch transcription flow (
/transcribe) - Streaming functionality (
/stream)
-
No regression in API responses
🎯 Impact
- Eliminates circular dependencies
- Enforces Single Responsibility Principle
- Enables easy addition of new ASR engines
- Improves code readability and maintainability
- Aligns architecture with scalable backend design
🚫 Out of Scope
- No changes to ASR models
- No changes to API contracts
- No changes to deployment setup
🧠 Summary
This refactor transforms the ASR system into a clean, modular, and extensible architecture by introducing a unified engine interface and decoupling responsibilities across layers.
*Closes #17 (closed)
