File size: 2,704 Bytes
1d86315
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Push Bhaari AI artifacts to HuggingFace Hub
"""

import os
from pathlib import Path
from huggingface_hub import HfApi, login
from datetime import datetime


def push_to_huggingface(
    folder_path: str,
    repo_id: str = "mohit-vashishta/bhaari-tok-v1",
    commit_message: str = None,
    token: str = None
):
    """
    Push folder to HuggingFace Hub
    
    Args:
        folder_path: Path to folder to upload
        repo_id: HuggingFace repo ID
        commit_message: Commit message
        token: HF token (or set HF_TOKEN env var)
    """
    token = token or os.environ.get("HF_TOKEN")
    if not token:
        raise ValueError("HF_TOKEN not set. Set it as environment variable or pass as argument.")
    
    # Login
    login(token=token)
    
    # Initialize API
    api = HfApi()
    
    # Create repo if not exists
    try:
        api.create_repo(repo_id=repo_id, repo_type="model", exist_ok=True)
        print(f"Repo created/exists: {repo_id}")
    except Exception as e:
        print(f"Repo creation note: {e}")
    
    # Upload folder
    if commit_message is None:
        commit_message = f"Update {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
    
    print(f"Uploading {folder_path} to {repo_id}...")
    api.upload_folder(
        folder_path=folder_path,
        repo_id=repo_id,
        commit_message=commit_message
    )
    
    print(f"Successfully uploaded to: https://huggingface.co/{repo_id}")
    return f"https://huggingface.co/{repo_id}"


def push_checkpoint(
    checkpoint_path: str,
    phase: str,
    step: int,
    loss: float,
    token: str = None
):
    """Push training checkpoint to HuggingFace"""
    repo_id = f"mohit-vashishta/bhaari-ai-{phase}"
    commit_message = f"Checkpoint {phase} step {step}, loss: {loss:.4f}"
    
    return push_to_huggingface(
        folder_path=checkpoint_path,
        repo_id=repo_id,
        commit_message=commit_message,
        token=token
    )


def push_report(
    report_path: str,
    phase: str,
    token: str = None
):
    """Push training report to HuggingFace"""
    repo_id = f"mohit-vashishta/bhaari-ai-reports"
    commit_message = f"Report: {phase} - {datetime.now().strftime('%Y-%m-%d')}"
    
    return push_to_huggingface(
        folder_path=str(Path(report_path).parent),
        repo_id=repo_id,
        commit_message=commit_message,
        token=token
    )


if __name__ == "__main__":
    import sys
    
    if len(sys.argv) < 2:
        print("Usage: python push_to_hf.py <folder_path> [repo_id]")
        sys.exit(1)
    
    folder = sys.argv[1]
    repo = sys.argv[2] if len(sys.argv) > 2 else "mohit-vashishta/bhaari-tok-v1"
    
    push_to_huggingface(folder, repo)