File size: 3,893 Bytes
991ca47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python3
"""
Setup script for Quran Recitation Transcription API
This script helps with initial setup and validation
"""

import os
import sys
import subprocess
from pathlib import Path

def check_python_version():
    """Check if Python version is 3.8 or higher"""
    version = sys.version_info
    if version.major < 3 or (version.major == 3 and version.minor < 8):
        print(f"❌ Python 3.8+ required. You have Python {version.major}.{version.minor}")
        return False
    print(f"βœ“ Python {version.major}.{version.minor} detected")
    return True

def check_gpu_availability():
    """Check if CUDA-capable GPU is available"""
    try:
        import torch
        if torch.cuda.is_available():
            print(f"βœ“ GPU detected: {torch.cuda.get_device_name(0)}")
            print(f"  CUDA Version: {torch.version.cuda}")
            return True
        else:
            print("⚠ No GPU detected. Will use CPU (slower transcription)")
            return False
    except ImportError:
        print("⚠ PyTorch not installed yet. GPU check skipped.")
        return None

def create_env_file():
    """Create .env file from .env.example if it doesn't exist"""
    env_path = Path(".env")
    env_example = Path(".env.example")
    
    if env_path.exists():
        print("βœ“ .env file already exists")
        return True
    
    if env_example.exists():
        env_path.write_text(env_example.read_text())
        print("βœ“ Created .env file from .env.example")
        return True
    
    print("❌ .env.example not found")
    return False

def install_dependencies():
    """Install required Python dependencies"""
    print("\nπŸ“¦ Installing dependencies...")
    try:
        subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"])
        print("βœ“ Dependencies installed successfully")
        return True
    except subprocess.CalledProcessError as e:
        print(f"❌ Failed to install dependencies: {e}")
        return False

def verify_imports():
    """Verify that all required packages can be imported"""
    required_packages = [
        "fastapi",
        "uvicorn",
        "faster_whisper",
        "torch",
        "pydantic"
    ]
    
    print("\nπŸ” Verifying imports...")
    all_ok = True
    for package in required_packages:
        try:
            __import__(package)
            print(f"βœ“ {package}")
        except ImportError:
            print(f"❌ {package} - not found")
            all_ok = False
    
    return all_ok

def main():
    """Run setup checks"""
    print("πŸš€ Quran Recitation Transcription API - Setup\n")
    print("=" * 50)
    
    # Check Python version
    if not check_python_version():
        sys.exit(1)
    
    # Check GPU availability
    gpu_available = check_gpu_availability()
    
    # Create .env file
    print("\nπŸ“ Configuration:")
    if not create_env_file():
        print("⚠ Please create .env file manually from .env.example")
    
    # Install dependencies
    print("\nπŸ“₯ Dependencies:")
    if not install_dependencies():
        sys.exit(1)
    
    # Verify imports
    print()
    if not verify_imports():
        print("\n❌ Some packages failed to import. Please check the error messages above.")
        sys.exit(1)
    
    # Summary
    print("\n" + "=" * 50)
    print("βœ… Setup completed successfully!")
    print("\nπŸ“‹ Next steps:")
    print("  1. (Optional) Edit .env file to customize settings")
    print("  2. Run the server:")
    print("     uvicorn main:app --reload")
    print("  3. Open http://localhost:8000/docs for API documentation")
    
    if gpu_available is False:
        print("\n⚠️  Note: Using CPU mode. For GPU acceleration:")
        print("   - Ensure CUDA is installed")
        print("   - Update .env: CUDA_VISIBLE_DEVICES=0")

if __name__ == "__main__":
    main()