-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcli.ts
More file actions
111 lines (97 loc) · 2.87 KB
/
cli.ts
File metadata and controls
111 lines (97 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { Command, OptionValues } from "commander";
import * as console from "console";
import { Client, InputSource, PathInput, UrlInput } from "@/index.js";
import { BaseInference } from "@/v2/parsing/inference/index.js";
import { BaseProduct } from "@/v2/product/baseProduct.js";
import {
Classification,
Crop,
Extraction,
Ocr,
Split,
} from "@/v2/product/index.js";
interface CliProduct {
name: string;
description: string;
productClass: typeof BaseProduct;
}
const program = new Command();
//
// EXECUTE THE COMMANDS
//
function initClient(options: OptionValues): Client {
return new Client({
apiKey: options.apiKey,
debug: options.debug,
});
}
async function enqueueAndGetInference(
product: typeof BaseProduct,
inputPath: string,
options: OptionValues
): Promise<void> {
const mindeeClient = initClient(options);
let inputSource: InputSource;
if (inputPath.startsWith("https://")) {
inputSource = new UrlInput({ url: inputPath });
} else {
inputSource = new PathInput({ inputPath: inputPath });
}
const response = await mindeeClient.enqueueAndGetResult(
product,
inputSource,
{ modelId: options.model },
{
initialDelaySec: 2,
delaySec: 1.5,
maxRetries: 80,
}
);
if (!response.inference) {
throw Error("Inference could not be retrieved");
}
printResponse(response.inference);
}
function printResponse(
document: BaseInference,
): void {
if (document) {
console.log(`\n${document}`);
}
}
//
// BUILD THE COMMANDS
//
function addMainOptions(prog: Command) {
prog.requiredOption(
"-m, --model <model_id>",
"Model ID (required)"
);
prog.argument("<input_path>", "full path or URL to the file");
}
export function cli() {
program.name("mindee")
.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 inferenceTypes: CliProduct[] = [
{ name: "extraction", description: "Extract data from a document.", productClass: Extraction },
{ name: "crop", description: "Crop a document.", productClass: Crop },
{ name: "split", description: "Split a document into pages.", productClass: Split },
{ name: "ocr", description: "Read text from a document.", productClass: Ocr },
{ name: "classification", description: "Classify a document.", productClass: Classification },
];
for (const inference of inferenceTypes) {
const inferenceCmd: Command = program.command(inference.name)
.description(inference.description);
addMainOptions(inferenceCmd);
inferenceCmd.action(function (
inputPath: string,
options: OptionValues,
) {
const allOptions = { ...program.opts(), ...options };
return enqueueAndGetInference(inference.productClass, inputPath, allOptions);
});
}
program.parse(process.argv);
}