File size: 1,449 Bytes
f94fdeb eb898c5 f94fdeb eb898c5 f94fdeb eb898c5 f94fdeb |
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 |
# Usage: python upload.py --dir <dir> --hub-name <hub_name>
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import argparse
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--path", type=str)
parser.add_argument("--hub-name", type=str)
return parser.parse_args()
def main():
args = get_args()
print(f"Args: {args}")
print(f"Loading tokenizer from path: {args.path}")
tokenizer = AutoTokenizer.from_pretrained(args.path)
print(f"Pushing the tokenizer to the Hub at {args.hub_name}")
tokenizer.push_to_hub(args.hub_name, private=True)
print(f"Loading model from path: {args.path}")
model = AutoModelForCausalLM.from_pretrained(
args.path,
return_dict=True,
torch_dtype=torch.bfloat16,
device_map="auto",
)
print(f"Pushing the model to the Hub at {args.hub_name}")
model.push_to_hub(args.hub_name, private=True)
from huggingface_hub import HfApi
api = HfApi()
for file in ["training_args.bin", "all_results.json", "eval_results.json"]:
try:
api.upload_file(
path_or_fileobj=f"{args.path}/{file}",
path_in_repo=file,
repo_id=args.hub_name,
repo_type="model",
)
except Exception as e:
print(f"Failed to upload {file}: {e}")
if __name__ == "__main__":
main() |