File size: 2,816 Bytes
92a5c40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
"""
Quick Start Script for AI Service
Validates environment and starts the service
"""

import os
import sys
import subprocess


def configure_console_encoding():
    """Use UTF-8 stdout/stderr when available, especially on Windows."""
    for stream_name in ("stdout", "stderr"):
        stream = getattr(sys, stream_name, None)
        if hasattr(stream, "reconfigure"):
            try:
                stream.reconfigure(encoding="utf-8")
            except Exception:
                pass


def check_python_version():
    """Ensure Python 3.9+"""
    if sys.version_info < (3, 9):
        print("❌ Python 3.9+ required")
        print(f"   Current: {sys.version}")
        return False
    print(f"βœ“ Python {sys.version_info.major}.{sys.version_info.minor}")
    return True


def check_dependencies():
    """Check if required packages are installed"""
    required = [
        "fastapi",
        "uvicorn",
        "torch",
        "transformers",
        "librosa",
        "soundfile"
    ]
    
    missing = []
    for package in required:
        try:
            __import__(package)
            print(f"βœ“ {package}")
        except ImportError:
            print(f"βœ— {package} (missing)")
            missing.append(package)
    
    if missing:
        print("\n❌ Missing dependencies. Install with:")
        print("   pip install -r requirements.txt")
        return False
    
    return True


def check_cuda():
    """Check CUDA availability"""
    try:
        import torch
        if torch.cuda.is_available():
            print(f"βœ“ CUDA available: {torch.cuda.get_device_name(0)}")
        else:
            print("⚠ CUDA not available (using CPU - slower)")
    except Exception as e:
        print(f"⚠ Could not check CUDA: {e}")


def start_service():
    """Start uvicorn server"""
    print("\nπŸš€ Starting AI Service...")
    print("   Press Ctrl+C to stop\n")

    app_dir = os.path.dirname(os.path.abspath(__file__))
    reload_enabled = os.getenv("AI_SERVICE_RELOAD", "").lower() == "true"

    command = [
        sys.executable, "-m", "uvicorn",
        "main:app",
        "--host", "0.0.0.0",
        "--port", "8000",
        "--app-dir", app_dir,
    ]
    if reload_enabled:
        command.append("--reload")

    try:
        subprocess.run(command)
    except KeyboardInterrupt:
        print("\nβœ“ Service stopped")


def main():
    configure_console_encoding()

    print("=" * 50)
    print("πŸ§ͺ AI Service Pre-flight Check")
    print("=" * 50 + "\n")
    
    if not check_python_version():
        sys.exit(1)
    
    if not check_dependencies():
        sys.exit(1)
    
    check_cuda()
    
    print("\nβœ“ All checks passed!")
    print("=" * 50 + "\n")
    
    start_service()


if __name__ == "__main__":
    main()