| # upload_safetensors.py | |
| from huggingface_hub import HfApi | |
| from safetensors.torch import save_file | |
| import torch | |
| import io | |
| # Replace with your HF token and repo | |
| token = "hf_your_token_here" | |
| repo_id = "yourname/Trouter-20b" | |
| api = HfApi(token=token) | |
| # Loop to create and upload 5 in-memory safetensors | |
| for i in range(1, 6): | |
| # Create dummy tensor | |
| weights = {f"layer{i}.weight": torch.randn(2, 2)} | |
| # Save tensor to in-memory buffer | |
| buffer = io.BytesIO() | |
| save_file(weights, buffer) | |
| buffer.seek(0) | |
| # Upload directly to Hugging Face | |
| api.upload_file( | |
| path_or_fileobj=buffer, | |
| path_in_repo=f"part{i}.safetensors", | |
| repo_id=repo_id, | |
| repo_type="model" | |
| ) | |
| print(f"✅ Uploaded part{i}.safetensors directly to Hugging Face") | |