File size: 3,820 Bytes
7762e8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Upload trained model to Hugging Face Hub
"""
import argparse
import sys
import os
from huggingface_hub import HfApi, create_repo
from transformers import AutoTokenizer, AutoModelForSequenceClassification

# Add parent directory to path (if needed)
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))


def upload_to_hub(
    model_path: str,
    repo_name: str,
    organization: str = None,
    private: bool = False
):
    """
    Upload model to Hugging Face Hub.
    
    Args:
        model_path: Path to the trained model
        repo_name: Name for the repository on Hugging Face Hub
        organization: Organization name (optional)
        private: Whether to make the repository private
    """
    print("=" * 60)
    print("Uploading Model to Hugging Face Hub")
    print("=" * 60)
    
    # Create full repo ID
    if organization:
        repo_id = f"{organization}/{repo_name}"
    else:
        repo_id = repo_name
    
    print(f"\nRepository: {repo_id}")
    print(f"Private: {private}")
    
    # Load model and tokenizer
    print("\n[1/3] Loading model...")
    try:
        tokenizer = AutoTokenizer.from_pretrained(model_path)
        model = AutoModelForSequenceClassification.from_pretrained(model_path)
        print("โœ“ Model loaded successfully")
    except Exception as e:
        print(f"โœ— Error loading model: {e}")
        return
    
    # Create repository
    print("\n[2/3] Creating repository...")
    try:
        create_repo(
            repo_id=repo_id,
            repo_type="model",
            exist_ok=True,
            private=private
        )
        print(f"โœ“ Repository created/verified: {repo_id}")
    except Exception as e:
        print(f"โœ— Error creating repository: {e}")
        print("\nMake sure you're logged in:")
        print("  huggingface-cli login")
        return
    
    # Push to hub
    print("\n[3/3] Uploading model and tokenizer...")
    try:
        model.push_to_hub(repo_id)
        tokenizer.push_to_hub(repo_id)
        print("โœ“ Upload complete!")
    except Exception as e:
        print(f"โœ— Error uploading: {e}")
        return
    
    print("\n" + "=" * 60)
    print("Success! ๐ŸŽ‰")
    print("=" * 60)
    print(f"\nYour model is now available at:")
    print(f"https://huggingface.co/{repo_id}")
    
    print("\nTo use your model:")
    print(f"""
from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained("{repo_id}")
model = AutoModelForSequenceClassification.from_pretrained("{repo_id}")
""")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Upload model to Hugging Face Hub")
    parser.add_argument(
        "--model-path",
        type=str,
        default="./results/final_model",
        help="Path to the trained model"
    )
    parser.add_argument(
        "--repo-name",
        type=str,
        required=True,
        help="Name for the repository on Hugging Face Hub"
    )
    parser.add_argument(
        "--organization",
        type=str,
        default=None,
        help="Organization name (optional)"
    )
    parser.add_argument(
        "--private",
        action="store_true",
        help="Make the repository private"
    )
    args = parser.parse_args()
    
    print("\nBefore uploading, make sure you:")
    print("1. Have a Hugging Face account")
    print("2. Are logged in: huggingface-cli login")
    print("3. Have reviewed the model card (MODEL_CARD.md)")
    
    response = input("\nProceed with upload? (yes/no): ")
    if response.lower() in ['yes', 'y']:
        upload_to_hub(
            args.model_path,
            args.repo_name,
            args.organization,
            args.private
        )
    else:
        print("Upload cancelled.")