CU1-X / deploy_hf_space.sh
Abdelkader HASSINE
Deploy CU1-X to Hugging Face Spaces
ff03012
raw
history blame
6.73 kB
#!/bin/bash
# Script de déploiement pour Hugging Face Spaces
# Build et push le Space vers Hugging Face
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
print_info() { echo -e "${BLUE}ℹ️ $1${NC}"; }
print_success() { echo -e "${GREEN}✅ $1${NC}"; }
print_warning() { echo -e "${YELLOW}⚠️ $1${NC}"; }
print_error() { echo -e "${RED}❌ $1${NC}"; }
echo ""
print_info "🚀 Deploying CU1-X to Hugging Face Spaces"
echo "================================================"
echo ""
# Check if we're in a git repo
if [ ! -d ".git" ]; then
print_error "Not a git repository!"
print_info "Initializing git repository..."
git init
print_success "Git repository initialized"
fi
# Check Git LFS
print_info "Configuring Git LFS..."
if ! command -v git-lfs &> /dev/null; then
print_error "Git LFS not installed!"
print_info "Install with: brew install git-lfs (macOS) or sudo apt install git-lfs (Linux)"
exit 1
fi
git lfs install > /dev/null 2>&1 || true
# Ensure model.pth is tracked
if [ -f "model.pth" ]; then
if ! git lfs ls-files | grep -q "model.pth"; then
print_info "Adding model.pth to Git LFS..."
git lfs track "*.pth"
git add .gitattributes
print_success "model.pth configured for Git LFS"
else
print_success "model.pth already tracked by Git LFS"
fi
else
print_error "model.pth not found!"
exit 1
fi
# Check HF remote
print_info "Checking Hugging Face remote..."
if git remote | grep -q "origin"; then
REMOTE_URL=$(git remote get-url origin 2>/dev/null || echo "")
if echo "$REMOTE_URL" | grep -q "huggingface.co"; then
print_success "HF Space remote configured: $REMOTE_URL"
SPACE_URL=$(echo "$REMOTE_URL" | sed -E 's|.*spaces/([^/]+)/([^/]+).*|\1/\2|')
print_info "Space URL: https://huggingface.co/spaces/$SPACE_URL"
else
print_warning "Remote exists but doesn't look like HF Space"
print_info "Current remote: $REMOTE_URL"
fi
else
print_warning "No remote configured"
print_info "You'll need to add a remote:"
print_info " git remote add origin https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME"
read -p "Do you want to configure it now? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
read -p "Enter your HF username: " HF_USERNAME
read -p "Enter your Space name: " SPACE_NAME
git remote add origin "https://huggingface.co/spaces/$HF_USERNAME/$SPACE_NAME"
print_success "Remote configured"
SPACE_URL="$HF_USERNAME/$SPACE_NAME"
else
print_error "Cannot deploy without remote"
exit 1
fi
fi
# Check login
print_info "Checking Hugging Face login..."
if command -v hf &> /dev/null; then
if hf auth whoami &> /dev/null 2>&1; then
USERNAME=$(hf auth whoami 2>/dev/null | head -n1)
print_success "Logged in as: $USERNAME"
else
print_warning "Not logged in"
print_info "Logging in..."
hf login
fi
elif command -v huggingface-cli &> /dev/null; then
if huggingface-cli whoami &> /dev/null 2>&1; then
USERNAME=$(huggingface-cli whoami 2>/dev/null | head -n1)
print_success "Logged in as: $USERNAME"
else
print_warning "Not logged in"
print_info "Logging in..."
huggingface-cli login
fi
else
print_error "Hugging Face CLI not found!"
print_info "Install: pip install huggingface-hub"
exit 1
fi
# Ensure requirements.txt is complete
print_info "Checking requirements.txt..."
if [ -f "requirements-full.txt" ] && [ -f "requirements.txt" ]; then
FULL_LINES=$(wc -l < requirements-full.txt)
CURRENT_LINES=$(wc -l < requirements.txt)
if [ $CURRENT_LINES -lt $FULL_LINES ]; then
print_warning "requirements.txt seems incomplete"
read -p "Use requirements-full.txt? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
cp requirements-full.txt requirements.txt
print_success "Updated requirements.txt from requirements-full.txt"
fi
fi
fi
# Stage all files
print_info "Staging files..."
git add .
print_success "Files staged"
# Check if there are changes
if [ -z "$(git status --porcelain)" ]; then
print_warning "No changes to commit"
print_info "Everything is already up to date"
else
# Show what will be committed
print_info "Changes to commit:"
git status --short
# Commit
print_info "Creating commit..."
COMMIT_MSG="Deploy CU1-X to Hugging Face Spaces
- Multi-model AI pipeline (RF-DETR, CLIP, OCR, BLIP)
- Unified API architecture
- Gradio web interface
- Full model weights included via Git LFS
- Ready for production deployment"
git commit -m "$COMMIT_MSG" || {
print_error "Commit failed"
exit 1
}
print_success "Changes committed"
fi
# Push to Hugging Face
print_info "Pushing to Hugging Face Spaces..."
print_warning "This may take several minutes (model.pth is 510MB)..."
echo ""
BRANCH=$(git branch --show-current 2>/dev/null || echo "main")
if git push -u origin "$BRANCH" 2>&1 | tee /tmp/hf_push.log; then
print_success "Push completed successfully!"
echo ""
echo "================================================"
print_success "🎉 Deployment Successful!"
echo "================================================"
echo ""
if [ -n "$SPACE_URL" ]; then
print_info "Your Space is deploying at:"
echo " https://huggingface.co/spaces/$SPACE_URL"
echo ""
print_info "Build progress:"
echo " https://huggingface.co/spaces/$SPACE_URL/logs"
echo ""
print_info "Once deployed, your app will be at:"
echo " https://huggingface.co/spaces/$SPACE_URL"
echo ""
print_info "API endpoint:"
echo " https://$SPACE_URL.hf.space/api/predict"
echo ""
fi
print_warning "First build may take 5-10 minutes"
print_info "HF Spaces will automatically:"
print_info " - Install dependencies from requirements.txt"
print_info " - Download models (CLIP, BLIP, EasyOCR)"
print_info " - Start app.py"
print_info " - Expose Gradio interface and API"
echo ""
print_success "All done! 🎉"
else
print_error "Push failed!"
echo ""
print_info "Common issues:"
print_info "1. Authentication failed: Run 'hf login' or 'huggingface-cli login'"
print_info "2. Git LFS error: Ensure Git LFS is installed and model.pth is tracked"
print_info "3. Network error: Check your internet connection"
echo ""
print_info "Check the error above for details"
exit 1
fi