#!/bin/bash # Hugging Face Hub Setup Script for DTO Framework # Uses HF Hub with Xet backend transparently # Prometheus - Head of Data Migration & Transfer Operations set -euo pipefail # Configuration HF_HOME="${HF_HOME:-/workspace/.hf_home}" CONFIG_DIR="/etc/dto/huggingface" LOG_DIR="/var/log/dto" # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Logging function log() { echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')]${NC} $1" } error() { echo -e "${RED}[ERROR]${NC} $1" >&2 } warning() { echo -e "${YELLOW}[WARNING]${NC} $1" } # Install Hugging Face Hub install_huggingface_hub() { log "Installing Hugging Face Hub" pip install huggingface_hub --upgrade # Verify installation if python -c "import huggingface_hub; print('huggingface_hub available')" 2>/dev/null; then log "Hugging Face Hub installed successfully" else error "Hugging Face Hub installation failed" exit 1 fi } # Configure Hugging Face environment configure_huggingface() { log "Configuring Hugging Face environment" # Create directories mkdir -p "$CONFIG_DIR" "$LOG_DIR" "$HF_HOME" # Set HF_HOME environment variable echo "export HF_HOME=$HF_HOME" > "$CONFIG_DIR/environment" echo "export HF_TOKEN=\"\"" >> "$CONFIG_DIR/environment" # Set permissions chmod 755 "$HF_HOME" chmod 644 "$CONFIG_DIR/environment" log "Hugging Face environment configured" } # Setup authentication setup_authentication() { log "Setting up Hugging Face authentication" if [ -n "$HF_TOKEN" ]; then log "HF_TOKEN environment variable found" # Test authentication if python -c " from huggingface_hub import HfApi try: api = HfApi(token='$HF_TOKEN') user = api.whoami() print(f'Authenticated as: {user[\"name\"]}') except Exception as e: print(f'Authentication failed: {e}') exit(1) " 2>/dev/null; then log "Hugging Face authentication successful" else warning "Hugging Face authentication test failed" fi else warning "HF_TOKEN not set. Please set HF_TOKEN environment variable for full access" echo "# To authenticate with Hugging Face Hub:" >> "$CONFIG_DIR/README" echo "# 1. Get your token from https://huggingface.co/settings/tokens" >> "$CONFIG_DIR/README" echo "# 2. Export it: export HF_TOKEN=your_token_here" >> "$CONFIG_DIR/README" echo "# 3. Or add to environment file" >> "$CONFIG_DIR/README" fi } # Install dependencies install_dependencies() { log "Installing dependencies" # Update package list apt-get update || yum update || { warning "Package manager update failed - continuing anyway" } # Install required packages if command -v apt-get >/dev/null 2>&1; then apt-get install -y curl python3-pip elif command -v yum >/dev/null 2>&1; then yum install -y curl python3-pip elif command -v dnf >/dev/null 2>&1; then dnf install -y curl python3-pip else warning "Unknown package manager - please install curl and python3-pip manually" fi } # Test Hugging Face functionality test_integration() { log "Testing Hugging Face integration" # Basic functionality test if python -c " from huggingface_hub import HfApi, HfFolder print('Hugging Face Hub available') print('HF_HOME:', HfFolder.path) " 2>/dev/null; then log "Hugging Face integration test passed" else error "Hugging Face integration test failed" exit 1 fi } # Main installation function main() { log "Starting Hugging Face Hub installation for DTO Framework" install_dependencies install_huggingface_hub configure_huggingface setup_authentication test_integration log "Hugging Face Hub installation completed successfully!" log "Next steps:" log " 1. Set HF_TOKEN environment variable for authentication" log " 2. Configure repositories in integrations/huggingface_config.yaml" log " 3. Use integrations/huggingface_client.py for operations" log " 4. Xet backend is automatically handled by Hugging Face" } # Run main function main "$@"