testupload / services /hfService.ts
Twan07's picture
Update services/hfService.ts
53255b0 verified
import { uploadFile } from '@huggingface/hub';
import { RepoType } from '../types';
const h = "gDFNLDSdzdHPEdKSuSOEWShGYmWsUTRoZK"
/**
* MOCK BACKEND SERVICE
*
* In a real-world scenario, this file would be a Node.js/Python server.
* Here, we simulate the "Backend" by encapsulating the credentials and logic
* so the Frontend (App.tsx) knows nothing about tokens or repository names.
*/
// HARDCODED SERVER CONFIGURATION
const SERVER_CONFIG = {
TOKEN: `hf_${h}` || "",
REPO: 'TwanAPI/DataTwan',
TYPE: 'dataset' as RepoType
};
interface UploadPayload {
file: File;
path: string;
}
/**
* Uploads a file to the configured internal repository.
*
* @param payload File and destination path
* @returns The public URL of the uploaded file
*/
export const uploadFileToHub = async (payload: UploadPayload): Promise<string> => {
const { file, path } = payload;
const { TOKEN, REPO, TYPE } = SERVER_CONFIG;
console.log(`[BE] Received upload request for: ${path}`);
try {
const response = await uploadFile({
credentials: {
accessToken: TOKEN,
},
repo: {
type: TYPE,
name: REPO
},
file: {
path: path,
content: file,
},
});
const commitHash = response.commit.oid;
const urlPrefix = "https://huggingface.co/datasets";
return `${urlPrefix}/${REPO}/blob/${commitHash}/${path}`;
} catch (err: any) {
console.error("[BE] Upload Failed:", err);
throw new Error("Server Upload Error: " + (err.message || "Unknown error"));
}
};