UNPKG

mindee

Version:

Mindee Client Library for Node.js

99 lines (96 loc) 3.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InferenceFactory = exports.Inference = void 0; const extras_1 = require("./extras"); const ragExtra_1 = require("./extras/ragExtra"); /** * * @typeParam DocT an extension of a `Prediction`. Is generic by default to * allow for easier optional `PageT` generic typing. * @typeParam PageT an extension of a `DocT` (`Prediction`). Should only be set * if a document's pages have specific implementation. */ class Inference { constructor(rawPrediction) { /** Wrapper for a document's pages prediction. */ this.pages = []; this.isRotationApplied = rawPrediction?.is_rotation_applied ?? undefined; this.product = rawPrediction?.product; if (rawPrediction["extras"] && Object.keys(rawPrediction["extras"].length > 0)) { const extras = {}; Object.entries(rawPrediction["extras"]).forEach(([extraKey, extraValue]) => { switch (extraKey) { case "cropper": extras.cropper = new extras_1.CropperExtra(extraValue); break; case "full_text_ocr": extras.fullTextOcr = new extras_1.FullTextOcrExtra(extraValue); break; case "rag": extras.rag = new ragExtra_1.RAGExtra(extraValue); break; } }); this.extras = extras; } } /** * Default string representation. */ toString() { let pages = ""; if (this.pages.toString().length > 0) { pages = ` Page Predictions ================ ${this.pages.map((e) => e.toString() || "").join("\n")}`; } return `Inference ######### :Product: ${this.product.name} v${this.product.version} :Rotation applied: ${this.isRotationApplied ? "Yes" : "No"} Prediction ========== ${this.prediction.toString().length === 0 ? "" : this.prediction.toString() + "\n"}${pages}`; } /** * Takes in an input string and replaces line breaks with `\n`. * @param outStr string to cleanup * @returns cleaned out string */ static cleanOutString(outStr) { const lines = / \n/gm; return outStr.replace(lines, "\n"); } } exports.Inference = Inference; /** * Factory to allow for static-like property access syntax in TypeScript. * Used to retrieve endpoint data for standard products. */ class InferenceFactory { /** * Builds a blank product of the given type & sends back the endpointName & endpointVersion parameters of OTS classes. * Note: this is needed to avoid passing anything other than the class of the object to the parse()/enqueue() call. * @param inferenceClass Class of the product we are using * @returns {Inference} An empty instance of a given product. */ static getEndpoint(inferenceClass) { if (inferenceClass.name === "CustomV1") { throw new Error("Cannot process custom endpoint as OTS API endpoints. Please provide an endpoint name & version manually."); } const emptyProduct = new inferenceClass({ prediction: {}, pages: [], }); if (!emptyProduct.endpointName || !emptyProduct.endpointVersion || emptyProduct.endpointName.length === 0 || emptyProduct.endpointVersion.length === 0) { throw new Error(`Error during endpoint verification, no endpoint found for product ${inferenceClass.name}.`); } return [emptyProduct.endpointName, emptyProduct.endpointVersion]; } } exports.InferenceFactory = InferenceFactory;