#!/bin/bash # Script to deploy to Hugging Face Space and test an endpoint # --- Configuration --- SPACE_DIR="" # Relative path to your Space directory SPACE_URL="https://marcosremar2-mineru.hf.space" # URL of your Hugging Face Space TEST_ENDPOINT="/voices" # API endpoint to test (e.g., /health, /api/status) COMMIT_MESSAGE="Automated push by deployment script" # Time to wait for the Space to update after push (in seconds) # Adjust this based on how long your Space typically takes to build/restart SPACE_UPDATE_WAIT_TIME=30 # Number of retries for git push in case of server errors MAX_PUSH_RETRIES=3 RETRY_WAIT_TIME=10 # --- Script Logic --- # Get the absolute path of the script's directory SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" TARGET_SPACE_DIR="$SCRIPT_DIR/$SPACE_DIR" echo "-----------------------------------------------------" echo "Starting Hugging Face Space Deployment and Test" echo "-----------------------------------------------------" echo "Space Directory: $TARGET_SPACE_DIR" echo "Space URL: $SPACE_URL" echo "Test Endpoint: $TEST_ENDPOINT" echo "-----------------------------------------------------" # Step 1: Navigate to the Space directory echo "\nStep 1: Navigating to $TARGET_SPACE_DIR..." cd "$TARGET_SPACE_DIR" || { echo "Error: Could not navigate to $SPACE_DIR. Check if the directory exists."; exit 1; } # Step 1.1: Clean npm cache to avoid installation errors echo "\nStep 1.1: Cleaning npm cache to prevent installation errors..." npm cache clean --force # Step 1.5: Check if Git LFS is installed and initialize it echo "\nStep 1.5: Checking Git LFS setup..." if ! command -v git-lfs &> /dev/null; then echo "Error: Git LFS is not installed. Please install Git LFS first." echo "On macOS, run: brew install git-lfs" echo "On Linux, run: sudo apt-get install git-lfs (Debian/Ubuntu) or sudo yum install git-lfs (CentOS)" echo "On Windows, download from https://git-lfs.github.com/" exit 1 else echo "Git LFS is installed. Initializing..." git lfs install # Track large files (adjust patterns as needed for your project) echo "Tracking large files in backend/bin/ with Git LFS..." git lfs track "backend/bin/*" fi # Step 2: Commit and push changes to Hugging Face Hub echo "\nStep 2: Committing and pushing changes to Hugging Face Hub..." # Add all changes echo "Adding all changes..." git add . # Commit changes if there are any git diff-index --quiet HEAD || { echo "Committing changes with message: $COMMIT_MESSAGE..."; git commit -m "$COMMIT_MESSAGE"; } || { echo "No changes to commit. Proceeding to push (in case of remote changes or first push)..."; } # Push to Hugging Face Hub with retries for server errors echo "Pushing to Hugging Face Hub (origin main)..." for ((i=1; i<=$MAX_PUSH_RETRIES; i++)); do echo "Push attempt $i of $MAX_PUSH_RETRIES..." # Use --force for the first attempt due to history rewrite from Git LFS migration if [ $i -eq 1 ]; then git push --verbose origin main --force && { echo "Push successful on attempt $i."; break; } else git push --verbose origin main && { echo "Push successful on attempt $i."; break; } fi echo "Error: Git push failed (attempt $i)." if [ $i -lt $MAX_PUSH_RETRIES ]; then echo "Waiting $RETRY_WAIT_TIME seconds before retry..." sleep $RETRY_WAIT_TIME fi done # Check if push was successful after all retries if [ $i -gt $MAX_PUSH_RETRIES ]; then echo "Error: Git push failed after $MAX_PUSH_RETRIES attempts." echo "All retries failed. Please check your git remote, branch, authentication, and internet connection." echo "You might need to run 'huggingface-cli login'." echo "Also, ensure large files are tracked with Git LFS." exit 1 fi # Step 3: Wait for Space to update echo "\nStep 3: Waiting for Space to update (this may take a while)..." echo "Waiting $SPACE_UPDATE_WAIT_TIME seconds..." sleep $SPACE_UPDATE_WAIT_TIME # Step 4: Test the Space endpoint echo "\nStep 4: Testing endpoint $SPACE_URL$TEST_ENDPOINT..." # Use curl to test the endpoint if curl -s -f "$SPACE_URL$TEST_ENDPOINT" > /dev/null; then echo "Success: Endpoint $SPACE_URL$TEST_ENDPOINT is reachable and returned a successful response." else echo "Error: Endpoint $SPACE_URL$TEST_ENDPOINT is not reachable or returned an error." echo "Please check if the Space is running correctly at $SPACE_URL." echo "You can also check the build logs in your Hugging Face Space settings for more details." fi echo "\nDeployment and test script completed."