Upload .python_tmp/d2b91a85-2f2f-4618-89d3-05708518b6ae.py with huggingface_hub
Browse files
.python_tmp/d2b91a85-2f2f-4618-89d3-05708518b6ae.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import HfApi
|
| 2 |
+
import os, sys, time
|
| 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 |
+
user = api.whoami(token=token)
|
| 12 |
+
username = user.get('name') or (user.get('user') or {}).get('name')
|
| 13 |
+
print('username:', username)
|
| 14 |
+
repo_name = 'MyAwesomeModel-ForecastPlan'
|
| 15 |
+
full_repo = f"{username}/{repo_name}"
|
| 16 |
+
|
| 17 |
+
# create repo
|
| 18 |
+
try:
|
| 19 |
+
api.create_repo(repo_id=full_repo, token=token, exist_ok=True, repo_type='model')
|
| 20 |
+
print('repo created or exists')
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print('create_repo error:', e)
|
| 23 |
+
|
| 24 |
+
root = os.getcwd()
|
| 25 |
+
uploaded = []
|
| 26 |
+
failed = []
|
| 27 |
+
start = time.time()
|
| 28 |
+
for dirpath, dirnames, filenames in os.walk(root):
|
| 29 |
+
# skip hidden .git and __pycache__ and hf_token
|
| 30 |
+
parts = dirpath.split(os.sep)
|
| 31 |
+
if '.git' in parts or '__pycache__' in parts:
|
| 32 |
+
continue
|
| 33 |
+
for fname in filenames:
|
| 34 |
+
if fname == 'hf_token.txt':
|
| 35 |
+
continue
|
| 36 |
+
fullpath = os.path.join(dirpath, fname)
|
| 37 |
+
relpath = os.path.relpath(fullpath, root).replace('\\', '/')
|
| 38 |
+
try:
|
| 39 |
+
with open(fullpath, 'rb') as f:
|
| 40 |
+
api.upload_file(path_or_fileobj=f, path_in_repo=relpath, repo_id=full_repo, token=token)
|
| 41 |
+
uploaded.append(relpath)
|
| 42 |
+
print('Uploaded', relpath)
|
| 43 |
+
except Exception as e:
|
| 44 |
+
failed.append((relpath, str(e)))
|
| 45 |
+
print('Failed', relpath, e)
|
| 46 |
+
# safety: stop if running too long
|
| 47 |
+
if time.time() - start > 100:
|
| 48 |
+
print('Reached time limit, stopping upload loop')
|
| 49 |
+
break
|
| 50 |
+
|
| 51 |
+
print('\nUpload summary:')
|
| 52 |
+
print('Uploaded count:', len(uploaded))
|
| 53 |
+
print('Failed count:', len(failed))
|
| 54 |
+
if failed:
|
| 55 |
+
for f,e in failed:
|
| 56 |
+
print('FAILED:', f, e)
|