Skip to content

Commit

Permalink
Removed functions code && supported functions plugin (#5979)
Browse files Browse the repository at this point in the history
  • Loading branch information
klakhov committed Nov 9, 2023
1 parent 118cc72 commit c87cb3b
Show file tree
Hide file tree
Showing 24 changed files with 167 additions and 815 deletions.
5 changes: 2 additions & 3 deletions cvat-core/src/api-implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export default function implementAPI(cvat) {
cvat.lambda.cancel.implementation = lambdaManager.cancel.bind(lambdaManager);
cvat.lambda.listen.implementation = lambdaManager.listen.bind(lambdaManager);
cvat.lambda.requests.implementation = lambdaManager.requests.bind(lambdaManager);
cvat.lambda.providers.implementation = lambdaManager.providers.bind(lambdaManager);

cvat.server.about.implementation = async () => {
const result = await serverProxy.server.about();
Expand Down Expand Up @@ -121,8 +120,8 @@ export default function implementAPI(cvat) {
return result;
};

cvat.server.request.implementation = async (url, data) => {
const result = await serverProxy.server.request(url, data);
cvat.server.request.implementation = async (url, data, requestConfig) => {
const result = await serverProxy.server.request(url, data, requestConfig);
return result;
};

Expand Down
8 changes: 2 additions & 6 deletions cvat-core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ function build() {
);
return result;
},
async request(url, data) {
const result = await PluginRegistry.apiWrapper(cvat.server.request, url, data);
async request(url, data, requestConfig) {
const result = await PluginRegistry.apiWrapper(cvat.server.request, url, data, requestConfig);
return result;
},
async setAuthData(response) {
Expand Down Expand Up @@ -203,10 +203,6 @@ function build() {
const result = await PluginRegistry.apiWrapper(cvat.lambda.requests);
return result;
},
async providers() {
const result = await PluginRegistry.apiWrapper(cvat.lambda.providers);
return result;
},
},
logger: loggerStorage,
config: {
Expand Down
1 change: 0 additions & 1 deletion cvat-core/src/core-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export interface SerializedModel {
return_type?: ModelReturnType;
owner?: any;
provider?: string;
api_key?: string;
url?: string;
help_message?: string;
animated_gif?: string;
Expand Down
61 changes: 18 additions & 43 deletions cvat-core/src/lambda-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,41 @@
import serverProxy from './server-proxy';
import { ArgumentError } from './exceptions';
import MLModel from './ml-model';
import { ModelProviders, RQStatus } from './enums';
import { RQStatus } from './enums';

export interface ModelProvider {
name: string;
icon: string;
attributes: Record<string, string>;
}

interface ModelProxy {
run: (body: any) => Promise<any>;
call: (modelID: string | number, body: any) => Promise<any>;
status: (requestID: string) => Promise<any>;
cancel: (requestID: string) => Promise<any>;
}

class LambdaManager {
private cachedList: MLModel[];
private listening: Record<number, {
onUpdate: ((status: RQStatus, progress: number, message?: string) => void)[];
functionID: string;
timeout: number | null;
}>;
private cachedList: any;

constructor() {
this.listening = {};
this.cachedList = null;
this.cachedList = [];
}

async list(): Promise<{ models: MLModel[], count: number }> {
const lambdaFunctions = await serverProxy.lambda.list();

const functionsResult = await serverProxy.functions.list();
const { results: functions, count: functionsCount } = functionsResult;

const result = [...lambdaFunctions, ...functions];
const models = result.map((serialzedModel) => new MLModel({ ...serialzedModel }));
const models = [];
for (const model of lambdaFunctions) {
models.push(
new MLModel({
...model,
}),
);
}

this.cachedList = models;
return { models, count: lambdaFunctions.length + functionsCount };
return { models, count: lambdaFunctions.length };
}

async run(taskID: number, model: MLModel, args: any) {
Expand All @@ -68,7 +64,7 @@ class LambdaManager {
function: model.id,
};

const result = await LambdaManager.getModelProxy(model).run(body);
const result = await serverProxy.lambda.run(body);
return result.id;
}

Expand All @@ -81,16 +77,14 @@ class LambdaManager {
...args,
task: taskID,
};

const result = await LambdaManager.getModelProxy(model).call(model.id, body);
const result = await serverProxy.lambda.call(model.id, body);
return result;
}

async requests() {
const lambdaRequests = await serverProxy.lambda.requests();
const functionsRequests = await serverProxy.functions.requests();
const result = [...lambdaRequests, ...functionsRequests];
return result.filter((request) => ['queued', 'started'].includes(request.status));
return lambdaRequests
.filter((request) => [RQStatus.QUEUED, RQStatus.STARTED].includes(request.status));
}

async cancel(requestID, functionID): Promise<void> {
Expand All @@ -107,7 +101,7 @@ class LambdaManager {
delete this.listening[requestID];
}

await LambdaManager.getModelProxy(model).cancel(requestID);
await serverProxy.lambda.cancel(requestID);
}

async listen(
Expand All @@ -125,9 +119,8 @@ class LambdaManager {
// already listening, avoid sending extra requests
return;
}

const timeoutCallback = (): void => {
LambdaManager.getModelProxy(model).status(requestID).then((response) => {
serverProxy.lambda.status(requestID).then((response) => {
const { status } = response;
if (requestID in this.listening) {
// check it was not cancelled
Expand Down Expand Up @@ -171,24 +164,6 @@ class LambdaManager {
timeout: window.setTimeout(timeoutCallback),
};
}

async providers(): Promise<ModelProvider[]> {
const providersData: Record<string, Record<string, string>> = await serverProxy.functions.providers();
const providers = Object.entries(providersData).map(([provider, attributes]) => {
const { icon } = attributes;
delete attributes.icon;
return {
name: provider,
icon,
attributes,
};
});
return providers;
}

private static getModelProxy(model: MLModel): ModelProxy {
return model.provider === ModelProviders.CVAT ? serverProxy.lambda : serverProxy.functions;
}
}

export default new LambdaManager();
48 changes: 2 additions & 46 deletions cvat-core/src/ml-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
//
// SPDX-License-Identifier: MIT

import serverProxy from './server-proxy';
import PluginRegistry from './plugins';
import { decodePreview } from './frames';
import {
ModelProviders, ModelKind, ModelReturnType,
} from './enums';
Expand Down Expand Up @@ -105,60 +103,18 @@ export default class MLModel {
this.changeToolsBlockerStateCallback = onChangeToolsBlockerState;
}

public async save(): Promise<MLModel> {
const result = await PluginRegistry.apiWrapper.call(this, MLModel.prototype.save);
return result;
}

public async delete(): Promise<MLModel> {
const result = await PluginRegistry.apiWrapper.call(this, MLModel.prototype.delete);
return result;
}

public async preview(): Promise<string> {
const result = await PluginRegistry.apiWrapper.call(this, MLModel.prototype.preview);
return result;
}
}

Object.defineProperties(MLModel.prototype.save, {
implementation: {
writable: false,
enumerable: false,
value: async function implementation(this: MLModel): Promise<MLModel> {
const modelData = {
provider: this.provider,
url: this.serialized.url,
api_key: this.serialized.api_key,
};

const model = await serverProxy.functions.create(modelData);
return new MLModel(model);
},
},
});

Object.defineProperties(MLModel.prototype.delete, {
implementation: {
writable: false,
enumerable: false,
value: async function implementation(this: MLModel): Promise<void> {
if (this.isDeletable) {
await serverProxy.functions.delete(this.id as number);
}
},
},
});

Object.defineProperties(MLModel.prototype.preview, {
implementation: {
writable: false,
enumerable: false,
value: async function implementation(this: MLModel): Promise<string> {
if (this.provider === ModelProviders.CVAT) return '';
const preview = await serverProxy.functions.getPreview(this.id);
if (!preview) return '';
return decodePreview(preview);
value: async function implementation(): Promise<string | null> {
return null;
},
},
});
Loading

0 comments on commit c87cb3b

Please sign in to comment.