ge / PYTHON_MIGRATION.md
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
4.25 kB
# 🐍 Python Conversion Summary
Vico telah dikonversi dari Node.js menjadi Python dengan menggunakan library resmi `huggingface_hub`.
## ✨ Perubahan Utama
### File Python Baru
- **`server.py`** - Flask API server (menggantikan server.js)
- **`hf_uploader.py`** - Utility upload dengan `huggingface_hub` library
- **`requirements.txt`** - Dependencies Python
- **`examples/upload-file.py`** - Contoh upload via API (Python)
- **`examples/direct-hf-upload.py`** - Contoh direct upload ke HF
### Fitur Baru
βœ… Menggunakan library resmi `huggingface_hub`
βœ… Support direct upload dengan `upload_file()` dan `upload_folder()`
βœ… Better error handling
βœ… Support upload folder (tidak hanya file)
βœ… Path structure: `/u/` untuk semua file
## πŸš€ Quick Start
### 1. Install Dependencies
```bash
pip install -r requirements.txt
```
### 2. Setup Environment
```bash
cp .env.example .env
# Edit .env dan set HF_TOKEN dan HF_REPO_ID
```
### 3. Run Server
```bash
python3 server.py
```
### 4. Upload File
**Via API:**
```bash
curl -X POST http://localhost:3000/api/upload \
-F "file=@data.csv"
```
**Via Python (Direct):**
```python
from huggingface_hub import login, upload_file
login(token='hf_...')
upload_file(
path_or_fileobj='data.csv',
path_in_repo='u/data.csv',
repo_id='username/dataset',
repo_type='dataset'
)
```
## πŸ“ API Endpoints
### POST /api/upload
Upload single file ke Hugging Face dataset
**Params:**
- `file` - File untuk di-upload
- `repo_id` - Target dataset (atau dari .env)
- `token` - HF token (atau dari .env)
- `subfolder` - Optional subfolder
**Response:**
```json
{
"success": true,
"data": {
"url": "https://huggingface.co/datasets/.../blob/main/u/...",
"file_path": "u/...",
"file_size": 12345,
"upload_timestamp": "2024-06-24T..."
}
}
```
## πŸ”„ Migration Guide
### Dari Node.js (Old)
```javascript
// Old: Node.js
npm start
```
### Ke Python (New)
```bash
# New: Python
python3 server.py
```
## πŸ“š Library: huggingface_hub
Dokumentasi: https://huggingface.co/docs/hub/security-tokens
### Contoh Penggunaan
```python
from huggingface_hub import login, upload_file, upload_folder
# Authenticate
login(token='hf_...')
# Upload single file
upload_file(
path_or_fileobj='path/to/file.csv',
path_in_repo='u/file.csv',
repo_id='username/dataset-name',
repo_type='dataset',
commit_message='Upload data'
)
# Upload entire folder
upload_folder(
folder_path='path/to/folder',
repo_id='username/dataset-name',
repo_type='dataset',
path_in_repo='u/folder',
commit_message='Upload folder'
)
```
## πŸ”‘ Environment Variables
Setup di `.env`:
```env
HF_TOKEN=hf_xxxxxxxxxxxxxxxxxxxxxxx
HF_REPO_ID=username/dataset-name
PORT=3000
NODE_ENV=development
```
## πŸ“¦ Dependencies
```
Flask==2.3.3 # Web framework
python-dotenv==1.0.0 # Environment variables
huggingface-hub==0.16.4 # Official HF library
requests==2.31.0 # HTTP client
```
## πŸ§ͺ Testing
### Test server health
```bash
curl http://localhost:3000
```
### Test upload (dengan .env set)
```bash
python3 examples/upload-file.py
```
### Test direct upload
```bash
python3 examples/direct-hf-upload.py
```
## βœ… Checklist
- [x] Server berjalan di port 3000
- [x] API endpoint `/api/upload` working
- [x] Environment variables support
- [x] Response format JSON
- [x] Error handling
- [x] Direct upload support
- [x] Folder upload support
- [x] Docker support
## πŸ“– File Reference
| File | Purpose |
|------|---------|
| `server.py` | Flask web server |
| `hf_uploader.py` | Upload utility dengan HF library |
| `requirements.txt` | Python dependencies |
| `examples/upload-file.py` | Upload via API |
| `examples/direct-hf-upload.py` | Direct upload |
| `Dockerfile` | Docker image |
## πŸš€ Production Deployment
### Dengan Gunicorn
```bash
pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:3000 server:app
```
### Dengan Docker
```bash
docker build -t vico-api .
docker run -p 3000:3000 \
-e HF_TOKEN=your_token \
-e HF_REPO_ID=username/dataset \
vico-api
```
---
**Note:** Old Node.js files (server.js, package.json) masih ada untuk referensi tapi tidak lagi digunakan.
Gunakan Python files untuk development baru.