Vu Minh Chien
commited on
Commit
Β·
0ef3fee
1
Parent(s):
53b2688
Replace HfApi with direct fetch API calls
Browse files- Remove @huggingface/hub dependency completely
- Use direct fetch calls to HF API endpoints
- Simplify dataset creation logic
- Use built-in Node.js fetch instead of external libraries
- hf-dataset.js +23 -68
- package.json +1 -2
hf-dataset.js
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
const { HfApi } = require("@huggingface/hub");
|
2 |
// Using built-in fetch (available in Node.js 18+)
|
3 |
|
4 |
class HFDatasetManager {
|
@@ -6,7 +5,7 @@ class HFDatasetManager {
|
|
6 |
this.hfToken = process.env.HF_TOKEN;
|
7 |
this.datasetId = process.env.HF_DATASET_ID || "Detomo/houzou-devices";
|
8 |
this.fileName = "devices.json";
|
9 |
-
this.
|
10 |
this.isEnabled = !!this.hfToken;
|
11 |
|
12 |
console.log(`π€ HF Dataset Manager initialized`);
|
@@ -65,16 +64,19 @@ class HFDatasetManager {
|
|
65 |
const devicesArray = Array.from(deviceMap.entries());
|
66 |
const jsonData = JSON.stringify(devicesArray, null, 2);
|
67 |
|
68 |
-
// Upload to HF dataset
|
69 |
-
await this.
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
},
|
75 |
-
|
76 |
-
repoType: "dataset"
|
77 |
});
|
|
|
|
|
|
|
|
|
78 |
|
79 |
console.log(`β
Successfully saved ${deviceMap.size} devices to HF dataset`);
|
80 |
return true;
|
@@ -92,69 +94,22 @@ class HFDatasetManager {
|
|
92 |
}
|
93 |
|
94 |
try {
|
95 |
-
console.log('π
|
96 |
|
97 |
-
// Try to
|
98 |
-
const
|
99 |
-
repo: this.datasetId
|
100 |
-
});
|
101 |
-
|
102 |
-
console.log('β
Dataset already exists');
|
103 |
-
return true;
|
104 |
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
try {
|
110 |
-
// Create the dataset
|
111 |
-
await this.hfApi.createRepo({
|
112 |
-
repo: this.datasetId,
|
113 |
-
type: "dataset",
|
114 |
-
private: false
|
115 |
-
});
|
116 |
-
|
117 |
-
console.log('β
Dataset created successfully');
|
118 |
-
|
119 |
-
// Create initial README
|
120 |
-
const readmeContent = `# Houzou Medical Devices Dataset
|
121 |
-
|
122 |
-
This dataset stores FCM tokens and device information for the Houzou Medical app notification system.
|
123 |
-
|
124 |
-
## Files
|
125 |
-
|
126 |
-
- \`devices.json\`: Contains device tokens and metadata
|
127 |
-
|
128 |
-
## Usage
|
129 |
-
|
130 |
-
This dataset is automatically managed by the Houzou Medical Notification Server.
|
131 |
-
|
132 |
-
Last updated: ${new Date().toISOString()}
|
133 |
-
`;
|
134 |
-
|
135 |
-
await this.hfApi.uploadFile({
|
136 |
-
repo: this.datasetId,
|
137 |
-
file: {
|
138 |
-
path: "README.md",
|
139 |
-
content: readmeContent
|
140 |
-
},
|
141 |
-
commitMessage: "Initial dataset setup",
|
142 |
-
repoType: "dataset"
|
143 |
-
});
|
144 |
-
|
145 |
-
// Create initial empty devices file
|
146 |
-
await this.saveDevices(new Map());
|
147 |
-
|
148 |
-
return true;
|
149 |
-
|
150 |
-
} catch (createError) {
|
151 |
-
console.error('β Error creating dataset:', createError);
|
152 |
-
return false;
|
153 |
-
}
|
154 |
} else {
|
155 |
-
console.
|
156 |
return false;
|
157 |
}
|
|
|
|
|
|
|
|
|
158 |
}
|
159 |
}
|
160 |
|
|
|
|
|
1 |
// Using built-in fetch (available in Node.js 18+)
|
2 |
|
3 |
class HFDatasetManager {
|
|
|
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`);
|
|
|
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 |
}
|
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 |
}
|
115 |
|
package.json
CHANGED
@@ -12,8 +12,7 @@
|
|
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.16.0"
|
17 |
},
|
18 |
"devDependencies": {
|
19 |
"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 |
},
|
17 |
"devDependencies": {
|
18 |
"nodemon": "^3.0.2"
|