Files
CloudFlare-ImgBed/functions/utils/huggingfaceAPI.js

245 lines
7.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Hugging Face Hub API 封装类
* 用于上传文件到 Hugging Face 仓库并获取文件
*
* 参考文档: https://huggingface.co/docs/huggingface_hub/guides/upload
*/
export class HuggingFaceAPI {
constructor(token, repo, isPrivate = false) {
this.token = token;
this.repo = repo; // 格式: username/repo-name
this.isPrivate = isPrivate;
this.baseURL = 'https://huggingface.co';
this.defaultHeaders = {
'Authorization': `Bearer ${this.token}`,
};
}
/**
* 检查仓库是否存在
* @returns {Promise<boolean>}
*/
async repoExists() {
try {
const response = await fetch(`${this.baseURL}/api/datasets/${this.repo}`, {
method: 'GET',
headers: this.defaultHeaders
});
console.log('Check repo exists:', this.repo, 'status:', response.status);
return response.ok;
} catch (error) {
console.error('Error checking repo existence:', error.message);
return false;
}
}
/**
* 创建仓库(如果不存在)
* @returns {Promise<boolean>}
*/
async createRepoIfNotExists() {
try {
const exists = await this.repoExists();
if (exists) {
console.log('Repo already exists:', this.repo);
return true;
}
console.log('Creating repo:', this.repo);
const response = await fetch(`${this.baseURL}/api/repos/create`, {
method: 'POST',
headers: {
...this.defaultHeaders,
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: this.repo.split('/')[1],
type: 'dataset',
private: this.isPrivate
})
});
if (!response.ok) {
const errorText = await response.text();
console.error('Create repo failed:', response.status, errorText);
// 如果是 409 冲突,说明仓库已存在
if (response.status === 409) {
return true;
}
throw new Error(`Failed to create repo: ${response.status} - ${errorText}`);
}
console.log(`Created HuggingFace repo: ${this.repo}`);
return true;
} catch (error) {
console.error('Error creating repo:', error.message);
return false;
}
}
/**
* 上传文件到仓库
* 使用 HuggingFace Hub 的 commit API
* @param {File|Blob} file - 要上传的文件
* @param {string} filePath - 存储路径(如 images/xxx.jpg
* @param {string} commitMessage - 提交信息
* @returns {Promise<Object>} 上传结果
*/
async uploadFile(file, filePath, commitMessage = 'Upload file') {
try {
// 确保仓库存在
const repoCreated = await this.createRepoIfNotExists();
if (!repoCreated) {
throw new Error('Failed to create or access repository');
}
// 获取文件内容
const arrayBuffer = await file.arrayBuffer();
const fileBytes = new Uint8Array(arrayBuffer);
const base64Content = this.uint8ArrayToBase64(fileBytes);
console.log('Uploading file:', filePath, 'size:', file.size, 'bytes');
// 使用 commit API 上传文件
// POST /api/datasets/{repo_id}/commit/{revision}
const commitUrl = `${this.baseURL}/api/datasets/${this.repo}/commit/main`;
const commitPayload = {
summary: commitMessage,
description: '',
files: [{
path: filePath,
encoding: 'base64',
content: base64Content
}]
};
console.log('Commit URL:', commitUrl);
console.log('Commit payload size:', JSON.stringify(commitPayload).length);
const response = await fetch(commitUrl, {
method: 'POST',
headers: {
...this.defaultHeaders,
'Content-Type': 'application/json'
},
body: JSON.stringify(commitPayload)
});
if (!response.ok) {
const errorText = await response.text();
console.error('Commit failed:', response.status, errorText);
throw new Error(`Commit failed: ${response.status} - ${errorText}`);
}
const result = await response.json();
console.log('Commit result:', JSON.stringify(result));
// 构建文件 URL
const fileUrl = `${this.baseURL}/datasets/${this.repo}/resolve/main/${filePath}`;
return {
success: true,
commitId: result.commitOid || result.commitId || result.oid,
filePath: filePath,
fileUrl: fileUrl,
fileSize: file.size
};
} catch (error) {
console.error('HuggingFace upload error:', error.message);
throw error;
}
}
/**
* 删除文件
* @param {string} filePath - 文件路径
* @param {string} commitMessage - 提交信息
* @returns {Promise<boolean>}
*/
async deleteFile(filePath, commitMessage = 'Delete file') {
try {
const commitUrl = `${this.baseURL}/api/datasets/${this.repo}/commit/main`;
const response = await fetch(commitUrl, {
method: 'POST',
headers: {
...this.defaultHeaders,
'Content-Type': 'application/json'
},
body: JSON.stringify({
summary: commitMessage,
deletedFiles: [filePath]
})
});
if (!response.ok) {
const errorText = await response.text();
console.error('HuggingFace delete error:', response.status, errorText);
return false;
}
return true;
} catch (error) {
console.error('Error deleting file from HuggingFace:', error.message);
return false;
}
}
/**
* 获取文件内容(用于私有仓库)
* @param {string} filePath - 文件路径
* @returns {Promise<Response>}
*/
async getFileContent(filePath) {
const fileUrl = `${this.baseURL}/datasets/${this.repo}/resolve/main/${filePath}`;
const response = await fetch(fileUrl, {
headers: this.isPrivate ? this.defaultHeaders : {}
});
return response;
}
/**
* 获取文件的公开访问 URL
* @param {string} filePath - 文件路径
* @returns {string}
*/
getFileURL(filePath) {
return `${this.baseURL}/datasets/${this.repo}/resolve/main/${filePath}`;
}
/**
* 检查文件是否存在
* @param {string} filePath - 文件路径
* @returns {Promise<boolean>}
*/
async fileExists(filePath) {
try {
const fileUrl = this.getFileURL(filePath);
const response = await fetch(fileUrl, {
method: 'HEAD',
headers: this.isPrivate ? this.defaultHeaders : {}
});
return response.ok;
} catch (error) {
return false;
}
}
/**
* Uint8Array 转 Base64
* @param {Uint8Array} bytes
* @returns {string}
*/
uint8ArrayToBase64(bytes) {
let binary = '';
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
}