-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathgoogleDocumentAI.ts
94 lines (80 loc) · 2.59 KB
/
googleDocumentAI.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
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
import fs from 'fs';
import { DocumentProcessorServiceClient } from '@google-cloud/documentai';
import { ModelProvider } from './base';
// https://cloud.google.com/document-ai/pricing
// $1.5 per 1000 pages for the first 5M pages
const COST_PER_PAGE = 1.5 / 1000;
export class GoogleDocumentAIProvider extends ModelProvider {
private client: DocumentProcessorServiceClient;
private processorPath: string;
constructor() {
super('google-document-ai');
const projectId = process.env.GOOGLE_PROJECT_ID;
const location = process.env.GOOGLE_LOCATION || 'us'; // default to 'us'
const processorId = process.env.GOOGLE_PROCESSOR_ID;
if (!projectId || !processorId) {
throw new Error('Missing required Google Document AI configuration');
}
const credentials = JSON.parse(
fs.readFileSync(process.env.GOOGLE_APPLICATION_CREDENTIALS_PATH || '', 'utf8'),
);
this.client = new DocumentProcessorServiceClient({
credentials,
});
this.processorPath = `projects/${projectId}/locations/${location}/processors/${processorId}`;
}
async ocr(imagePath: string) {
try {
// Download the image
const response = await fetch(imagePath);
const arrayBuffer = await response.arrayBuffer();
const imageContent = Buffer.from(arrayBuffer).toString('base64');
// Determine MIME type from URL
const mimeType = this.getMimeType(imagePath);
const request = {
name: this.processorPath,
rawDocument: {
content: imageContent,
mimeType: mimeType,
},
};
const start = performance.now();
const [result] = await this.client.processDocument(request);
const { document } = result;
const end = performance.now();
// Extract text from the document
const text = document?.text || '';
return {
text,
usage: {
duration: end - start,
totalCost: COST_PER_PAGE, // the input is always 1 page.
},
};
} catch (error) {
console.error('Google Document AI Error:', error);
throw error;
}
}
private getMimeType(url: string): string {
const extension = url.split('.').pop()?.toLowerCase();
switch (extension) {
case 'pdf':
return 'application/pdf';
case 'png':
return 'image/png';
case 'jpg':
case 'jpeg':
return 'image/jpeg';
case 'tiff':
case 'tif':
return 'image/tiff';
case 'gif':
return 'image/gif';
case 'bmp':
return 'image/bmp';
default:
return 'image/png'; // default to PNG
}
}
}