-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.ts
220 lines (204 loc) · 6.51 KB
/
dataset.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
import {
DatasetVersion,
Dataset as GrpcDataset,
Input as GrpcInput,
} from "clarifai-nodejs-grpc/proto/clarifai/api/resources_pb";
import { UserError } from "../errors";
import { ClarifaiUrl, ClarifaiUrlHelper } from "../urls/helper";
import { AuthConfig } from "../utils/types";
import { Lister } from "./lister";
import { Input, InputBulkUpload } from "./input";
import {
DeleteDatasetVersionsRequest,
ListDatasetVersionsRequest,
PostDatasetVersionsRequest,
} from "clarifai-nodejs-grpc/proto/clarifai/api/service_pb";
import {
JavaScriptValue,
Struct,
} from "google-protobuf/google/protobuf/struct_pb";
import { promisifyGrpcCall } from "../utils/misc";
import { StatusCode } from "clarifai-nodejs-grpc/proto/clarifai/api/status/status_code_pb";
type DatasetConfig =
| {
authConfig?: AuthConfig;
datasetId: string;
datasetVersionId?: string;
url?: undefined;
}
| {
authConfig?: AuthConfig;
datasetId?: undefined;
datasetVersionId?: undefined;
url: ClarifaiUrl;
};
export class Dataset extends Lister {
private info: GrpcDataset = new GrpcDataset();
private batchSize: number = 128;
private input: Input;
constructor({ authConfig, datasetId, url, datasetVersionId }: DatasetConfig) {
if (url && datasetId) {
throw new UserError("You can only specify one of url or dataset_id.");
}
if (url) {
const [userId, appId, , _datasetId, _datasetVersionId] =
ClarifaiUrlHelper.splitClarifaiUrl(url);
if (authConfig) authConfig.userId = userId;
if (authConfig) authConfig.appId = appId;
datasetId = _datasetId;
datasetVersionId = _datasetVersionId;
}
super({ authConfig });
this.info.setId(datasetId!);
this.info.setVersion(new DatasetVersion().setId(datasetVersionId!));
this.input = new Input({ authConfig });
}
async createVersion({
id,
description,
metadata = {},
}: {
id: string;
description: string;
metadata?: Record<string, JavaScriptValue>;
}): Promise<DatasetVersion.AsObject> {
const request = new PostDatasetVersionsRequest();
request.setUserAppId(this.userAppId);
request.setDatasetId(this.info.getId());
const datasetVersion = new DatasetVersion();
datasetVersion.setId(id);
datasetVersion.setDescription(description);
datasetVersion.setMetadata(Struct.fromJavaScript(metadata));
request.setDatasetVersionsList([datasetVersion]);
const postDatasetVersions = promisifyGrpcCall(
this.STUB.client.postDatasetVersions,
this.STUB.client,
);
const response = await this.grpcRequest(postDatasetVersions, request);
const responseObject = response.toObject();
if (responseObject.status?.code !== StatusCode.SUCCESS) {
throw new Error(responseObject.status?.description);
}
console.info("\nDataset Version created\n%s", response.getStatus());
return responseObject.datasetVersionsList[0];
}
async deleteVersion(versionId: string): Promise<void> {
const request = new DeleteDatasetVersionsRequest();
request.setUserAppId(this.userAppId);
request.setDatasetId(this.info.getId());
request.setDatasetVersionIdsList([versionId]);
const deleteDatasetVersions = promisifyGrpcCall(
this.STUB.client.deleteDatasetVersions,
this.STUB.client,
);
const response = await this.grpcRequest(deleteDatasetVersions, request);
const responseObject = response.toObject();
if (responseObject.status?.code !== StatusCode.SUCCESS) {
throw new Error(responseObject.status?.description);
}
console.info("\nDataset Version Deleted\n%s", response.getStatus());
}
async *listVersions(
pageNo?: number,
perPage?: number,
): AsyncGenerator<DatasetVersion.AsObject[], void, unknown> {
const request = new ListDatasetVersionsRequest();
request.setUserAppId(this.userAppId);
request.setDatasetId(this.info.getId());
const listDatasetVersions = promisifyGrpcCall(
this.STUB.client.listDatasetVersions,
this.STUB.client,
);
const listDatasetVersionsGenerator = this.listPagesGenerator(
listDatasetVersions,
request,
pageNo,
perPage,
);
for await (const versions of listDatasetVersionsGenerator) {
yield versions.toObject().datasetVersionsList;
}
}
async uploadFromFolder({
folderPath,
inputType,
labels = false,
batchSize = this.batchSize,
uploadProgressEmitter,
}: {
folderPath: string;
inputType: "image" | "text";
labels: boolean;
batchSize?: number;
uploadProgressEmitter?: InputBulkUpload;
}): Promise<void> {
if (["image", "text"].indexOf(inputType) === -1) {
throw new UserError("Invalid input type");
}
let inputProtos: GrpcInput[] = [];
if (inputType === "image") {
inputProtos = Input.getImageInputsFromFolder({
folderPath: folderPath,
datasetId: this.info.getId(),
labels: labels,
});
}
if (inputType === "text") {
inputProtos = Input.getTextInputsFromFolder({
folderPath: folderPath,
datasetId: this.info.getId(),
labels: labels,
});
}
await this.input.bulkUpload({
inputs: inputProtos,
batchSize: batchSize,
uploadProgressEmitter,
});
}
async uploadFromCSV({
csvPath,
inputType = "text",
csvType,
labels = true,
batchSize = 128,
uploadProgressEmitter,
}: {
csvPath: string;
inputType?: "image" | "text" | "video" | "audio";
csvType: "raw" | "url" | "file";
labels?: boolean;
batchSize?: number;
uploadProgressEmitter?: InputBulkUpload;
}): Promise<void> {
if (!["image", "text", "video", "audio"].includes(inputType)) {
throw new UserError(
"Invalid input type, it should be image, text, audio, or video",
);
}
if (!["raw", "url", "file"].includes(csvType)) {
throw new UserError(
"Invalid csv type, it should be raw, url, or file_path",
);
}
if (!csvPath.endsWith(".csv")) {
throw new UserError("csvPath should be a csv file");
}
if (csvType === "raw" && inputType !== "text") {
throw new UserError("Only text input type is supported for raw csv type");
}
batchSize = Math.min(128, batchSize);
const inputProtos = await Input.getInputsFromCsv({
csvPath: csvPath,
inputType: inputType,
csvType: csvType,
datasetId: this.info.getId(),
labels: labels,
});
await this.input.bulkUpload({
inputs: inputProtos,
batchSize: batchSize,
uploadProgressEmitter,
});
}
}