File size: 5,830 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/bash

# Hugging Face Upload Script for DTO Framework
# Uses HF Hub with Xet backend for all uploads
# Prometheus - Head of Data Migration & Transfer Operations

set -euo pipefail

# Load .env file
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DTO_ROOT="$(dirname "$(dirname "$SCRIPT_DIR")")"
ENV_FILE="$DTO_ROOT/.env"

if [ -f "$ENV_FILE" ]; then
    export $(grep -v '^#' "$ENV_FILE" | xargs)
    echo "βœ… Loaded .env file from $ENV_FILE"
else
    echo "❌ .env file not found at $ENV_FILE"
    exit 1
fi

# Configuration
HF_HOME="${HF_HOME:-/workspace/.hf_home}"
LOG_DIR="/var/log/dto"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)

# 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"
}

# Verify authentication
verify_auth() {
    log "Verifying Hugging Face authentication"
    
    if [ -z "$HF_TOKEN" ]; then
        error "HF_TOKEN not set. Please check .env file"
        exit 1
    fi
    
    python3 -c "
from huggingface_hub import HfApi
try:
    api = HfApi(token='$HF_TOKEN')
    user = api.whoami()
    print(f'βœ… Authenticated as: {user[\"name\"]}')
    print(f'βœ… Organization: {user.get(\"orgs\", [{}])[0].get(\"name\", \"None\")}')
except Exception as e:
    print(f'❌ Authentication failed: {e}')
    exit(1)
"
}

# Upload models to HF Hub
upload_models() {
    local model_dir="$1"
    local repo_id="${2:-$HF_REPO_MODELS}"
    
    log "Uploading models from $model_dir to $repo_id"
    
    python3 -c "
import os
from integrations.huggingface_client import HuggingFaceClient

client = HuggingFaceClient()
if not client.is_authenticated():
    print('❌ Not authenticated')
    exit(1)

# Upload all model files
for root, dirs, files in os.walk('$model_dir'):
    for file in files:
        if file.endswith(('.safetensors', '.pt', '.bin', '.json')):
            full_path = os.path.join(root, file)
            rel_path = os.path.relpath(full_path, '$model_dir')
            
            print(f'πŸ“€ Uploading: {rel_path}')
            success = client.upload_artifact(full_path, rel_path, '$repo_id')
            if not success:
                print(f'❌ Failed to upload: {rel_path}')
                exit(1)

print('βœ… All models uploaded successfully')
"
}

# Upload datasets to HF Hub
upload_datasets() {
    local data_dir="$1"
    local repo_id="${2:-$HF_REPO_DATASETS}"
    
    log "Uploading datasets from $data_dir to $repo_id"
    
    python3 -c "
import os
from integrations.huggingface_client import HuggingFaceClient

client = HuggingFaceClient()
if not client.is_authenticated():
    print('❌ Not authenticated')
    exit(1)

# Upload all dataset files
for root, dirs, files in os.walk('$data_dir'):
    for file in files:
        if file.endswith(('.parquet', '.jsonl', '.csv', '.txt')):
            full_path = os.path.join(root, file)
            rel_path = os.path.relpath(full_path, '$data_dir')
            
            print(f'πŸ“€ Uploading: {rel_path}')
            success = client.upload_artifact(full_path, rel_path, '$repo_id')
            if not success:
                print(f'❌ Failed to upload: {rel_path}')
                exit(1)

print('βœ… All datasets uploaded successfully')
"
}

# Upload artifacts
upload_artifacts() {
    local artifacts_dir="$1"
    local repo_id="${2:-$HF_REPO_ARTIFACTS}"
    
    log "Uploading artifacts from $artifacts_dir to $repo_id"
    
    python3 -c "
import os
from integrations.huggingface_client import HuggingFaceClient

client = HuggingFaceClient()
if not client.is_authenticated():
    print('❌ Not authenticated')
    exit(1)

# Upload all artifact files
for root, dirs, files in os.walk('$artifacts_dir'):
    for file in files:
        full_path = os.path.join(root, file)
        rel_path = os.path.relpath(full_path, '$artifacts_dir')
        
        print(f'πŸ“€ Uploading: {rel_path}')
        success = client.upload_artifact(full_path, rel_path, '$repo_id')
        if not success:
            print(f'❌ Failed to upload: {rel_path}')
            exit(1)

print('βœ… All artifacts uploaded successfully')
"
}

# Create repository if it doesn't exist
create_repo_if_needed() {
    local repo_id="$1"
    local repo_type="${2:-model}"
    
    log "Ensuring repository exists: $repo_id"
    
    python3 -c "
from huggingface_hub import HfApi, RepositoryNotFoundError

api = HfApi(token='$HF_TOKEN')
try:
    api.repo_info('$repo_id')
    print('βœ… Repository already exists')
except RepositoryNotFoundError:
    print('πŸ“¦ Creating new repository')
    api.create_repo('$repo_id', repo_type='$repo_type', private=True)
    print('βœ… Repository created successfully')
except Exception as e:
    print(f'❌ Error checking repository: {e}')
    exit(1)
"
}

# Main upload function
main() {
    log "Starting DTO Framework upload to Hugging Face Hub"
    
    # Verify authentication first
    verify_auth
    
    # Create repositories if they don't exist
    create_repo_if_needed "$HF_REPO_MODELS" "model"
    create_repo_if_needed "$HF_REPO_DATASETS" "dataset"
    create_repo_if_needed "$HF_REPO_ARTIFACTS" "model"
    
    # Upload everything
    upload_models "/data/adaptai/aiml/02_models" "$HF_REPO_MODELS"
    upload_datasets "/data/adaptai/aiml/03_training" "$HF_REPO_DATASETS"
    upload_artifacts "/data/adaptai/aiml/04_data" "$HF_REPO_ARTIFACTS"
    
    log "βœ… All uploads completed successfully!"
    log "πŸ“Š Models:    https://huggingface.co/$HF_REPO_MODELS"
    log "πŸ“Š Datasets:  https://huggingface.co/$HF_REPO_DATASETS"
    log "πŸ“Š Artifacts: https://huggingface.co/$HF_REPO_ARTIFACTS"
}

# Run main function
main "$@"