-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathhuggingFace.js
More file actions
185 lines (164 loc) · 7.35 KB
/
Copy pathhuggingFace.js
File metadata and controls
185 lines (164 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// uploadFile
const { deleteFiles, createRepo, deleteRepo, listSpaces, uploadFilesWithProgress } = require("@huggingface/hub");
var fs = require('fs').promises;
var path = require('path');
const axios = require('axios');
class HuggingFace {
constructor(accessToken) {
this.credentials = { accessToken };
}
// repoFullName = 'username/repoName'
// directoryPath = Folder containing docker training service. Default: Autotrain-advanced.
// isPrivate = Default: false
// hardware - Default: 't4-medium'
// secrets = A list of env variables the trainer will recieve. eg: { key: 'key1', value: 'val1'}], Default: []
async createOrUpdateSpace(repoFullName, directoryPath = false, isPrivate = false, hardware = 't4-medium', secrets = []) {
try {
const username = repoFullName.split('/')[0];
let existingSpaces = await this.listSpaces(username);
console.log('Spaces check: ', { existingSpaces });
let spaceExists = existingSpaces.some(space => space.name === repoFullName);
if (spaceExists) {
console.log('Deleting: ', { repoFullName });
await this.deleteSpace(repoFullName);
}
existingSpaces = await this.listSpaces(username);
spaceExists = existingSpaces.some(space => space.name === repoFullName);
if (!spaceExists) {
var files = await this.getFiles(directoryPath || path.join(__dirname, 'train/image/') );
console.log('Creating: ', repoFullName, 'With Files: ', {files})
await createRepo({
repo: { name: repoFullName, type: "space" },
credentials: this.credentials,
private: isPrivate,
sdk: "docker", // Required for space
hardware: hardware,
files,
});
console.log(`Space '${repoFullName}' created.`);
this.upgradeSpace(repoFullName, hardware);
const defaults = {
"PROJECT_NAME": 'my_llm',
"MODEL_NAME": 'abhishek/llama-2-7b-hf-small-shards',
"PUSH_TO_HUB": false,
"HF_TOKEN": this.credentials.accessToken,
"REPO_ID": repoFullName
};
secrets.forEach(secret => {
// Override default value if key exists in secrets
if (defaults.hasOwnProperty(secret.key)) {
defaults[secret.key] = secret.value;
}
// Set the secret, regardless of whether it was in defaults or not
this.setSecret(repoFullName, secret.key, secret.value);
});
}
} catch (error) {
console.error('Error in createOrUpdateSpace:', error);
throw error;
}
}
async setSecret(repoFullName, secretKey, secretValue) {
const endpoint = 'https://huggingface.co';
const headers = { 'Authorization': `Bearer ${this.credentials.accessToken}`};
const payload = {
key: secretKey,
value: secretValue,
};
try {
const response = await axios.post(`${endpoint}/api/spaces/${repoFullName}/secrets`, payload, { headers });
console.log('secret request response:', response.data);
} catch (error) {
console.error('Error requesting new secret:', error.response ? error.response.data : error);
}
}
async upgradeSpace(repoFullName, hardware) {
const endpoint = 'https://huggingface.co';
const headers = { 'Authorization': `Bearer ${this.credentials.accessToken}`};
const payload = {
flavor: hardware
};
try {
const response = await axios.post(`${endpoint}/api/spaces/${repoFullName}/hardware`, payload, { headers });
console.log('Hardware request response:', response.data);
} catch (error) {
console.error('Error requesting new hardware:', error.response ? error.response.data : error);
}
}
async getFiles(directoryPath, rootPath = directoryPath) {
console.log('directory path: ', {directoryPath})
const entries = await fs.readdir(directoryPath, { withFileTypes: true });
const files = await Promise.all(entries.map((entry) => {
const fullPath = path.join(directoryPath, entry.name);
let relativePath = path.relative(rootPath, fullPath);
relativePath = relativePath.replace(/\\/g, '/'); // linux path
if (entry.isDirectory()) {
return this.getFiles(fullPath, rootPath);
} else {
return fs.readFile(fullPath).then((content) => ({
path: relativePath,
content: new Uint8Array(content),
}));
}
}));
// Flatten the array and remove any possible empty slots
return Array.prototype.concat(...files);
}
async uploadDirectory(repoFullName, directoryPath) {
// Read all files in the directory
var filenames = await fs.readdir(directoryPath);
// Map each filename to a File object
var files = await Promise.all(filenames.map(async function(filename) {
var filePath = path.join(directoryPath, filename);
var content = await fs.readFile(filePath);
return { path: filePath, content };
}));
// Call uploadFilesWithProgress with necessary parameters
for await (var event of uploadFilesWithProgress({
credentials: this.credentials,
repo: repoFullName,
files,
// commitTitle: `Add ${files.length} files`,
// hubUrl: '', // Your hubUrl here
// branch: '', // Your branch here
// isPullRequest: false, // Or true if it's a PR
// parentCommit: '', // Your parentCommit here
// useWebWorkers: false, // Or true if you want to use web workers
})) {
console.log('Upload progress:', event);
}
console.log('Upload completed');
}
async listSpaces(owner) {
try {
// console.log('Listing spaces for owner:', owner);
const spaces = [];
for await (const space of listSpaces({ search: { owner:owner }, credentials: this.credentials })) {
// console.log('Found space:', space.id);
spaces.push(space);
}
return spaces;
} catch (error) {
console.error('Error listing spaces:', error);
throw error;
}
}
async deleteSpace(repoName) {
try {
const deleteParams = {
repo: { name: repoName, type: "space" },
credentials: this.credentials
};
// console.log('Deleting space:', repoName);
await deleteRepo(deleteParams);
} catch (error) {
console.error('Error deleting space:', error);
throw error;
}
}
}
module.exports = HuggingFace;
//
// api.request_space_hardware(repo_id=TRAINING_SPACE_ID, hardware=SpaceHardware.CPU_BASIC)
// else: api.request_space_hardware(repo_id=TRAINING_SPACE_ID, hardware=SpaceHardware.T4_MEDIUM)
//