Spaces:
Runtime error
Runtime error
| #!/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() | |