UNPKG

openvino-genai-node

Version:

OpenVINO™ GenAI pipelines for using from Node.js environment

92 lines 3.72 kB
// Copyright (C) 2023-2026 Intel Corporation // SPDX-License-Identifier: Apache-2.0 var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import util from "node:util"; import { Image2ImagePipeline as Image2ImagePipelineWrapper } from "../addon.js"; /** * Pipeline for image-to-image generation. * * Generates image tensor(s) for a single prompt conditioned on an input image. * Use the factory method Image2ImagePipeline() to create and * initialize an instance. */ export class Image2ImagePipeline { constructor(modelPath, device, properties = {}) { this.pipeline = null; this.modelPath = modelPath; this.device = device; this.properties = properties; } /** * Load the pipeline. Must be called once before generate(). */ async init() { const pipeline = new Image2ImagePipelineWrapper(); const initPromise = util.promisify(pipeline.init.bind(pipeline)); await initPromise(this.modelPath, this.device, this.properties); this.pipeline = pipeline; } /** * Generate image tensor(s) for a single prompt conditioned on the input image. * * The input `image` must be a `u8` Tensor with batched NHWC shape `[1, H, W, 3]` * (single image, RGB channels-last). Unbatched `[H, W, 3]` tensors are rejected. * * Batch image generation is supported through `generationConfig.num_images_per_prompt`. * Returned tensor shape is `[N, H, W, 3]` for batched generation and `[1, H, W, 3]` for single image. */ async generate(prompt, image, options = {}) { if (!this.pipeline) throw new Error("Image2ImagePipeline is not initialized"); const { callback } = options, generationConfig = __rest(options, ["callback"]); const generatePromise = util.promisify(this.pipeline.generate.bind(this.pipeline)); return await generatePromise(prompt, image, generationConfig, callback); } /** * Decode a latent image into an RGB image tensor using the VAE decoder. * * Useful inside a generation `callback` to obtain the intermediate image at a denoising step. * Returned tensor shape is `[N, H, W, 3]` with `u8` pixels. */ async decode(latent) { if (!this.pipeline) throw new Error("Image2ImagePipeline is not initialized"); const decodePromise = util.promisify(this.pipeline.decode.bind(this.pipeline)); return await decodePromise(latent); } /** * Get performance metrics from the latest generate() call. */ getPerformanceMetrics() { if (!this.pipeline) throw new Error("Image2ImagePipeline is not initialized"); return this.pipeline.getPerformanceMetrics(); } /** * Get current image generation config. */ getGenerationConfig() { if (!this.pipeline) throw new Error("Image2ImagePipeline is not initialized"); return this.pipeline.getGenerationConfig(); } /** * Update image generation config. */ setGenerationConfig(config) { if (!this.pipeline) throw new Error("Image2ImagePipeline is not initialized"); this.pipeline.setGenerationConfig(config); } } //# sourceMappingURL=image2ImagePipeline.js.map