File size: 8,108 Bytes
60d7455
 
 
 
 
 
 
 
 
 
a2c64e7
 
a25386c
a2c64e7
 
 
 
a25386c
a2c64e7
 
 
 
a25386c
 
a2c64e7
 
 
a25386c
 
a2c64e7
 
 
 
 
 
 
 
 
 
 
 
a25386c
a2c64e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25386c
 
 
 
a2c64e7
 
 
 
 
 
 
 
 
 
7ad9382
 
a2c64e7
 
 
 
7ad9382
 
a2c64e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25386c
 
a2c64e7
a25386c
 
 
 
 
 
 
 
 
 
 
 
 
 
a2c64e7
a25386c
 
 
 
 
 
 
 
a2c64e7
 
a25386c
 
7ad9382
 
 
 
 
 
 
 
 
a25386c
 
7ad9382
 
 
 
 
 
 
a25386c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2c64e7
 
 
 
 
 
 
 
 
 
 
a25386c
a2c64e7
a25386c
 
 
a2c64e7
 
 
 
 
 
 
 
a25386c
 
 
a2c64e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25386c
 
a2c64e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25386c
 
 
a2c64e7
a25386c
 
 
 
 
 
 
 
a2c64e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a25386c
a2c64e7
 
 
a25386c
 
 
 
 
 
 
a2c64e7
a25386c
a2c64e7
 
 
a25386c
a2c64e7
a25386c
 
a2c64e7
 
 
 
 
 
a25386c
 
a2c64e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7008b6
 
a25386c
 
 
 
a2c64e7
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
---
title: fe_03f1
emoji: πŸ˜‚
colorFrom: blue
colorTo: gray
sdk: docker
app_port: 3000
pinned: false
---

# Vico - Hugging Face File Upload API

API Python Flask untuk upload file ke Hugging Face Dataset dengan response format JSON. Menggunakan library resmi `huggingface_hub`.

## πŸš€ Fitur

- βœ… Upload file ke Hugging Face Dataset via REST API
- βœ… Menggunakan library resmi `huggingface_hub`
- βœ… Validasi file dan error handling
- βœ… Response JSON terstruktur
- βœ… Support subfolder dalam repository
- βœ… File size limit 100MB
- βœ… Environment variables support (HF_TOKEN, HF_REPO_ID)
- βœ… Automatic temporary file cleanup

## πŸ“‹ Prerequisites

- Python 3.8 atau lebih tinggi
- pip (Python package manager)
- Hugging Face account dengan access token

## πŸ”§ Instalasi

### 1. Clone Repository
```bash
git clone https://github.com/4cko/vico.git
cd vico
```

### 2. Install Dependencies
```bash
pip install -r requirements.txt
```

### 3. Setup Environment Variables
```bash
cp .env.example .env
# Edit .env sesuai kebutuhan (opsional)
```

### 4. Dapatkan Hugging Face Token
1. Buka https://huggingface.co/settings/tokens
2. Buat token baru dengan akses 'write'
3. Simpan token tersebut

## 🎯 Cara Menggunakan

### 1. Jalankan Server
```bash
# Production mode
python3 server.py

# Development mode (dengan auto-reload)
python3 -m flask run --reload
```

Server akan berjalan di `http://localhost:3000`

### 2. Upload File

**Endpoint:** `POST /api/upload`

**Required Fields:**
- `file` - File yang akan di-upload (form-data)
- `repo_id` - Hugging Face repo ID (format: `username/dataset-name`) *atau set di .env*
- `token` - Hugging Face API token *atau set di .env*

**Optional Fields:**
- `subfolder` - Subfolder dalam repository (default: root)

**Catatan:** Jika `repo_id` dan `token` sudah di-set di `.env` (HF_REPO_ID dan HF_TOKEN), Anda tidak perlu mengirimnya di request. Nilai dari request akan override nilai di .env.

#### Contoh menggunakan cURL:
```bash
curl -X POST http://localhost:3000/api/upload \
  -F "file=@/path/to/file.csv" \
  -F "repo_id=username/my-dataset" \
  -F "token=hf_xxxxxxxxxxxxxxxxxxxxxxx" \
  -F "subfolder=data"
```

#### Contoh menggunakan Python:
```python
import requests

files = {
    'file': open('/path/to/file.csv', 'rb')
}
data = {
    'repo_id': 'username/my-dataset',
    'token': 'hf_xxxxxxxxxxxxxxxxxxxxxxx',
    'subfolder': 'data'
}

response = requests.post(
    'http://localhost:3000/api/upload',
    files=files,
    data=data
)

print(response.json())
```

#### Direct Upload dengan huggingface_hub
Anda juga bisa upload langsung tanpa API server:

```python
from huggingface_hub import login, upload_file, upload_folder

# Authenticate
login(token='hf_your_token_here')

# Upload single file
upload_file(
    path_or_fileobj='/path/to/file.csv',
    path_in_repo='u/file.csv',
    repo_id='username/my-dataset',
    repo_type='dataset',
    commit_message='Upload data'
)

# Atau upload entire folder
upload_folder(
    folder_path='/path/to/folder',
    repo_id='username/my-dataset',
    repo_type='dataset',
    path_in_repo='u/folder',
    commit_message='Upload folder'
)
```

#### Dengan Environment Variables (.env)
Jika sudah set `.env` dengan `HF_TOKEN` dan `HF_REPO_ID`:

**cURL:**
```bash
curl -X POST http://localhost:3000/api/upload \
  -F "file=@/path/to/file.csv"
```

**Python:**
```python
import requests

files = {'file': open('file.csv', 'rb')}
# repo_id dan token akan diambil dari .env

r = requests.post('http://localhost:3000/api/upload', files=files)
print(r.json())
```

#### Contoh menggunakan huggingface_hub (Direct Upload)
```python
from huggingface_hub import login, upload_file

# Authenticate
login(token='hf_your_token_here')

# Upload file
upload_file(
    path_or_fileobj='/path/to/file.csv',
    path_in_repo='u/file.csv',
    repo_id='username/my-dataset',
    repo_type='dataset',
    commit_message='Upload data'
)

print('βœ… File uploaded!')
```

### 3. Response Format

#### βœ… Sukses Upload (Status 200):
```json
{
  "success": true,
  "message": "File berhasil di-upload ke Hugging Face",
  "data": {
    "file_name": "data.csv",
    "file_size": 12345,
    "repo_id": "username/my-dataset",
    "path_prefix": "/u/",
    "subfolder": "data",
    "url": "https://huggingface.co/datasets/username/my-dataset/blob/main/u/data/data.csv",
    "file_path": "u/data/data.csv",
    "upload_timestamp": "2024-06-24T10:30:00Z"
  }
}
```

#### ❌ Error Response (Status 400/500):
```json
{
  "success": false,
  "error": "repo_id dan token diperlukan (kirim via request atau set di .env)",
  "required_fields": ["repo_id", "token"],
  "hint": "Set HF_REPO_ID dan HF_TOKEN di .env atau kirim via form data"
}
```

```json
{
  "success": false,
  "error": "Token tidak valid atau expired"
}
```

```json
{
  "success": false,
  "error": "Repository tidak ditemukan: invalid/repo"
}
```

## πŸ“ API Documentation

### GET /
Health check endpoint

**Response:**
```json
{
  "status": "ok",
  "message": "Hugging Face File Upload API",
  "version": "1.0.0",
  "language": "Python",
  "framework": "Flask",
  "endpoints": {
    "upload": {
      "method": "POST",
      "path": "/api/upload",
      "description": "Upload file ke Hugging Face dataset",
      "required": ["file", "repo_id", "token"],
      "optional": ["subfolder"]
    }
  }
}
```

## πŸ” Security Notes

1. **Token Protection:**
   - Jangan commit `.env` file ke repository
   - Gunakan environment variables untuk production
   - Rotate token secara berkala

2. **File Validation:**
   - Max file size: 100MB
   - File validation dilakukan pada server
   - Automatic cleanup file lokal setelah upload

3. **Error Handling:**
   - Token tidak valid
   - Repository tidak ditemukan
   - Access denied
   - Network errors

## πŸ“‚ Project Structure

```
vico/
β”œβ”€β”€ server.py              # Main Flask server
β”œβ”€β”€ hf_uploader.py         # Hugging Face upload utility
β”œβ”€β”€ requirements.txt       # Python dependencies
β”œβ”€β”€ .env.example           # Environment variables template
β”œβ”€β”€ README.md              # Dokumentasi
β”œβ”€β”€ examples/
β”‚   β”œβ”€β”€ upload-file.py             # Contoh upload via API
β”‚   β”œβ”€β”€ direct-hf-upload.py        # Contoh direct upload ke HF
β”‚   β”œβ”€β”€ curl-upload.sh             # Contoh cURL
β”‚   β”œβ”€β”€ env-upload.sh              # Contoh menggunakan .env
β”‚   └── sample-data.csv            # Sample data file
└── uploads/               # Temporary folder untuk uploaded files
```

## πŸ› Troubleshooting

### Token tidak valid
```
Error: Token tidak valid atau expired
```
**Solusi:** Check token di https://huggingface.co/settings/tokens

### Repository tidak ditemukan
```
Error: Repository tidak ditemukan: username/dataset
```
**Solusi:** 
- Pastikan dataset sudah di-create di Hugging Face
- Format: `username/dataset-name` (lowercase)

### File terlalu besar
```
Error: File too large
```
**Solusi:** Max file size adalah 100MB. Ubah di `server.py` jika perlu

### Connection timeout
```
Error: Connection timeout
```
**Solusi:** Untuk file besar, process mungkin memerlukan waktu lebih lama. Tunggu lebih lama atau tingkatkan timeout.

### Module not found
```
ModuleNotFoundError: No module named 'flask'
```
**Solusi:** Install dependencies dengan `pip install -r requirements.txt`

## πŸš€ Deployment

### Menggunakan Gunicorn (Production)
```bash
pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:3000 server:app
```

### Menggunakan 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
```

## πŸ“„ License

MIT

## πŸ‘€ Author

4cko

## πŸ’¬ Support

Untuk masalah atau pertanyaan, buka issue di repository ini.

---

**Catatan:** API ini menggunakan library resmi Hugging Face Hub. Pastikan token memiliki akses 'write' untuk repository yang dituju.

Dokumentasi library: https://huggingface.co/docs/hub/security-tokens

**Catatan:** API ini menggunakan Hugging Face Hub API. Pastikan token memiliki akses 'write' untuk repository yang dituju.