Upload .python_tmp/2edd8fcd-1ba3-4aa5-8735-f1c16db69425.py with huggingface_hub
Browse files
.python_tmp/2edd8fcd-1ba3-4aa5-8735-f1c16db69425.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import HfApi
|
| 2 |
+
import os, sys
|
| 3 |
+
|
| 4 |
+
token_path = 'hf_token.txt'
|
| 5 |
+
if not os.path.exists(token_path):
|
| 6 |
+
print('hf_token.txt not found')
|
| 7 |
+
sys.exit(1)
|
| 8 |
+
|
| 9 |
+
token = open(token_path).read().strip()
|
| 10 |
+
api = HfApi()
|
| 11 |
+
# get username
|
| 12 |
+
user = api.whoami(token=token)
|
| 13 |
+
username = user.get('name') or (user.get('user') or {}).get('name')
|
| 14 |
+
print('username:', username)
|
| 15 |
+
repo_name = 'MyAwesomeModel-ForecastPlan'
|
| 16 |
+
full_repo = f"{username}/{repo_name}"
|
| 17 |
+
print('full_repo:', full_repo)
|
| 18 |
+
|
| 19 |
+
# create repo
|
| 20 |
+
try:
|
| 21 |
+
api.create_repo(repo_id=full_repo, token=token, exist_ok=True, repo_type='model')
|
| 22 |
+
print('repo created or exists')
|
| 23 |
+
except TypeError:
|
| 24 |
+
# fallback to older signature
|
| 25 |
+
try:
|
| 26 |
+
api.create_repo(repo_name, token=token, exist_ok=True, repo_type='model')
|
| 27 |
+
print('repo created (fallback)')
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print('create_repo fallback error', e)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print('create_repo error:', e)
|
| 32 |
+
|
| 33 |
+
# Upload small set of important files only to avoid timeout
|
| 34 |
+
files_to_upload = ['README.md', 'LICENSE']
|
| 35 |
+
count = 0
|
| 36 |
+
for fname in files_to_upload:
|
| 37 |
+
if not os.path.exists(fname):
|
| 38 |
+
continue
|
| 39 |
+
try:
|
| 40 |
+
with open(fname, 'rb') as f:
|
| 41 |
+
api.upload_file(path_or_fileobj=f, path_in_repo=fname, repo_id=full_repo, token=token)
|
| 42 |
+
count += 1
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print('Failed to upload', fname, 'error:', e)
|
| 45 |
+
|
| 46 |
+
print('Done. Uploaded files:', count)
|