File size: 1,109 Bytes
9cb1789 |
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 |
import os
from huggingface_hub import HfApi, HfFolder, Repository
from transformers import AutoModel, AutoTokenizer
# Set your Hugging Face credentials
HUGGINGFACE_TOKEN = "your_huggingface_access_token" # Replace with your token
MODEL_NAME = "your-model-name" # Name of the model on Hugging Face Hub (e.g., "your-username/your-model")
# Save token for authentication
HfFolder.save_token(HUGGINGFACE_TOKEN)
# Load a model (Example: BERT)
model = AutoModel.from_pretrained("bert-base-uncased")
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
# Save the model and tokenizer locally
MODEL_DIR = "./my_model"
model.save_pretrained(MODEL_DIR)
tokenizer.save_pretrained(MODEL_DIR)
# Upload model to Hugging Face Hub
api = HfApi()
api.create_repo(repo_id=MODEL_NAME, exist_ok=True) # Create repo if it doesn't exist
# Clone and push the model
repo = Repository(local_dir=MODEL_DIR, clone_from=MODEL_NAME)
repo.git_add()
repo.git_commit("Initial commit: Upload model")
repo.git_push()
print(f"Model pushed to Hugging Face: https://huggingface.co/{MODEL_NAME}")
|