File size: 3,568 Bytes
9919ebb
 
5d9786c
9919ebb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73acdc2
9919ebb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
136
137
138
139
140
141
import argparse
import os
from transformers import AutoModel, AutoModelForCausalLM, AutoTokenizer, AutoConfig
from huggingface_hub import HfApi, login
import sys

def parse_arguments():
    parser = argparse.ArgumentParser(description='Upload model to Hugging Face Hub')
    
    parser.add_argument(
        '--model_path',
        type=str,
        required=True,
        help='Local path to the model'
    )
    
    parser.add_argument(
        '--repo_name',
        type=str,
        required=True,
        help='Destination repo name (format: username/repo_name)'
    )
    
    parser.add_argument(
        '--token',
        type=str,
        required=True,
        help='Hugging Face API token'
    )
    
    parser.add_argument(
        '--model_card',
        type=str,
        default=None,
        help='Path to model card README.md file (optional)'
    )
    
    parser.add_argument(
        '--private',
        action='store_true',
        help='Make the repository private'
    )

    return parser.parse_args()

def create_default_model_card(repo_name):
    return f"""---
language: en
tags:
- pytorch
- transformer
license: apache-2.0
---

# Model Card for {repo_name}

## Model Description

[Provide a brief description of your model here]

## Intended Use

[Describe the intended use of your model]

## Training Procedure

[Describe how the model was trained]

## Evaluation Results

[Provide evaluation metrics and results]

## Limitations

[Describe any known limitations]

## Additional Information

For more information, please visit the model repository.
"""

def upload_model(model_path, repo_name, token, model_card=None, private=False):
    try:
        # Login to Hugging Face
        login(token)
        print("Successfully logged in to Hugging Face")

        # Load model, tokenizer, and config
        print("Loading model, tokenizer, and config...")
        model = AutoModelForCausalLM.from_pretrained(model_path)
        tokenizer = AutoTokenizer.from_pretrained(model_path)
        config = AutoConfig.from_pretrained(model_path)

        # Create model card if not provided
        if model_card is None:
            print("No model card provided. Creating default model card...")
            model_card_content = create_default_model_card(repo_name)
            with open("README.md", "w", encoding="utf-8") as f:
                f.write(model_card_content)
        else:
            # Copy provided model card
            with open(model_card, "r", encoding="utf-8") as f:
                model_card_content = f.read()
            with open("README.md", "w", encoding="utf-8") as f:
                f.write(model_card_content)

        # Push everything to hub
        print(f"Uploading model to {repo_name}...")
        model.push_to_hub(repo_name, private=private)
        tokenizer.push_to_hub(repo_name, private=private)
        config.push_to_hub(repo_name, private=private)

        # Clean up
        if os.path.exists("README.md"):
            os.remove("README.md")

        print(f"Successfully uploaded model to https://huggingface.co/{repo_name}")
        return True

    except Exception as e:
        print(f"Error occurred: {str(e)}")
        return False

def main():
    args = parse_arguments()
    
    success = upload_model(
        model_path=args.model_path,
        repo_name=args.repo_name,
        token=args.token,
        model_card=args.model_card,
        private=args.private
    )
    
    if success:
        sys.exit(0)
    else:
        sys.exit(1)

if __name__ == "__main__":
    main()