#!/bin/bash # Whisper API Deployment Script for Hugging Face Spaces # Usage: ./deploy.sh set -e echo "🚀 Whisper API Deployment Script" echo "================================" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration SPACE_NAME="whisper-api-service" SPACE_SDK="docker" DEFAULT_HF_USERNAME="" # Functions print_step() { echo -e "\n${BLUE}▶ $1${NC}" } print_success() { echo -e "${GREEN}✅ $1${NC}" } print_warning() { echo -e "${YELLOW}⚠️ $1${NC}" } print_error() { echo -e "${RED}❌ $1${NC}" } # Check if required files exist check_files() { print_step "Checking required files..." required_files=( "app.py" "requirements.txt" "Dockerfile" "README.md" ) for file in "${required_files[@]}"; do if [[ -f "$file" ]]; then print_success "Found $file" else print_error "Missing required file: $file" exit 1 fi done # Check optional files optional_files=( "test_api.py" ".env.example" ".gitignore" ) for file in "${optional_files[@]}"; do if [[ -f "$file" ]]; then print_success "Found optional file: $file" else print_warning "Optional file not found: $file" fi done } # Create .gitignore if not exists create_gitignore() { if [[ ! -f ".gitignore" ]]; then print_step "Creating .gitignore file..." cat > .gitignore << 'EOF' # Python __pycache__/ *.pyc *.pyo *.pyd .Python *.so .pytest_cache/ *.egg-info/ # Virtual Environment venv/ env/ .venv/ # IDE .vscode/ .idea/ *.swp *.swo # OS .DS_Store Thumbs.db # Logs *.log logs/ # Environment .env .env.local # Cache .cache/ *.cache # Temporary files temp/ tmp/ *.tmp # Model cache models/ cache/ # Test files test_audio.* test_video.* EOF print_success "Created .gitignore" else print_success ".gitignore already exists" fi } # Validate Dockerfile validate_dockerfile() { print_step "Validating Dockerfile..." if grep -q "FROM python:" Dockerfile; then print_success "Base image is Python-based" else print_error "Dockerfile should use Python base image" exit 1 fi if grep -q "EXPOSE 7860" Dockerfile; then print_success "Port 7860 is exposed" else print_error "Dockerfile should expose port 7860" exit 1 fi if grep -q "ffmpeg" Dockerfile; then print_success "FFmpeg is installed" else print_warning "FFmpeg may not be installed" fi } # Test requirements test_requirements() { print_step "Testing requirements.txt..." required_packages=( "torch" "transformers" "flask" "yt-dlp" "ffmpeg-python" ) for package in "${required_packages[@]}"; do if grep -q "^${package}" requirements.txt; then print_success "Required package found: $package" else print_error "Missing required package: $package" exit 1 fi done } # Create Space configuration create_space_config() { print_step "Creating Space configuration..." # Create README header for Space if ! grep -q "title:" README.md; then print_step "Adding Space metadata to README.md..." # Backup original README cp README.md README.md.backup # Create new README with metadata cat > README.md << 'EOF' --- title: Whisper API Service emoji: 🎤 colorFrom: blue colorTo: purple sdk: docker app_port: 7860 pinned: false license: mit duplicated_from: models: - openai/whisper-large-v3 datasets: [] tags: - audio - speech-to-text - transcription - translation - whisper - api - flask --- EOF # Append original README content cat README.md.backup >> README.md rm README.md.backup print_success "Added Space metadata to README.md" else print_success "Space metadata already exists in README.md" fi } # Git operations setup_git() { print_step "Setting up Git repository..." if [[ ! -d ".git" ]]; then git init print_success "Initialized Git repository" else print_success "Git repository already exists" fi # Add all files git add . # Check if there are changes to commit if git diff --cached --quiet; then print_warning "No changes to commit" else git commit -m "Initial commit: Whisper API Service - Complete Flask API for speech-to-text and translation - Support for 99 languages - Multiple input types: file upload, YouTube, direct URLs - Docker deployment ready - WordPress plugin integration ready - Extension hooks for future plugins" print_success "Created initial commit" fi } # Hugging Face deployment deploy_to_hf() { print_step "Deploying to Hugging Face Spaces..." # Check if git-lfs is installed if ! command -v git-lfs &> /dev/null; then print_error "Git LFS is required but not installed" print_step "Install Git LFS:" echo " Ubuntu/Debian: sudo apt install git-lfs" echo " macOS: brew install git-lfs" echo " Windows: Download from https://git-lfs.github.io/" exit 1 fi # Initialize git-lfs git lfs install # Check if HF CLI is installed if ! command -v huggingface-cli &> /dev/null; then print_error "Hugging Face CLI is required but not installed" print_step "Install HF CLI: pip install huggingface_hub" exit 1 fi # Get username if [[ -z "$DEFAULT_HF_USERNAME" ]]; then read -p "Enter your Hugging Face username: " HF_USERNAME else HF_USERNAME=$DEFAULT_HF_USERNAME fi if [[ -z "$HF_USERNAME" ]]; then print_error "Hugging Face username is required" exit 1 fi REPO_URL="https://huggingface.co/spaces/${HF_USERNAME}/${SPACE_NAME}" print_step "Creating Space: ${HF_USERNAME}/${SPACE_NAME}" # Create the space (this might fail if it already exists, which is fine) huggingface-cli repo create "${HF_USERNAME}/${SPACE_NAME}" --type space --space_sdk docker 2>/dev/null || true # Add remote if not exists if ! git remote get-url origin &> /dev/null; then git remote add origin "${REPO_URL}" print_success "Added remote origin" else print_success "Remote origin already exists" fi # Push to Hugging Face print_step "Pushing to Hugging Face Spaces..." git push origin main -f print_success "Successfully deployed to Hugging Face Spaces!" echo -e "\n${GREEN}🎉 Your Space is available at:${NC}" echo -e "${BLUE}${REPO_URL}${NC}" print_step "Space will be building... This may take several minutes." print_step "Monitor the build process in your Space's logs." } # Local testing test_local() { print_step "Testing local deployment..." if command -v docker &> /dev/null; then print_step "Building Docker image..." docker build -t whisper-api-test . print_step "Starting container for testing..." docker run -d -p 7860:7860 --name whisper-api-test whisper-api-test print_step "Waiting for service to start..." sleep 10 # Test health endpoint if curl -f http://localhost:7860/health &> /dev/null; then print_success "Local deployment test successful!" print_step "API is running at: http://localhost:7860" else print_error "Local deployment test failed" fi # Cleanup docker stop whisper-api-test &> /dev/null || true docker rm whisper-api-test &> /dev/null || true else print_warning "Docker not available for local testing" print_step "You can test manually with: python app.py" fi } # Create deployment info create_deployment_info() { print_step "Creating deployment information..." cat > DEPLOYMENT.md << EOF # Deployment Information ## Hugging Face Space - **URL**: https://huggingface.co/spaces/${HF_USERNAME}/${SPACE_NAME} - **SDK**: Docker - **Port**: 7860 - **Model**: openai/whisper-large-v3 ## API Endpoints - **Health Check**: \`GET /health\` - **Languages**: \`GET /languages\` - **Transcribe**: \`POST /transcribe\` - **Batch Transcribe**: \`POST /batch_transcribe\` - **Initialize**: \`POST /initialize\` ## Testing \`\`\`bash # Test the API python test_api.py # Or use curl curl https://your-username-${SPACE_NAME}.hf.space/health \`\`\` ## WordPress Integration \`\`\`php \$api_url = 'https://your-username-${SPACE_NAME}.hf.space'; // Use this URL in your WordPress plugin \`\`\` ## Next Steps 1. Wait for Space to build (5-10 minutes) 2. Test all endpoints 3. Integrate with WordPress plugin 4. Add custom extensions if needed ## Monitoring - Check Space logs for errors - Monitor API performance - Set up alerts for downtime Deployed on: $(date) EOF print_success "Created DEPLOYMENT.md with deployment information" } # Main deployment flow main() { print_step "Starting deployment process..." # Pre-deployment checks check_files create_gitignore validate_dockerfile test_requirements # Local testing (optional) read -p "Do you want to test locally first? (y/n): " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then test_local fi # Prepare for deployment create_space_config setup_git # Deploy read -p "Deploy to Hugging Face Spaces? (y/n): " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then deploy_to_hf create_deployment_info else print_warning "Deployment cancelled by user" exit 0 fi print_success "Deployment process completed!" echo -e "\n${GREEN}🎉 Next Steps:${NC}" echo "1. Wait for your Space to build (5-10 minutes)" echo "2. Test the API endpoints" echo "3. Integrate with your WordPress plugin" echo "4. Monitor the logs for any issues" echo -e "\n${BLUE}📖 Useful Commands:${NC}" echo "- Test API: python test_api.py" echo "- View logs: Check your Space's logs tab" echo "- Update: git push origin main" } # Run main function main "$@"