Here’s a minimal, working example showing how you can create a Hugging Face model repo and upload files into it using Python.
1. Install dependencies
uv pip install huggingface_hub
2. Example Python script
from huggingface_hub import HfApi, create_repo, upload_file
import os
# 1. Authenticate (make sure HF_TOKEN is in your environment variables)
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
raise EnvironmentError("Missing HF_TOKEN in environment variables.")
# 2. Define repo details
repo_name = "test-model-repo"
namespace = "your-username" # replace with your HF username/org
repo_id = f"{namespace}/{repo_name}"
# 3. Create repo (only first time, idempotent)
create_repo(repo_id, token=hf_token, repo_type="model", exist_ok=True)
print(f"✅ Repo created: https://huggingface.co/{repo_id}")
# 4. Upload files
api = HfApi()
# Example: upload a config.json file
upload_file(
path_or_fileobj="config.json", # local file path
path_in_repo="config.json", # where it will appear in repo
repo_id=repo_id,
token=hf_token
)
# Example: upload model weights
upload_file(
path_or_fileobj="pytorch_model.bin",
path_in_repo="pytorch_model.bin",
repo_id=repo_id,
token=hf_token
)
print("✅ Files uploaded successfully!")
3. Workflow
Get your token from Hugging Face settings → Access Tokens.
Save it in environment:
export HF_TOKEN=your_token_hereRun the script → it will:
- Create a model repo if not exists.
- Upload your files (e.g.,
config.json,pytorch_model.bin). - You’ll see them in
https://huggingface.co/your-username/test-model-repo.
👉 Do you want me to also show an example of uploading multiple files/folders at once (like the whole model directory with tokenizer, config, and weights)?