Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/code_samples/default_v2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const inputSource = new mindee.PathInput({ inputPath: filePath });

// Send for processing
const response = mindeeClient.enqueueAndGetInference(
mindee.v2.ExtractionInference,
inputSource,
inferenceParams
);
Expand Down
2 changes: 1 addition & 1 deletion src/http/apiCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function sendRequestAndReadResponse(
}
try {
const parsedResponse = JSON.parse(responseBody);
logger.debug("JSON parsed successfully, returning object.");
logger.debug("JSON parsed successfully, returning plain object.");
return {
messageObj: response,
data: parsedResponse,
Expand Down
19 changes: 14 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ export * as v1 from "./v1/index.js";
export * as v2 from "./v2/index.js";
export {
Client,
ExtractionParameters,
DataSchema,
InferenceFile,
InferenceResponse,
InferenceModel,
ClassificationInference,
ClassificationResponse,
CropInference,
CropResponse,
ExtractionInference,
ExtractionResponse,
OcrInference,
OcrResponse,
SplitInference,
SplitResponse,
JobResponse,
RawText,
RagMetadata,
InferenceParameters,
DataSchema,
ErrorResponse,
} from "./v2/index.js";
export type { PollingOptions } from "./v2/index.js";
2 changes: 1 addition & 1 deletion src/input/localInputSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export abstract class LocalInputSource extends InputSource {
);
}
this.inputType = inputType;
logger.debug(`Loading file from: ${inputType}`);
logger.debug(`New local input source of type: ${inputType}`);
}

protected async checkMimetype(): Promise<string> {
Expand Down
70 changes: 47 additions & 23 deletions src/v2/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import { Command, OptionValues } from "commander";
import { Client } from "./client.js";
import { PathInput } from "../input/index.js";
import * as console from "console";
import { Inference } from "@/v2/parsing/index.js";
import {
BaseInference,
ClassificationInference,
CropInference,
ExtractionInference,
OcrInference,
SplitInference,
InferenceResponseConstructor,
} from "@/v2/parsing/inference/index.js";

const program = new Command();

Expand All @@ -18,28 +26,33 @@ function initClient(options: OptionValues): Client {
});
}

async function callEnqueueAndGetInference(
async function enqueueAndGetInference(
responseType: InferenceResponseConstructor<any>,
inputPath: string,
options: any
options: OptionValues
): Promise<void> {
const mindeeClient = initClient(options);
const inputSource = new PathInput({ inputPath: inputPath });
const response = await mindeeClient.enqueueAndGetInference(inputSource, {
modelId: options.model,
pollingOptions: {
initialDelaySec: 2,
delaySec: 1.5,
maxRetries: 80,
const response = await mindeeClient.enqueueAndGetInference(
responseType,
inputSource,
{
modelId: options.model,
pollingOptions: {
initialDelaySec: 2,
delaySec: 1.5,
maxRetries: 80,
}
}
});
);
if (!response.inference) {
throw Error("Inference could not be retrieved");
}
printResponse(response.inference);
}

function printResponse(
document: Inference,
document: BaseInference,
): void {
if (document) {
console.log(`\n${document}`);
Expand All @@ -55,26 +68,37 @@ function addMainOptions(prog: Command) {
"-m, --model <model_id>",
"Model ID (required)"
);
prog.option("-k, --api-key <api_key>", "API key for document endpoint");
prog.argument("<input_path>", "full path to the file");
}

export function cli() {
program.name("mindee")
.description("Command line interface for Mindee products.")
.option("-d, --debug", "high verbosity mode");
.description("Command line interface for Mindee V2 products.")
.option("-d, --debug", "high verbosity mode")
.option("-k, --api-key <api_key>", "your Mindee API key");

const inferenceCmd: Command = program.command("parse")
.description("Send a file and retrieve the inference results.");
const inferenceTypes = [
{ name: "extract", description: "Extract data from a document.", responseType: ExtractionInference },
{ name: "crop", description: "Crop a document.", responseType: CropInference },
{ name: "split", description: "Split a document into pages.", responseType: SplitInference },
{ name: "ocr", description: "Read text from a document.", responseType: OcrInference },
{ name: "classify", description: "Classify a document.", responseType: ClassificationInference },
];

addMainOptions(inferenceCmd);
for (const inference of inferenceTypes) {
const inferenceCmd: Command = program.command(inference.name)
.description(inference.description);

inferenceCmd.action(function (
inputPath: string,
options: OptionValues,
) {
return callEnqueueAndGetInference(inputPath, options);
});
addMainOptions(inferenceCmd);

inferenceCmd.action(function (
inputPath: string,
options: OptionValues,
) {
const allOptions = { ...program.opts(), ...options };
return enqueueAndGetInference(inference.responseType, inputPath, allOptions);
});
}

program.parse(process.argv);
}
Loading
Loading