-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathzerox.ts
41 lines (32 loc) · 1020 Bytes
/
zerox.ts
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
import { zerox } from 'zerox';
import { ModelProvider } from './base';
import { calculateTokenCost } from './shared';
export class ZeroxProvider extends ModelProvider {
constructor() {
super('zerox');
}
async ocr(imagePath: string) {
const startTime = performance.now();
const result = await zerox({
filePath: imagePath,
openaiAPIKey: process.env.OPENAI_API_KEY,
});
const endTime = performance.now();
const text = result.pages.map((page) => page.content).join('\n');
const inputCost = calculateTokenCost(this.model, 'input', result.inputTokens);
const outputCost = calculateTokenCost(this.model, 'output', result.outputTokens);
const usage = {
duration: endTime - startTime,
inputTokens: result.inputTokens,
outputTokens: result.outputTokens,
totalTokens: result.inputTokens + result.outputTokens,
inputCost,
outputCost,
totalCost: inputCost + outputCost,
};
return {
text,
usage,
};
}
}