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

Files changed (2) hide show
  1. hf-dataset.js +23 -68
  2. 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.hfApi = new HfApi({ accessToken: this.hfToken });
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.hfApi.uploadFile({
70
- repo: this.datasetId,
71
- file: {
72
- path: this.fileName,
73
- content: jsonData
74
  },
75
- commitMessage: `Update devices data - ${new Date().toISOString()}`,
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('πŸ” Checking if dataset exists...');
96
 
97
- // Try to get dataset info
98
- const datasetInfo = await this.hfApi.datasetInfo({
99
- repo: this.datasetId
100
- });
101
-
102
- console.log('βœ… Dataset already exists');
103
- return true;
104
 
105
- } catch (error) {
106
- if (error.statusCode === 404) {
107
- console.log('πŸ“ Dataset not found, creating new one...');
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.error('❌ Error checking dataset:', error);
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"