Vu Minh Chien commited on
Commit
fbf957b
Β·
1 Parent(s): 0ef3fee
Files changed (3) hide show
  1. hf-dataset.js +40 -22
  2. package-lock.json +5 -5
  3. package.json +2 -1
hf-dataset.js CHANGED
@@ -1,13 +1,16 @@
1
- // Using built-in fetch (available in Node.js 18+)
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
- // Upload to HF dataset using direct file upload
68
- const response = await fetch(`https://huggingface.co/datasets/${this.datasetId}/upload/main/${this.fileName}`, {
69
- method: 'PUT',
70
- headers: {
71
- 'Authorization': `Bearer ${this.hfToken}`,
72
- 'Content-Type': 'application/json'
 
 
 
73
  },
74
- body: jsonData
 
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('πŸ” Initializing dataset...');
98
 
99
- // Try to create initial empty devices file - this will auto-create the dataset if needed
100
- const success = await this.saveDevices(new Map());
101
-
102
- if (success) {
103
- console.log('βœ… Dataset initialized successfully');
104
  return true;
105
- } else {
106
- console.log('⚠️ Dataset initialization failed, will use fallback');
107
- return false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.16.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.16.0",
237
- "resolved": "https://registry.npmjs.org/@huggingface/hub/-/hub-0.16.0.tgz",
238
- "integrity": "sha512-4B/i2SLIvNTvUFF0MZlPOzWrtJ86RZ8kp4EN8RXgp7CAfSw4xQRWcYYvtjVwhp3VWPBhRpSMsLyL2aesG5uScQ==",
239
  "license": "MIT",
240
  "dependencies": {
241
- "@huggingface/tasks": "^0.12.8"
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"