MyAwesomeModel-ForecastPlan / .python_tmp /2edd8fcd-1ba3-4aa5-8735-f1c16db69425.py
FuryAssassin's picture
Upload .python_tmp/2edd8fcd-1ba3-4aa5-8735-f1c16db69425.py with huggingface_hub
98c02cc verified
from huggingface_hub import HfApi
import os, sys
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()
# get username
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}"
print('full_repo:', full_repo)
# create repo
try:
api.create_repo(repo_id=full_repo, token=token, exist_ok=True, repo_type='model')
print('repo created or exists')
except TypeError:
# fallback to older signature
try:
api.create_repo(repo_name, token=token, exist_ok=True, repo_type='model')
print('repo created (fallback)')
except Exception as e:
print('create_repo fallback error', e)
except Exception as e:
print('create_repo error:', e)
# Upload small set of important files only to avoid timeout
files_to_upload = ['README.md', 'LICENSE']
count = 0
for fname in files_to_upload:
if not os.path.exists(fname):
continue
try:
with open(fname, 'rb') as f:
api.upload_file(path_or_fileobj=f, path_in_repo=fname, repo_id=full_repo, token=token)
count += 1
except Exception as e:
print('Failed to upload', fname, 'error:', e)
print('Done. Uploaded files:', count)