magdyks's picture
Upload upload.py with huggingface_hub
36b73c7 verified
from huggingface_hub import HfApi
import os
from pathlib import Path
def upload_to_huggingface(token_path=None):
"""
Upload all files from current directory to Hugging Face repo magdyk/Qwen2-1.5B-Instruct-turbo-lora
Args:
token_path (str, optional): Path to file containing HF token. If None,
will look for token in env vars or ~/.huggingface
"""
# Initialize Hugging Face API
api = HfApi()
repo_id = "magdyks/Qwen2-1.5B-Instruct-turbo-lora"
# Get token either from file or environment
if token_path:
with open(token_path) as f:
token = f.read().strip()
else:
token = os.getenv("HF_TOKEN") # Will also check ~/.huggingface
if not token:
raise ValueError("No token found. Please provide token_path or set HF_TOKEN environment variable")
# Create repo if it doesn't exist (will raise error if repo exists)
try:
api.create_repo(repo_id, private=False, token=token)
print(f"Created new repository: {repo_id}")
except Exception as e:
print(f"Repository {repo_id} already exists or error occurred: {e}")
# Get all files in current directory
current_dir = Path(".")
files_to_upload = []
# Walk through directory and collect files
for path in current_dir.rglob("*"):
if path.is_file() and not path.name.startswith("."): # Skip hidden files
files_to_upload.append(str(path))
# Upload each file
for file_path in files_to_upload:
try:
api.upload_file(
path_or_fileobj=file_path,
path_in_repo=file_path,
repo_id=repo_id,
token=token
)
print(f"Uploaded: {file_path}")
except Exception as e:
print(f"Error uploading {file_path}: {e}")
print("Upload complete!")
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Upload files to Hugging Face')
parser.add_argument('--token-path', type=str, help='Path to file containing HF token')
args = parser.parse_args()
upload_to_huggingface(args.token_path)