File size: 3,831 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
Modal deployer for deploying transcription services
"""

import subprocess
from typing import Optional

from ..utils.config import AudioProcessingConfig
from ..utils.errors import DeploymentError
from .endpoint_manager import EndpointManager


class ModalDeployer:
    """Deployer for Modal transcription services"""
    
    def __init__(self, config: Optional[AudioProcessingConfig] = None):
        self.config = config or AudioProcessingConfig()
        self.endpoint_manager = EndpointManager()
    
    def deploy_transcription_service(self) -> Optional[str]:
        """Deploy transcription service to Modal"""
        
        print("πŸš€ Deploying transcription service to Modal...")
        
        try:
            # Deploy the Modal app
            print("πŸš€ Running modal deploy command...")
            result = subprocess.run(
                ["modal", "deploy", "modal_config.py"],
                capture_output=True,
                text=True
            )
            
            if result.returncode == 0:
                # Extract or construct endpoint URL
                endpoint_url = self._extract_endpoint_url(result.stdout)
                
                if endpoint_url:
                    # Save endpoint configuration
                    self.endpoint_manager.set_endpoint("transcribe_audio", endpoint_url)
                    print(f"βœ… Transcription service deployed: {endpoint_url}")
                    return endpoint_url
                else:
                    print("⚠️ Could not extract endpoint URL from deployment output")
                    return None
            else:
                raise DeploymentError(
                    f"Modal deployment failed: {result.stderr}",
                    service="transcription"
                )
                
        except FileNotFoundError:
            raise DeploymentError(
                "Modal CLI not found. Please install Modal: pip install modal",
                service="transcription"
            )
        except Exception as e:
            raise DeploymentError(
                f"Failed to deploy transcription service: {str(e)}",
                service="transcription"
            )
    
    def _extract_endpoint_url(self, output: str) -> Optional[str]:
        """Extract endpoint URL from deployment output"""
        
        # Look for URL in output
        for line in output.split('\n'):
            if 'https://' in line and 'modal.run' in line:
                # Extract URL from line
                parts = line.split()
                for part in parts:
                    if part.startswith('https://') and 'modal.run' in part:
                        return part
        
        # Fallback to constructed URL
        return f"https://{self.config.modal_app_name}--transcribe-audio-endpoint.modal.run"
    
    def check_deployment_status(self) -> bool:
        """Check if transcription service is deployed and healthy"""
        
        endpoint_url = self.endpoint_manager.get_endpoint("transcribe_audio")
        if not endpoint_url:
            print("❌ No transcription endpoint configured")
            return False
        
        if self.endpoint_manager.check_endpoint_health("transcribe_audio"):
            print(f"βœ… Transcription service is healthy: {endpoint_url}")
            return True
        else:
            print(f"❌ Transcription service is not responding: {endpoint_url}")
            return False
    
    def undeploy_transcription_service(self):
        """Remove transcription service endpoint"""
        self.endpoint_manager.remove_endpoint("transcribe_audio")
        print("πŸ—‘οΈ Transcription service endpoint removed from configuration")
        print("πŸ’‘ Note: The actual Modal deployment may still be active. Use 'modal app stop' to stop it.")