nsakib161's picture
Fresh start: Configure for HF Spaces
991ca47
#!/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()