conduct-regulation / deploy.sh
Sangmin's picture
Add font support and documentation files
d4079fb
#!/bin/bash
# Deployment script for Hugging Face Spaces
# Usage: ./deploy.sh YOUR_HF_USERNAME SPACE_NAME [HF_TOKEN]
set -e
# Check if arguments are provided
if [ $# -lt 2 ]; then
echo "Usage: ./deploy.sh YOUR_HF_USERNAME SPACE_NAME [HF_TOKEN]"
echo "Example: ./deploy.sh johndoe qwen-ocr-detection"
echo " ./deploy.sh johndoe qwen-ocr-detection hf_xxxxxxxxxxxx"
echo ""
echo "Get your token at: https://huggingface.co/settings/tokens"
exit 1
fi
HF_USERNAME=$1
SPACE_NAME=$2
HF_TOKEN=${3:-$HF_TOKEN} # Use provided token or environment variable
echo "🚀 Deploying Qwen OCR Detection to Hugging Face Spaces"
echo "Username: $HF_USERNAME"
echo "Space name: $SPACE_NAME"
echo ""
# Check if huggingface-cli is installed
if ! command -v huggingface-cli &> /dev/null; then
echo "❌ huggingface-cli not found. Installing..."
pip install --upgrade huggingface_hub
fi
# Handle authentication
echo "📝 Checking Hugging Face authentication..."
# If token is provided, use it
if [ ! -z "$HF_TOKEN" ]; then
echo "Using provided token..."
export HF_TOKEN=$HF_TOKEN
# Verify token works
if ! huggingface-cli whoami 2>/dev/null | grep -q "$HF_USERNAME"; then
echo "❌ Token authentication failed or username mismatch!"
echo "Please check your token at: https://huggingface.co/settings/tokens"
echo "Make sure you're using a WRITE token, not READ."
exit 1
fi
echo "✅ Authentication successful!"
else
# Try to check if already logged in
LOGGED_IN_USER=$(huggingface-cli whoami 2>/dev/null | grep "username:" | awk '{print $2}' || echo "")
if [ -z "$LOGGED_IN_USER" ]; then
echo "❌ Not logged in to Hugging Face!"
echo ""
echo "Please login using one of these methods:"
echo ""
echo "Option 1: Run with token:"
echo " ./deploy.sh $HF_USERNAME $SPACE_NAME YOUR_HF_TOKEN"
echo ""
echo "Option 2: Login interactively:"
echo " huggingface-cli login"
echo ""
echo "Option 3: Set environment variable:"
echo " export HF_TOKEN=your_token_here"
echo ""
echo "Get your token at: https://huggingface.co/settings/tokens"
echo "Make sure to use a WRITE token, not READ!"
exit 1
elif [ "$LOGGED_IN_USER" != "$HF_USERNAME" ]; then
echo "⚠️ Warning: Logged in as '$LOGGED_IN_USER' but deploying as '$HF_USERNAME'"
echo "Continue? (y/n)"
read -r response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
echo "Deployment cancelled."
exit 1
fi
else
echo "✅ Already logged in as $HF_USERNAME"
fi
fi
# Initialize git repository if not already initialized
if [ ! -d .git ]; then
echo "📦 Initializing git repository..."
git init
git add .
git commit -m "Initial commit: Qwen OCR Text Detection app"
fi
# Create the Space
echo "🏗️ Creating Space on Hugging Face..."
SPACE_URL="https://huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME"
# Try to create the space (will fail if it already exists, which is OK)
if huggingface-cli repo create "$HF_USERNAME/$SPACE_NAME" --type space --space_sdk gradio 2>/dev/null; then
echo "✅ Space created successfully!"
else
echo "ℹ️ Space might already exist or creation failed, attempting to push anyway..."
fi
# Configure git remote
echo "📤 Configuring git remote..."
if [ ! -z "$HF_TOKEN" ]; then
# Use token in URL for authentication
git remote add origin "https://$HF_USERNAME:$HF_TOKEN@huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME" 2>/dev/null || \
git remote set-url origin "https://$HF_USERNAME:$HF_TOKEN@huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME"
else
# Use standard URL (will use stored credentials)
git remote add origin "$SPACE_URL" 2>/dev/null || \
git remote set-url origin "$SPACE_URL"
fi
# Push to Hugging Face
echo "📤 Pushing to Hugging Face Spaces..."
if git push -u origin main --force; then
echo "✅ Push successful!"
else
echo "❌ Push failed!"
echo ""
echo "Troubleshooting:"
echo "1. Check your authentication token has WRITE permissions"
echo "2. Verify the Space URL: $SPACE_URL"
echo "3. Try creating the Space manually at: https://huggingface.co/new-space"
echo "4. Then run: git push -u origin main --force"
exit 1
fi
echo ""
echo "✅ Deployment complete!"
echo "🌐 Your Space is available at: https://huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME"
echo ""
echo "📝 Note: It may take a few minutes for the Space to build and become available."
echo "Check the build logs at: https://huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME/logs"