| #!/bin/bash |
|
|
| |
| |
| |
|
|
| set -euo pipefail |
|
|
| |
| 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 |
|
|
| |
| HF_HOME="${HF_HOME:-/workspace/.hf_home}" |
| LOG_DIR="/var/log/dto" |
| TIMESTAMP=$(date +%Y%m%d_%H%M%S) |
|
|
| |
| RED='\033[0;31m' |
| GREEN='\033[0;32m' |
| YELLOW='\033[1;33m' |
| NC='\033[0m' |
|
|
| |
| 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_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() { |
| 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() { |
| 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() { |
| 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_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() { |
| log "Starting DTO Framework upload to Hugging Face Hub" |
| |
| |
| verify_auth |
| |
| |
| 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_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" |
| } |
|
|
| |
| main "$@" |