File size: 2,871 Bytes
b5df735
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Factory for creating transcription adapters
"""

import os
from typing import Optional

from ..interfaces.transcriber import ITranscriber
from ..utils.config import AudioProcessingConfig
from ..utils.errors import ConfigurationError
from .local_adapter import LocalTranscriptionAdapter
from .modal_adapter import ModalTranscriptionAdapter


class TranscriptionAdapterFactory:
    """Factory for creating appropriate transcription adapters"""
    
    @staticmethod
    def create_adapter(
        deployment_mode: str = "auto",
        config: Optional[AudioProcessingConfig] = None,
        endpoint_url: Optional[str] = None
    ) -> ITranscriber:
        """
        Create transcription adapter based on deployment mode
        
        Args:
            deployment_mode: "local", "modal", or "auto"
            config: Configuration object
            endpoint_url: Modal endpoint URL (for modal/auto mode)
            
        Returns:
            ITranscriber: Appropriate transcription adapter
        """
        
        config = config or AudioProcessingConfig()
        
        # Auto mode: decide based on environment and endpoint availability
        if deployment_mode == "auto":
            if endpoint_url:
                print(f"🌐 Auto mode: Using Modal adapter with endpoint {endpoint_url}")
                return ModalTranscriptionAdapter(config=config, endpoint_url=endpoint_url)
            else:
                print(f"🏠 Auto mode: Using Local adapter (no endpoint configured)")
                return LocalTranscriptionAdapter(config=config)
        
        # Explicit local mode
        elif deployment_mode == "local":
            print(f"🏠 Using Local transcription adapter")
            return LocalTranscriptionAdapter(config=config)
        
        # Explicit modal mode  
        elif deployment_mode == "modal":
            if not endpoint_url:
                raise ConfigurationError(
                    "Modal endpoint URL is required for modal mode",
                    config_key="endpoint_url"
                )
            print(f"🌐 Using Modal transcription adapter with endpoint {endpoint_url}")
            return ModalTranscriptionAdapter(config=config, endpoint_url=endpoint_url)
        
        else:
            raise ConfigurationError(
                f"Unsupported deployment mode: {deployment_mode}. Use 'local', 'modal', or 'auto'",
                config_key="deployment_mode"
            )
    
    @staticmethod
    def _detect_deployment_mode() -> str:
        """Auto-detect deployment mode based on environment"""
        import os
        
        # Check if running in Modal environment
        if os.environ.get("MODAL_TASK_ID"):
            return "local"  # We're inside Modal, use local processing
        else:
            return "modal"  # We're outside Modal, use remote endpoint