| |
| """ |
| Quick Start Script for AI Service |
| Validates environment and starts the service |
| """ |
|
|
| import os |
| import sys |
| import subprocess |
|
|
|
|
| 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 |
| |
| |
| try: |
| import torch |
|
|
| def parse_version(version_str: str) -> tuple: |
| version_core = version_str.split("+")[0] |
| parts = version_core.split(".") |
| return tuple(int(p) for p in parts[:3]) |
|
|
| if parse_version(torch.__version__) < (2, 6, 0): |
| print("\nβ torch>=2.6.0 required for secure model loading") |
| print(f" Current: {torch.__version__}") |
| return False |
| except Exception as exc: |
| print(f"\nβ Could not validate torch version: {exc}") |
|
|
| 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__)) |
|
|
| try: |
| subprocess.run([ |
| sys.executable, "-m", "uvicorn", |
| "main:app", |
| "--reload", |
| "--host", "0.0.0.0", |
| "--port", "8000", |
| "--app-dir", app_dir, |
| ]) |
| except KeyboardInterrupt: |
| print("\nβ Service stopped") |
|
|
|
|
| def main(): |
| 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() |
|
|