File size: 1,928 Bytes
0300160 |
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 |
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
```bash
uv pip install huggingface_hub
```
---
### 2. Example Python script
```python
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
1. Get your token from [Hugging Face settings → Access Tokens](https://huggingface.co/settings/tokens).
2. Save it in environment:
```bash
export HF_TOKEN=your_token_here
```
3. Run 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)?
|