v0.0.13: Context Manager
v0.0.13: Context Manager
Version 0.0.13 introduces a context manager to save files directly to the Hub. See below for some examples.
Example with a single file
from huggingface_hub import Repository
repo = Repository("text-files", clone_from="<user>/text-files", use_auth_token=True)
with repo.commit("My first file."):
with open("file.txt", "w+") as f:
f.write(json.dumps({"key": "value"}))
Example with a torch.save
statement:
import torch
from huggingface_hub import Repository
model = torch.nn.Transformer()
repo = Repository("torch-files", clone_from="<user>/torch-files", use_auth_token=True)
with repo.commit("Adding my cool model!"):
torch.save(model.state_dict(), "model.pt")
Example with a Flax/JAX seralization statement
from flax import serialization
from jax import random
from flax import linen as nn
from huggingface_hub import Repository
model = nn.Dense(features=5)
key1, key2 = random.split(random.PRNGKey(0))
x = random.normal(key1, (10,))
params = model.init(key2, x)
bytes_output = serialization.to_bytes(params)
repo = Repository("flax-model", clone_from="<user>/flax-model", use_auth_token=True)
with repo.commit("Adding my cool Flax model!"):
with open("flax_model.msgpack", "wb") as f:
f.write(bytes_output)