-
Notifications
You must be signed in to change notification settings - Fork 0
feat: search & datasets #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0badd79
feat: setup basic dataset versions
DaniAkash e5bced7
feat: setup concurrent annotation upload
DaniAkash 31d1827
feat: setup datasets base class
DaniAkash 3a192c5
feat: setup image classification dataset classes
DaniAkash ce8e305
feat: setup bulk upload utils
DaniAkash 4ddc80b
feat: setup bulkupload
DaniAkash 1daefde
feat: upload from folder method
DaniAkash 911ea52
feat: upload from csv
DaniAkash 1f3eb9a
fix: search test cases
DaniAkash bb12468
fix: lint issues
DaniAkash 07c1474
feat: added algorithm to search
DaniAkash dfa9655
fix: test cases
DaniAkash cc27f48
feat: setup tests for search
DaniAkash 4a3376e
chore: fix lint issues
DaniAkash b9b03fa
fix: failing tests
DaniAkash 7916a07
chore: upgrade grpc module
DaniAkash 2f732dc
chore: replace deprecated uuid package
DaniAkash fd1c824
fix: addressed review comments
DaniAkash a95725d
feat: use event emitter for bulk upload progress
DaniAkash 350171a
chore: fix lint issues
DaniAkash c93eef4
refactor: use array notation for polygon type
DaniAkash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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, | ||
| }); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.