Skip to content

Instantly share code, notes, and snippets.

@fakerybakery
Created December 30, 2023 00:15
Show Gist options
  • Save fakerybakery/8f81ac19be4e76a37225fa8339ba3814 to your computer and use it in GitHub Desktop.
Save fakerybakery/8f81ac19be4e76a37225fa8339ba3814 to your computer and use it in GitHub Desktop.
Hugging Face Hub: Upload in pure Python, with only the Requests & JSON libraries!
FILE_CONTENTS = 'File Contents'
REPO_TYPE = 'dataset'
REPO_ID = 'username/repo'
HF_TOKEN = 'hf_***'
import requests
import json
import base64
x = [
json.dumps(
{
"key": "header",
"value": {
"summary": "Commit Message",
"description": "Commit Description"
}
}
).encode(),
json.dumps(
{
"key": "file",
"value": {
"content": base64.b64encode(FILE_CONTENTS.encode()).decode(),
"path": "text.txt",
"encoding": "base64"
}
}
).encode(),
]
items = b"\n".join(x)
print(requests.post(
url=f"https://huggingface.co/api/{REPO_TYPE}s/{REPO_ID}/commit/main",
headers={
"Content-Type": "application/x-ndjson",
"authorization": f"Bearer {HF_TOKEN}",
"user-agent": "",
},
data=items,
params=None,
).text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment