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}")