dragxd commited on
Commit
f4c0fa3
·
1 Parent(s): 3cdbd12

Fix: Update Hugging Face API to use commit endpoint instead of deprecated upload endpoint

Browse files
Files changed (1) hide show
  1. 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 Buffer
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
- // Upload file using HF Hub API
42
- const uploadUrl = `${HF_API_BASE}/${repoType === 'dataset' ? 'datasets' : 'models'}/${repoId}/upload/${pathInRepo}`;
 
 
43
 
44
- const response = await fetch(uploadUrl, {
 
 
 
 
 
 
 
 
 
 
 
 
45
  method: 'POST',
46
  headers: {
47
  'Authorization': `Bearer ${token}`,
48
- 'Content-Type': 'application/octet-stream',
49
  },
50
- body: buffer,
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) {