File size: 2,942 Bytes
a25386c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #!/usr/bin/env python3
"""
Contoh: Upload file langsung ke Hugging Face menggunakan huggingface_hub
Cara jalankan:
python3 examples/direct-hf-upload.py
"""
import os
import sys
from pathlib import Path
from huggingface_hub import login, upload_file, upload_folder
# Konfigurasi
HF_TOKEN = os.getenv('HF_TOKEN', 'your_hf_token_here')
REPO_ID = os.getenv('HF_REPO_ID', 'username/dataset-name')
FILE_PATH = './examples/sample-data.csv'
FOLDER_PATH = './examples'
def upload_single_file():
"""Upload single file"""
print('π Direct Upload - Single File')
print('=' * 50)
# Validasi file
if not Path(FILE_PATH).exists():
print(f'β File tidak ditemukan: {FILE_PATH}')
print('π Membuat file contoh...')
with open(FILE_PATH, 'w') as f:
f.write('id,name,value\n')
f.write('1,Alice,100\n')
f.write('2,Bob,200\n')
try:
# Authenticate
print('π Authenticating...')
login(token=HF_TOKEN)
# Upload file
print(f'π€ Uploading {FILE_PATH}...')
info = upload_file(
path_or_fileobj=FILE_PATH,
path_in_repo=f'u/{Path(FILE_PATH).name}',
repo_id=REPO_ID,
repo_type='dataset',
commit_message='Upload sample data'
)
print('β
Upload berhasil!')
print(f'π URL: https://huggingface.co/datasets/{REPO_ID}/blob/main/u/{Path(FILE_PATH).name}')
print(f'π Commit: {info}')
except Exception as e:
print(f'β Error: {str(e)}')
sys.exit(1)
def upload_folder_contents():
"""Upload entire folder"""
print('\nπ Direct Upload - Folder')
print('=' * 50)
if not Path(FOLDER_PATH).exists():
print(f'β Folder tidak ditemukan: {FOLDER_PATH}')
sys.exit(1)
try:
# Authenticate
print('π Authenticating...')
login(token=HF_TOKEN)
# Upload folder
print(f'π€ Uploading folder {FOLDER_PATH}...')
info = upload_folder(
folder_path=FOLDER_PATH,
repo_id=REPO_ID,
repo_type='dataset',
path_in_repo='u/examples',
commit_message='Upload examples folder'
)
print('β
Upload berhasil!')
print(f'π URL: https://huggingface.co/datasets/{REPO_ID}/tree/main/u/examples')
print(f'π Commit: {info}')
except Exception as e:
print(f'β Error: {str(e)}')
sys.exit(1)
if __name__ == '__main__':
print('π Contoh Upload Langsung ke Hugging Face')
print('=' * 50)
print()
if len(sys.argv) > 1 and sys.argv[1] == 'folder':
upload_folder_contents()
else:
upload_single_file()
print('\nπ‘ Tip: Jalankan dengan "folder" untuk upload folder')
print(' python3 examples/direct-hf-upload.py folder')
|