| |
| """ |
| 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() |
|
|