UNPKG

askui

Version:

Reliable, automated end-to-end-testing that depends on what is shown on your screen instead of the technology you are running on

140 lines (139 loc) 6.76 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import urljoin from 'url-join'; import { ControlCommand, InferenceResponse } from '../core/ui-control-commands'; import { Annotation } from '../core/annotation/annotation'; import { resizeBase64ImageWithSameRatio } from '../utils/transformations'; import { InferenceResponseError } from './inference-response-error'; import { ConfigurationError } from './config-error'; import { logger } from '../lib/logger'; export class InferenceClient { constructor(baseUrl, httpClient, resize, workspaceId, modelComposition, apiVersion = 'v1') { this.baseUrl = baseUrl; this.httpClient = httpClient; this.resize = resize; this.workspaceId = workspaceId; this.modelComposition = modelComposition; this.apiVersion = apiVersion; const versionedBaseUrl = urljoin(this.baseUrl, 'api', this.apiVersion); const url = workspaceId ? urljoin(versionedBaseUrl, 'workspaces', workspaceId) : versionedBaseUrl; this.urls = { actEndpoint: urljoin(url, 'act', 'inference'), inference: urljoin(url, 'inference'), isImageRequired: urljoin(url, 'instruction', 'is-image-required'), vqaInference: urljoin(url, 'vqa', 'inference'), }; this.httpClient.urlsToRetry = Object.values(this.urls); if (this.resize !== undefined && this.resize <= 0) { throw new ConfigurationError(`Resize must be a positive number. The current resize value "${this.resize}" is not valid.`); } this.resize = this.resize ? Math.ceil(this.resize) : this.resize; } isImageRequired(instruction) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.httpClient.post(this.urls.isImageRequired, { instruction, }); return response.body.isImageRequired; }); } // eslint-disable-next-line class-methods-use-this resizeIfNeeded(customElements, image) { return __awaiter(this, void 0, void 0, function* () { if (!image || customElements.length > 0 || this.resize === undefined) { return { base64Image: image, resizeRatio: 1 }; } return resizeBase64ImageWithSameRatio(image, this.resize); }); } inference() { return __awaiter(this, arguments, void 0, function* (customElements = [], image, instruction, modelComposition = []) { const resizedImage = yield this.resizeIfNeeded(customElements, image); const response = yield this.httpClient.post(this.urls.inference, this.urls.inference.includes('v4-experimental') ? { image: resizedImage.base64Image, instruction, tasks: ['OCR'], } : { customElements, image: resizedImage.base64Image, instruction, modelComposition: modelComposition.length > 0 ? modelComposition : this.modelComposition, }); InferenceClient.logMetaInformation(response.headers); return InferenceResponse.fromJson(response.body, resizedImage.resizeRatio, image); }); } vqaInference(image, prompt, config) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.httpClient.post(this.urls.vqaInference, { config, image, prompt, }); InferenceClient.logMetaInformation(response.headers); return response.body; }); } static logMetaInformation(headers) { if (headers['askui-usage-warnings'] !== undefined) { logger.warn(headers['askui-usage-warnings']); } } predictControlCommand(instruction_1, modelComposition_1) { return __awaiter(this, arguments, void 0, function* (instruction, modelComposition, customElements = [], image) { const inferenceResponse = yield this.inference(customElements, image, instruction, modelComposition); if (!(inferenceResponse instanceof ControlCommand)) { throw new InferenceResponseError('Internal Error. Can not execute command'); } return inferenceResponse; }); } getDetectedElements(instruction_1, image_1) { return __awaiter(this, arguments, void 0, function* (instruction, image, customElements = []) { const inferenceResponse = yield this.inference(customElements, image, instruction); if (!(inferenceResponse instanceof Annotation)) { throw new InferenceResponseError('Internal Error. Unable to get the detected elements'); } return inferenceResponse.detected_elements; }); } predictImageAnnotation(image_1) { return __awaiter(this, arguments, void 0, function* (image, customElements = []) { const inferenceResponse = yield this.inference(customElements, image); if (!(inferenceResponse instanceof Annotation)) { throw new InferenceResponseError('Internal Error. Can not execute annotation'); } return inferenceResponse; }); } predictVQAAnswer(prompt, image, config) { return __awaiter(this, void 0, void 0, function* () { const inferenceResponse = yield this.vqaInference(image, prompt, config); const { response } = inferenceResponse.data; try { return JSON.parse(response); } catch (error) { logger.warn(`Response is no valid JSON: ${response}`); } return response; }); } predictActResponse(params) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.httpClient.post(this.urls.actEndpoint, params); InferenceClient.logMetaInformation(response.headers); return response.body; }); } }