-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupload.ts
297 lines (253 loc) · 7.93 KB
/
upload.ts
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Upload file to server using /files API
import * as core from '../core';
import { isAxiosError } from 'axios';
import fs from 'fs';
import fetch from 'node-fetch';
import * as path from 'path';
import progress from 'progress-stream';
import readline from 'readline';
import { asyncBufferFromFile, parquetMetadataAsync, parquetSchema, SchemaTree } from 'hyparquet';
export interface FileResponse {
id: string;
object: string;
type: 'jsonl' | 'parquet';
purpose: 'fine-tune';
filename: string;
bytes: number;
line_count: number;
processed: boolean;
}
export interface ErrorResponse {
message: string;
}
const failedUploadMessage = {
message: 'failed to upload file',
};
const baseURL = 'https://api.together.xyz/v1';
const MAX_FILE_SIZE = 4.8; // GB
const BYTES_PER_GB = 1024 * 1024 * 1024;
const MIN_SAMPLES = 1;
const PARQUET_EXPECTED_COLUMNS = ['input_ids', 'attention_mask', 'labels'];
export interface CheckFileResponse {
success: boolean;
message?: string;
}
export async function check_file(fileName: string): Promise<CheckFileResponse> {
const stat = fs.statSync(fileName);
if (stat.size == 0) {
return { success: false, message: `File is empty` };
}
if (stat.size > MAX_FILE_SIZE * BYTES_PER_GB) {
return { success: false, message: `File size exceeds the limit of ${MAX_FILE_SIZE} GB` };
}
const fileType = path.extname(fileName);
if (fileType !== '.jsonl' && fileType !== '.parquet') {
return {
success: false,
message: 'File type must be either .jsonl or .parquet',
};
}
if (fileType == '.jsonl') {
const jsonlCheck = await check_jsonl(fileName);
if (jsonlCheck) {
return { success: false, message: jsonlCheck };
}
}
if (fileType == '.parquet') {
const parquetCheck = await check_parquet(fileName);
if (parquetCheck) {
return { success: false, message: parquetCheck };
}
}
return { success: true };
}
export async function check_parquet(fileName: string): Promise<string | undefined> {
try {
const asyncBuffer = await asyncBufferFromFile(fileName);
const metadata = await parquetMetadataAsync(asyncBuffer);
const { children } = parquetSchema(metadata);
const fieldNames = children.map((child: SchemaTree) => child.element.name);
if (!fieldNames.includes('input_ids')) {
return `Parquet file ${fileName} does not contain the 'input_ids' column.`;
}
for (const fieldName of fieldNames) {
if (!PARQUET_EXPECTED_COLUMNS.includes(fieldName)) {
return `Parquet file ${fileName} contains unexpected column ${fieldName}. Only ${PARQUET_EXPECTED_COLUMNS.join(
', ',
)} are supported`;
}
}
const numRows = metadata.num_rows;
if (numRows < MIN_SAMPLES) {
return `Parquet file ${fileName} contains only ${numRows} samples. Minimum of ${MIN_SAMPLES} samples are required`;
}
} catch (err) {
return `failed to read parquet file ${fileName}`;
}
return undefined;
}
// return undefined if the file is valid, otherwise return an error message
export async function check_jsonl(fileName: string): Promise<string | undefined> {
const fileStream = fs.createReadStream(fileName);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
let errors: string[] = [];
let lineNumber = 1;
for await (const line of rl) {
try {
// do not proceed if there are too many errors
if (errors.length > 20) {
break;
}
const parsedLine = JSON.parse(line);
if (typeof parsedLine !== 'object') {
errors.push(`Line number ${lineNumber} is not a valid JSON object`);
continue;
}
if (!('text' in parsedLine)) {
errors.push(
`Missing 'text' field was found on line ${lineNumber} of the the input file. Expected format: {'text': 'my sample string'}.`,
);
continue;
}
if (typeof parsedLine['text'] !== 'string') {
errors.push(`'Invalid value type for "text" key on line ${lineNumber}. Expected string`);
continue;
}
} catch (error) {
errors.push(`Error parsing line number ${lineNumber}`);
}
lineNumber += 1;
}
lineNumber -= 1;
if (lineNumber < MIN_SAMPLES) {
errors.push(`Processing ${fileName} resulted in only ${lineNumber - 1} samples.`);
}
if (errors.length > 0) {
return errors.join('\n');
}
return undefined;
}
export async function upload(fileName: string, check: boolean = true): Promise<FileResponse | ErrorResponse> {
let purpose = 'fine-tune';
if (!fs.existsSync(fileName)) {
return {
message: 'File does not exists',
};
}
const fileType = path.extname(fileName);
if (fileType !== '.jsonl' && fileType !== '.parquet') {
return {
message: 'File type must be either .jsonl or .parquet',
};
}
if (check) {
const checkFile = await check_file(fileName);
if (!checkFile.success) {
return {
message: checkFile.message || `verification of ${fileName} failed with some unknown reason`,
};
}
}
// steps to do
// 1. check if file exists
// 2. get signed upload url
// 3. upload file
const baseUrl = core.readEnv('TOGETHER_API_BASE_URL') || 'https://api.together.ai/v1';
const apiKey = core.readEnv('TOGETHER_API_KEY');
if (!apiKey) {
return {
message: 'API key is required',
};
}
const getSigned = baseURL + '/files';
try {
const params = new URLSearchParams({
file_name: fileName,
purpose: purpose,
});
const fullUrl = `${getSigned}?${params}`;
const r = await fetch(fullUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Bearer ${apiKey}`,
},
redirect: 'manual',
body: params.toString(),
});
if (r.status !== 302) {
return failedUploadMessage;
}
const uploadUrl = r.headers.get('location') || '';
if (!uploadUrl || uploadUrl === '') {
return failedUploadMessage;
}
const fileId = r.headers.get('x-together-file-id') || '';
if (!fileId || fileId === '') {
return failedUploadMessage;
}
const fileStream = fs.createReadStream(fileName);
const fileSize = fs.statSync(fileName).size;
const progressStream = progress({
length: fileSize,
time: 100, // Emit progress events every 100ms
});
// Listen to progress events and log them
progressStream.on('progress', (progress) => {
displayProgress(progress.percentage);
});
let uploadedBytes = 0;
// upload the file to uploadUrl
const uploadResponse = await fetch(uploadUrl, {
method: 'PUT',
headers: {
'Content-Type': 'application/octet-stream',
},
body: fileStream.pipe(progressStream),
});
displayProgress(100);
process.stdout.write('\n');
return {
id: fileId,
object: 'file',
type: 'jsonl',
purpose: 'fine-tune',
filename: fileName,
bytes: fileSize,
line_count: 0,
processed: true,
};
} catch (error) {
if (isAxiosError(error)) {
// handle axios error here
if (error.status) {
return {
message: `failed to upload file with status ${error.status}`,
};
}
}
return {
message: 'failed to upload file',
};
}
}
async function displayProgress(progress: number) {
const barWidth = 40; // Number of characters for the progress bar
const completedBars = Math.round((progress / 100) * barWidth);
let remainingBars = barWidth - completedBars;
if (remainingBars < 0) {
remainingBars = 0;
}
const progressBar = `[${'='.repeat(completedBars)}${' '.repeat(remainingBars)}] ${progress.toFixed(2)}%`;
// Clear the line and write progress
//process.stdout.clearLine(0); //clean entire line
process.stdout.cursorTo(0);
process.stdout.write(progressBar, () => {});
await sleep(2000);
}
async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}