Spaces:
Sleeping
Sleeping
Update services/q
Browse files- services/q +59 -0
services/q
CHANGED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { uploadFile } from '@huggingface/hub';
|
| 2 |
+
import { RepoType } from '../types';
|
| 3 |
+
|
| 4 |
+
/**
|
| 5 |
+
* MOCK BACKEND SERVICE
|
| 6 |
+
*
|
| 7 |
+
* In a real-world scenario, this file would be a Node.js/Python server.
|
| 8 |
+
* Here, we simulate the "Backend" by encapsulating the credentials and logic
|
| 9 |
+
* so the Frontend (App.tsx) knows nothing about tokens or repository names.
|
| 10 |
+
*/
|
| 11 |
+
|
| 12 |
+
// HARDCODED SERVER CONFIGURATION
|
| 13 |
+
const SERVER_CONFIG = {
|
| 14 |
+
TOKEN: process.env.HF_TOKEN,
|
| 15 |
+
REPO: 'TwanAPI/DataTwan',
|
| 16 |
+
TYPE: 'dataset' as RepoType
|
| 17 |
+
};
|
| 18 |
+
|
| 19 |
+
interface UploadPayload {
|
| 20 |
+
file: File;
|
| 21 |
+
path: string;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
/**
|
| 25 |
+
* Uploads a file to the configured internal repository.
|
| 26 |
+
*
|
| 27 |
+
* @param payload File and destination path
|
| 28 |
+
* @returns The public URL of the uploaded file
|
| 29 |
+
*/
|
| 30 |
+
export const uploadFileToHub = async (payload: UploadPayload): Promise<string> => {
|
| 31 |
+
const { file, path } = payload;
|
| 32 |
+
const { TOKEN, REPO, TYPE } = SERVER_CONFIG;
|
| 33 |
+
|
| 34 |
+
console.log(`[BE] Received upload request for: ${path}`);
|
| 35 |
+
|
| 36 |
+
try {
|
| 37 |
+
const response = await uploadFile({
|
| 38 |
+
credentials: {
|
| 39 |
+
accessToken: TOKEN,
|
| 40 |
+
},
|
| 41 |
+
repo: {
|
| 42 |
+
type: TYPE,
|
| 43 |
+
name: REPO
|
| 44 |
+
},
|
| 45 |
+
file: {
|
| 46 |
+
path: path,
|
| 47 |
+
content: file,
|
| 48 |
+
},
|
| 49 |
+
});
|
| 50 |
+
|
| 51 |
+
const commitHash = response.commit.oid;
|
| 52 |
+
const urlPrefix = "https://huggingface.co/datasets";
|
| 53 |
+
return `${urlPrefix}/${REPO}/blob/${commitHash}/${path}`;
|
| 54 |
+
|
| 55 |
+
} catch (err: any) {
|
| 56 |
+
console.error("[BE] Upload Failed:", err);
|
| 57 |
+
throw new Error("Server Upload Error: " + (err.message || "Unknown error"));
|
| 58 |
+
}
|
| 59 |
+
};
|