ge / examples /python-requests.py
4cko
/usr/local/rvm/gems/ruby-3.4.7/bin:/usr/local/rvm/gems/ruby-3.4.7@global/bin:/usr/local/rvm/rubies/ruby-3.4.7/bin:/home/codespace/.vscode-remote/data/User/globalStorage/github.copilot-chat/debugCommand:/home/codespace/.vscode-remote/data/User/globalStorage/github.copilot-chat/copilotCli:/vscode/bin/linux-x64/7e7950df89d055b5a378379db9ee14290772148a/bin/remote-cli:/home/codespace/.local/bin:/home/codespace/.dotnet:/home/codespace/nvm/current/bin:/home/codespace/.php/current/bin:/home/codespace/.python/current/bin:/home/codespace/java/current/bin:/home/codespace/.ruby/current/bin:/home/codespace/.local/bin:/usr/local/python/current/bin:/usr/local/py-utils/bin:/usr/local/jupyter:/usr/local/oryx:/usr/local/go/bin:/go/bin:/usr/local/sdkman/bin:/usr/local/sdkman/candidates/java/current/bin:/usr/local/sdkman/candidates/gradle/current/bin:/usr/local/sdkman/candidates/maven/current/bin:/usr/local/sdkman/candidates/ant/current/bin:/usr/local/rvm/gems/default/bin:/usr/local/rvm/gems/default@global/bin:/usr/local/rvm/rubies/default/bin:/usr/local/share/rbenv/bin:/usr/local/php/current/bin:/opt/conda/bin:/usr/local/nvs:/usr/local/share/nvm/versions/node/v24.14.0/bin:/usr/local/hugo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/share/dotnet:/home/codespace/.dotnet/tools:/usr/local/rvm/bin
a25386c
Raw
History Blame Contribute Delete
1.98 kB
#!/usr/bin/env python3
"""
Contoh: Upload file menggunakan Python + requests
Cara jalankan:
python3 examples/python-requests.py
"""
import requests
import os
import sys
from pathlib import Path
# Konfigurasi
API_URL = 'http://localhost:3000/api/upload'
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'
def upload_file():
"""Upload file ke API"""
# 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:
# Setup request
with open(FILE_PATH, 'rb') as f:
files = {'file': f}
data = {
'repo_id': REPO_ID,
'token': HF_TOKEN,
'subfolder': 'datasets'
}
print('πŸ“€ Uploading file...')
response = requests.post(
API_URL,
files=files,
data=data,
timeout=30
)
# Check response
if response.status_code == 200:
result = response.json()
print('βœ… Upload berhasil!')
print('πŸ“Š Response:')
import json
print(json.dumps(result, indent=2))
return result
else:
print(f'❌ Error {response.status_code}:')
print(response.json())
sys.exit(1)
except requests.exceptions.ConnectionError:
print('❌ Tidak dapat connect ke API')
print(f' Pastikan server berjalan di {API_URL}')
sys.exit(1)
except Exception as e:
print(f'❌ Error: {str(e)}')
sys.exit(1)
if __name__ == '__main__':
upload_file()