Spaces:
Sleeping
Sleeping
Vu Minh Chien
commited on
Commit
Β·
fbf957b
1
Parent(s):
0ef3fee
hfapi
Browse files- hf-dataset.js +40 -22
- package-lock.json +5 -5
- package.json +2 -1
hf-dataset.js
CHANGED
@@ -1,13 +1,16 @@
|
|
1 |
-
|
2 |
|
3 |
class HFDatasetManager {
|
4 |
constructor() {
|
5 |
this.hfToken = process.env.HF_TOKEN;
|
6 |
this.datasetId = process.env.HF_DATASET_ID || "Detomo/houzou-devices";
|
7 |
this.fileName = "devices.json";
|
8 |
-
this.baseUrl = "https://huggingface.co/api/v1";
|
9 |
this.isEnabled = !!this.hfToken;
|
10 |
|
|
|
|
|
|
|
|
|
11 |
console.log(`π€ HF Dataset Manager initialized`);
|
12 |
console.log(` Dataset: ${this.datasetId}`);
|
13 |
console.log(` Enabled: ${this.isEnabled}`);
|
@@ -64,19 +67,19 @@ class HFDatasetManager {
|
|
64 |
const devicesArray = Array.from(deviceMap.entries());
|
65 |
const jsonData = JSON.stringify(devicesArray, null, 2);
|
66 |
|
67 |
-
//
|
68 |
-
const
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
73 |
},
|
74 |
-
|
|
|
75 |
});
|
76 |
-
|
77 |
-
if (!response.ok) {
|
78 |
-
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
79 |
-
}
|
80 |
|
81 |
console.log(`β
Successfully saved ${deviceMap.size} devices to HF dataset`);
|
82 |
return true;
|
@@ -94,21 +97,36 @@ class HFDatasetManager {
|
|
94 |
}
|
95 |
|
96 |
try {
|
97 |
-
console.log('π
|
98 |
|
99 |
-
// Try to
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
console.log('β
Dataset initialized successfully');
|
104 |
return true;
|
105 |
-
}
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
}
|
109 |
|
110 |
} catch (error) {
|
111 |
console.error('β Error initializing dataset:', error);
|
|
|
112 |
return false;
|
113 |
}
|
114 |
}
|
|
|
1 |
+
const { HfApi } = require('@huggingface/hub');
|
2 |
|
3 |
class HFDatasetManager {
|
4 |
constructor() {
|
5 |
this.hfToken = process.env.HF_TOKEN;
|
6 |
this.datasetId = process.env.HF_DATASET_ID || "Detomo/houzou-devices";
|
7 |
this.fileName = "devices.json";
|
|
|
8 |
this.isEnabled = !!this.hfToken;
|
9 |
|
10 |
+
if (this.isEnabled) {
|
11 |
+
this.hfApi = new HfApi({ accessToken: this.hfToken });
|
12 |
+
}
|
13 |
+
|
14 |
console.log(`π€ HF Dataset Manager initialized`);
|
15 |
console.log(` Dataset: ${this.datasetId}`);
|
16 |
console.log(` Enabled: ${this.isEnabled}`);
|
|
|
67 |
const devicesArray = Array.from(deviceMap.entries());
|
68 |
const jsonData = JSON.stringify(devicesArray, null, 2);
|
69 |
|
70 |
+
// Create a buffer from the JSON data
|
71 |
+
const fileContent = Buffer.from(jsonData, 'utf8');
|
72 |
+
|
73 |
+
// Upload using HfApi.uploadFile
|
74 |
+
await this.hfApi.uploadFile({
|
75 |
+
repo: this.datasetId,
|
76 |
+
file: {
|
77 |
+
path: this.fileName,
|
78 |
+
content: fileContent
|
79 |
},
|
80 |
+
repoType: 'dataset',
|
81 |
+
commitMessage: `Update devices data (${deviceMap.size} devices)`
|
82 |
});
|
|
|
|
|
|
|
|
|
83 |
|
84 |
console.log(`β
Successfully saved ${deviceMap.size} devices to HF dataset`);
|
85 |
return true;
|
|
|
97 |
}
|
98 |
|
99 |
try {
|
100 |
+
console.log('π Checking if dataset exists...');
|
101 |
|
102 |
+
// Try to check if dataset exists
|
103 |
+
try {
|
104 |
+
await this.hfApi.repoInfo({ repo: this.datasetId, repoType: 'dataset' });
|
105 |
+
console.log('β
Dataset already exists');
|
|
|
106 |
return true;
|
107 |
+
} catch (error) {
|
108 |
+
if (error.message.includes('404')) {
|
109 |
+
console.log('π Dataset not found, creating new one...');
|
110 |
+
|
111 |
+
// Create new dataset
|
112 |
+
await this.hfApi.createRepo({
|
113 |
+
repo: this.datasetId,
|
114 |
+
repoType: 'dataset',
|
115 |
+
description: 'Device tokens for Houzou Medical App notifications'
|
116 |
+
});
|
117 |
+
|
118 |
+
console.log('β
Dataset created successfully');
|
119 |
+
|
120 |
+
// Create initial empty devices file
|
121 |
+
await this.saveDevices(new Map());
|
122 |
+
return true;
|
123 |
+
}
|
124 |
+
throw error;
|
125 |
}
|
126 |
|
127 |
} catch (error) {
|
128 |
console.error('β Error initializing dataset:', error);
|
129 |
+
console.log('β οΈ Dataset initialization failed, will use fallback');
|
130 |
return false;
|
131 |
}
|
132 |
}
|
package-lock.json
CHANGED
@@ -9,7 +9,7 @@
|
|
9 |
"version": "1.0.0",
|
10 |
"license": "MIT",
|
11 |
"dependencies": {
|
12 |
-
"@huggingface/hub": "^0.
|
13 |
"body-parser": "^1.20.2",
|
14 |
"cors": "^2.8.5",
|
15 |
"dotenv": "^16.3.1",
|
@@ -233,12 +233,12 @@
|
|
233 |
}
|
234 |
},
|
235 |
"node_modules/@huggingface/hub": {
|
236 |
-
"version": "0.
|
237 |
-
"resolved": "https://registry.npmjs.org/@huggingface/hub/-/hub-0.
|
238 |
-
"integrity": "sha512-
|
239 |
"license": "MIT",
|
240 |
"dependencies": {
|
241 |
-
"@huggingface/tasks": "^0.12.
|
242 |
},
|
243 |
"engines": {
|
244 |
"node": ">=18"
|
|
|
9 |
"version": "1.0.0",
|
10 |
"license": "MIT",
|
11 |
"dependencies": {
|
12 |
+
"@huggingface/hub": "^0.19.0",
|
13 |
"body-parser": "^1.20.2",
|
14 |
"cors": "^2.8.5",
|
15 |
"dotenv": "^16.3.1",
|
|
|
233 |
}
|
234 |
},
|
235 |
"node_modules/@huggingface/hub": {
|
236 |
+
"version": "0.19.0",
|
237 |
+
"resolved": "https://registry.npmjs.org/@huggingface/hub/-/hub-0.19.0.tgz",
|
238 |
+
"integrity": "sha512-5f0POHxsQzi1RrtGmk5I+PuSXQnWx4c7WU6+JofZcdrb5mT5frV01MpGS41HRPwoQm2ZWjBfzkRpAnqBNCw2Hg==",
|
239 |
"license": "MIT",
|
240 |
"dependencies": {
|
241 |
+
"@huggingface/tasks": "^0.12.28"
|
242 |
},
|
243 |
"engines": {
|
244 |
"node": ">=18"
|
package.json
CHANGED
@@ -12,7 +12,8 @@
|
|
12 |
"firebase-admin": "^12.0.0",
|
13 |
"cors": "^2.8.5",
|
14 |
"body-parser": "^1.20.2",
|
15 |
-
"dotenv": "^16.3.1"
|
|
|
16 |
},
|
17 |
"devDependencies": {
|
18 |
"nodemon": "^3.0.2"
|
|
|
12 |
"firebase-admin": "^12.0.0",
|
13 |
"cors": "^2.8.5",
|
14 |
"body-parser": "^1.20.2",
|
15 |
+
"dotenv": "^16.3.1",
|
16 |
+
"@huggingface/hub": "^0.19.0"
|
17 |
},
|
18 |
"devDependencies": {
|
19 |
"nodemon": "^3.0.2"
|