Fix: Update Hugging Face API to use commit endpoint instead of deprecated upload endpoint
Browse files- src/lib/huggingface.ts +22 -7
src/lib/huggingface.ts
CHANGED
|
@@ -15,7 +15,7 @@ export interface HuggingFaceFileResult {
|
|
| 15 |
const HF_API_BASE = 'https://huggingface.co/api';
|
| 16 |
|
| 17 |
/**
|
| 18 |
-
* Upload a file to Hugging Face Hub
|
| 19 |
*/
|
| 20 |
export async function uploadToHuggingFace(
|
| 21 |
file: Blob,
|
|
@@ -30,24 +30,39 @@ export async function uploadToHuggingFace(
|
|
| 30 |
throw new Error('Hugging Face credentials not configured. Set HF_TOKEN and HF_REPO_ID');
|
| 31 |
}
|
| 32 |
|
| 33 |
-
// Convert Blob to
|
| 34 |
const arrayBuffer = await file.arrayBuffer();
|
| 35 |
const buffer = Buffer.from(arrayBuffer);
|
|
|
|
| 36 |
|
| 37 |
// Determine file extension
|
| 38 |
const ext = fileName.split('.').pop() || 'bin';
|
| 39 |
const pathInRepo = `files/${fileId}.${ext}`;
|
| 40 |
|
| 41 |
-
//
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
method: 'POST',
|
| 46 |
headers: {
|
| 47 |
'Authorization': `Bearer ${token}`,
|
| 48 |
-
'Content-Type': 'application/
|
| 49 |
},
|
| 50 |
-
body:
|
| 51 |
});
|
| 52 |
|
| 53 |
if (!response.ok) {
|
|
|
|
| 15 |
const HF_API_BASE = 'https://huggingface.co/api';
|
| 16 |
|
| 17 |
/**
|
| 18 |
+
* Upload a file to Hugging Face Hub using the commit endpoint
|
| 19 |
*/
|
| 20 |
export async function uploadToHuggingFace(
|
| 21 |
file: Blob,
|
|
|
|
| 30 |
throw new Error('Hugging Face credentials not configured. Set HF_TOKEN and HF_REPO_ID');
|
| 31 |
}
|
| 32 |
|
| 33 |
+
// Convert Blob to base64 for the commit API
|
| 34 |
const arrayBuffer = await file.arrayBuffer();
|
| 35 |
const buffer = Buffer.from(arrayBuffer);
|
| 36 |
+
const base64Content = buffer.toString('base64');
|
| 37 |
|
| 38 |
// Determine file extension
|
| 39 |
const ext = fileName.split('.').pop() || 'bin';
|
| 40 |
const pathInRepo = `files/${fileId}.${ext}`;
|
| 41 |
|
| 42 |
+
// Use the commit endpoint (new API)
|
| 43 |
+
// Format: POST /api/{repo_type}s/{repo_id}/commit/{revision}
|
| 44 |
+
const repoTypePlural = repoType === 'dataset' ? 'datasets' : 'models';
|
| 45 |
+
const commitUrl = `${HF_API_BASE}/${repoTypePlural}/${repoId}/commit/main`;
|
| 46 |
|
| 47 |
+
// The commit API expects operations in a specific format
|
| 48 |
+
const commitData = {
|
| 49 |
+
operations: [
|
| 50 |
+
{
|
| 51 |
+
operation: 'add',
|
| 52 |
+
path: pathInRepo,
|
| 53 |
+
content: base64Content,
|
| 54 |
+
}
|
| 55 |
+
],
|
| 56 |
+
commit_message: `Upload ${fileName}`,
|
| 57 |
+
};
|
| 58 |
+
|
| 59 |
+
const response = await fetch(commitUrl, {
|
| 60 |
method: 'POST',
|
| 61 |
headers: {
|
| 62 |
'Authorization': `Bearer ${token}`,
|
| 63 |
+
'Content-Type': 'application/json',
|
| 64 |
},
|
| 65 |
+
body: JSON.stringify(commitData),
|
| 66 |
});
|
| 67 |
|
| 68 |
if (!response.ok) {
|