| from huggingface_hub import HfApi |
| import os, sys, time |
|
|
| token_path = 'hf_token.txt' |
| if not os.path.exists(token_path): |
| print('hf_token.txt not found') |
| sys.exit(1) |
|
|
| token = open(token_path).read().strip() |
| api = HfApi() |
| user = api.whoami(token=token) |
| username = user.get('name') or (user.get('user') or {}).get('name') |
| print('username:', username) |
| repo_name = 'MyAwesomeModel-ForecastPlan' |
| full_repo = f"{username}/{repo_name}" |
|
|
| |
| try: |
| api.create_repo(repo_id=full_repo, token=token, exist_ok=True, repo_type='model') |
| print('repo created or exists') |
| except Exception as e: |
| print('create_repo error:', e) |
|
|
| root = os.getcwd() |
| uploaded = [] |
| failed = [] |
| start = time.time() |
| for dirpath, dirnames, filenames in os.walk(root): |
| |
| parts = dirpath.split(os.sep) |
| if '.git' in parts or '__pycache__' in parts: |
| continue |
| for fname in filenames: |
| if fname == 'hf_token.txt': |
| continue |
| fullpath = os.path.join(dirpath, fname) |
| relpath = os.path.relpath(fullpath, root).replace('\\', '/') |
| try: |
| with open(fullpath, 'rb') as f: |
| api.upload_file(path_or_fileobj=f, path_in_repo=relpath, repo_id=full_repo, token=token) |
| uploaded.append(relpath) |
| print('Uploaded', relpath) |
| except Exception as e: |
| failed.append((relpath, str(e))) |
| print('Failed', relpath, e) |
| |
| if time.time() - start > 100: |
| print('Reached time limit, stopping upload loop') |
| break |
|
|
| print('\nUpload summary:') |
| print('Uploaded count:', len(uploaded)) |
| print('Failed count:', len(failed)) |
| if failed: |
| for f,e in failed: |
| print('FAILED:', f, e) |
|
|