File size: 3,203 Bytes
ccd807f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Hugging Face Hub Uploader Script for LawSLM.

Uploads model weights, tokenizer, configs, and repository card (README.md & MODEL_CARD.md)
to Hugging Face Hub while filtering out build/node artifacts.
"""

import argparse
import os
import sys

try:
    from huggingface_hub import HfApi, create_repo
except ImportError:
    print("Package 'huggingface_hub' is required. Install using: pip install huggingface_hub")
    sys.exit(1)


def push_to_huggingface(repo_id: str, local_dir: str = ".", token: str = None, private: bool = False) -> str:
    """
    Pushes project repository and model card to Hugging Face Hub.
    """
    # Clean token string if wrapped in quotes
    if token:
        token = token.strip('"').strip("'").strip()
    else:
        token = os.getenv("HF_TOKEN")
        if token:
            token = token.strip('"').strip("'").strip()

    if "your-username" in repo_id:
        print("\n[ERROR] 'your-username' is a placeholder!")
        print("Please replace 'your-username' with your real Hugging Face account username.")
        print("Example: python scripts/push_to_hf.py --repo_id Amit123103/Law_model_slm --token hf_XXXXX\n")
        sys.exit(1)

    api = HfApi(token=token)

    # Auto-resolve username if only repo name is supplied
    if "/" not in repo_id and token:
        try:
            user_info = api.whoami(token=token)
            username = user_info.get("name")
            if username:
                repo_id = f"{username}/{repo_id}"
                print(f"Auto-resolved Hugging Face repository ID to: '{repo_id}'")
        except Exception:
            pass

    print(f"Creating/verifying Hugging Face repository '{repo_id}'...")
    create_repo(repo_id=repo_id, repo_type="model", private=private, exist_ok=True, token=token)
    
    # Patterns to ignore when uploading to HF Hub (prevents unsafe file warnings)
    ignore_patterns = [
        "node_modules/*",
        "**/node_modules/*",
        "dist/*",
        "**/dist/*",
        ".oxlintrc.json",
        ".pytest_cache/*",
        "__pycache__/*",
        "**/__pycache__/*",
        "*.pyc",
        ".env",
        ".git/*",
        "*.log"
    ]
    
    print(f"Uploading repository '{local_dir}' to Hugging Face Hub ({repo_id})...")
    url = api.upload_folder(
        folder_path=local_dir,
        repo_id=repo_id,
        repo_type="model",
        ignore_patterns=ignore_patterns,
        token=token
    )
    
    print(f"\nSuccessfully pushed model repository to Hugging Face!")
    print(f"Repository URL: {url}")
    return url


def main():
    parser = argparse.ArgumentParser(description="Push LawSLM code, config, and Model Card to Hugging Face Hub.")
    parser.add_argument("--repo_id", type=str, required=True, help="Hugging Face repo ID (e.g., 'Amit123103/Law_model_slm')")
    parser.add_argument("--token", type=str, default=os.getenv("HF_TOKEN"), help="Hugging Face User Access Token (Write permission)")
    parser.add_argument("--private", action="store_true", help="Create as private repository")
    args = parser.parse_args()

    push_to_huggingface(repo_id=args.repo_id, token=args.token, private=args.private)


if __name__ == "__main__":
    main()