|
|
|
|
|
""" |
|
|
Hugging Face Hub Client Integration for Data Transfer Operations |
|
|
Uses HF Hub with Xet backend for artifact storage and management |
|
|
""" |
|
|
|
|
|
import os |
|
|
import logging |
|
|
from typing import Dict, List, Optional, Union |
|
|
from pathlib import Path |
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.INFO) |
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
try: |
|
|
from huggingface_hub import HfApi, HfFolder, snapshot_download, upload_file, create_repo |
|
|
from huggingface_hub.utils import RepositoryNotFoundError |
|
|
HF_AVAILABLE = True |
|
|
except ImportError: |
|
|
HF_AVAILABLE = False |
|
|
logger.warning("huggingface_hub not available. Install with: pip install huggingface_hub") |
|
|
|
|
|
class HuggingFaceClient: |
|
|
"""Client for Hugging Face Hub operations with Xet backend""" |
|
|
|
|
|
def __init__(self, repo_id: Optional[str] = None, token: Optional[str] = None): |
|
|
"""Initialize HF client with optional repo and token""" |
|
|
self.repo_id = repo_id |
|
|
self.token = token or os.getenv('HF_TOKEN') or HfFolder.get_token() if HF_AVAILABLE else None |
|
|
self.api = HfApi(token=self.token) if HF_AVAILABLE else None |
|
|
|
|
|
def is_authenticated(self) -> bool: |
|
|
"""Check if authenticated with Hugging Face Hub""" |
|
|
if not HF_AVAILABLE: |
|
|
return False |
|
|
try: |
|
|
return self.api.is_authenticated |
|
|
except: |
|
|
return False |
|
|
|
|
|
def upload_artifact(self, local_path: str, repo_path: str, repo_id: Optional[str] = None) -> bool: |
|
|
"""Upload artifact to HF Hub repo (uses Xet backend transparently)""" |
|
|
if not HF_AVAILABLE: |
|
|
logger.error("huggingface_hub not available") |
|
|
return False |
|
|
|
|
|
repo_id = repo_id or self.repo_id |
|
|
if not repo_id: |
|
|
logger.error("No repository ID provided") |
|
|
return False |
|
|
|
|
|
try: |
|
|
|
|
|
try: |
|
|
self.api.repo_info(repo_id) |
|
|
except RepositoryNotFoundError: |
|
|
logger.info(f"Creating repository: {repo_id}") |
|
|
self.api.create_repo(repo_id, repo_type="model", private=True) |
|
|
|
|
|
|
|
|
upload_file( |
|
|
path_or_fileobj=local_path, |
|
|
path_in_repo=repo_path, |
|
|
repo_id=repo_id, |
|
|
token=self.token |
|
|
) |
|
|
|
|
|
logger.info(f"Uploaded {local_path} to {repo_id}/{repo_path}") |
|
|
return True |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Upload failed: {e}") |
|
|
return False |
|
|
|
|
|
def download_artifact(self, repo_path: str, local_path: str, repo_id: Optional[str] = None) -> bool: |
|
|
"""Download artifact from HF Hub repo""" |
|
|
if not HF_AVAILABLE: |
|
|
logger.error("huggingface_hub not available") |
|
|
return False |
|
|
|
|
|
repo_id = repo_id or self.repo_id |
|
|
if not repo_id: |
|
|
logger.error("No repository ID provided") |
|
|
return False |
|
|
|
|
|
try: |
|
|
|
|
|
downloaded_path = snapshot_download( |
|
|
repo_id=repo_id, |
|
|
allow_patterns=repo_path, |
|
|
local_dir=os.path.dirname(local_path), |
|
|
token=self.token |
|
|
) |
|
|
|
|
|
|
|
|
import shutil |
|
|
actual_file = os.path.join(downloaded_path, repo_path) |
|
|
if os.path.exists(actual_file): |
|
|
shutil.move(actual_file, local_path) |
|
|
logger.info(f"Downloaded {repo_path} to {local_path}") |
|
|
return True |
|
|
else: |
|
|
logger.error(f"File not found in download: {repo_path}") |
|
|
return False |
|
|
|
|
|
except Exception as e: |
|
|
logger.error(f"Download failed: {e}") |
|
|
return False |
|
|
|
|
|
def list_artifacts(self, repo_id: Optional[str] = None) -> List[str]: |
|
|
"""List artifacts in HF Hub repo""" |
|
|
if not HF_AVAILABLE: |
|
|
logger.error("huggingface_hub not available") |
|
|
return [] |
|
|
|
|
|
repo_id = repo_id or self.repo_id |
|
|
if not repo_id: |
|
|
logger.error("No repository ID provided") |
|
|
return [] |
|
|
|
|
|
try: |
|
|
repo_info = self.api.repo_info(repo_id) |
|
|
return [file.rfilename for file in repo_info.siblings] |
|
|
except Exception as e: |
|
|
logger.error(f"List failed: {e}") |
|
|
return [] |
|
|
|
|
|
def get_repo_info(self, repo_id: Optional[str] = None) -> Optional[Dict]: |
|
|
"""Get repository information""" |
|
|
if not HF_AVAILABLE: |
|
|
return None |
|
|
|
|
|
repo_id = repo_id or self.repo_id |
|
|
if not repo_id: |
|
|
return None |
|
|
|
|
|
try: |
|
|
info = self.api.repo_info(repo_id) |
|
|
return { |
|
|
'id': info.id, |
|
|
'private': info.private, |
|
|
'size': getattr(info, 'size', None), |
|
|
'downloads': info.downloads, |
|
|
'last_modified': info.last_modified, |
|
|
'xet_enabled': getattr(info, 'xet_enabled', None), |
|
|
} |
|
|
except Exception as e: |
|
|
logger.error(f"Repo info failed: {e}") |
|
|
return None |
|
|
|
|
|
def initialize_hf_integration(): |
|
|
"""Initialize Hugging Face integration for DTO framework""" |
|
|
client = HuggingFaceClient() |
|
|
|
|
|
if not HF_AVAILABLE: |
|
|
logger.warning("huggingface_hub not available. Please install: pip install huggingface_hub") |
|
|
return None |
|
|
|
|
|
if not client.is_authenticated(): |
|
|
logger.warning("Not authenticated with Hugging Face Hub. Set HF_TOKEN environment variable") |
|
|
|
|
|
return client |
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
hf_client = initialize_hf_integration() |
|
|
if hf_client and hf_client.is_authenticated(): |
|
|
print("Hugging Face integration initialized successfully") |
|
|
print(f"Authenticated as: {hf_client.api.whoami()['name']}") |
|
|
else: |
|
|
print("Hugging Face integration not available or not authenticated") |