MyAwesomeModel-ForecastPlan / .python_tmp /d2b91a85-2f2f-4618-89d3-05708518b6ae.py
FuryAssassin's picture
Upload .python_tmp/d2b91a85-2f2f-4618-89d3-05708518b6ae.py with huggingface_hub
bbf8a75 verified
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}"
# create repo
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):
# skip hidden .git and __pycache__ and hf_token
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)
# safety: stop if running too long
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)