File size: 4,291 Bytes
fd357f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/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 "$@"