| from huggingface_hub import login, HfApi |
| import json |
| import argparse |
| import os |
|
|
| def upload_json_to_hf(token, repo_id, file_path, file_name): |
| |
| login(token) |
| |
| |
| api = HfApi() |
| |
| |
| try: |
| api.upload_file( |
| path_or_fileobj=file_path, |
| path_in_repo=file_name, |
| repo_id=repo_id, |
| repo_type="dataset" |
| ) |
| print(f"Successfully uploaded {file_name} to {repo_id}") |
| except Exception as e: |
| print(f"Error uploading file: {str(e)}") |
| raise |
|
|
| def main(): |
| parser = argparse.ArgumentParser(description='Upload JSON file to Hugging Face') |
| |
| |
| parser.add_argument( |
| '--token', |
| type=str, |
| help='Hugging Face access token (or set HUGGINGFACE_TOKEN env variable)', |
| default=os.getenv('HUGGINGFACE_TOKEN') |
| ) |
| |
| parser.add_argument( |
| '--repo-id', |
| type=str, |
| required=True, |
| help='Repository ID (format: username/repo-name)' |
| ) |
| |
| parser.add_argument( |
| '--file-path', |
| type=str, |
| required=True, |
| help='Path to the JSON file to upload' |
| ) |
| |
| parser.add_argument( |
| '--file-name', |
| type=str, |
| help='Name to save the file as in the repository (defaults to the input filename)', |
| ) |
|
|
| |
| args = parser.parse_args() |
|
|
| |
| if not args.token: |
| raise ValueError("Please provide a token either via --token or HUGGINGFACE_TOKEN environment variable") |
|
|
| |
| if not args.file_name: |
| args.file_name = os.path.basename(args.file_path) |
|
|
| |
| if not os.path.exists(args.file_path): |
| raise FileNotFoundError(f"File not found: {args.file_path}") |
| |
| try: |
| with open(args.file_path, 'r') as f: |
| json.load(f) |
| except json.JSONDecodeError: |
| raise ValueError(f"File is not valid JSON: {args.file_path}") |
|
|
| |
| upload_json_to_hf(args.token, args.repo_id, args.file_path, args.file_name) |
|
|
| if __name__ == "__main__": |
| main() |