Created
December 30, 2023 00:15
-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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