|
| 1 | +const { TextractClient, AnalyzeDocumentCommand, InvalidS3ObjectException } = require("@aws-sdk/client-textract"); |
| 2 | + |
| 3 | +const BaseService = require("../../services/BaseService"); |
| 4 | + |
| 5 | +class AWSTextractService extends BaseService { |
| 6 | + _construct () { |
| 7 | + this.clients_ = {}; |
| 8 | + } |
| 9 | + |
| 10 | + static IMPLEMENTS = { |
| 11 | + ['puter-ocr']: { |
| 12 | + async recognize ({ source, test_mode }) { |
| 13 | + const resp = await this.analyze_document(source); |
| 14 | + |
| 15 | + // Simplify the response for common interface |
| 16 | + const puter_response = { |
| 17 | + blocks: [] |
| 18 | + }; |
| 19 | + |
| 20 | + for ( const block of resp.Blocks ) { |
| 21 | + if ( block.BlockType === 'PAGE' ) continue; |
| 22 | + if ( block.BlockType === 'CELL' ) continue; |
| 23 | + if ( block.BlockType === 'TABLE' ) continue; |
| 24 | + if ( block.BlockType === 'MERGED_CELL' ) continue; |
| 25 | + if ( block.BlockType === 'LAYOUT_FIGURE' ) continue; |
| 26 | + if ( block.BlockType === 'LAYOUT_TEXT' ) continue; |
| 27 | + |
| 28 | + const puter_block = { |
| 29 | + type: `text/textract:${block.BlockType}`, |
| 30 | + confidence: block.Confidence, |
| 31 | + text: block.Text, |
| 32 | + }; |
| 33 | + puter_response.blocks.push(puter_block); |
| 34 | + } |
| 35 | + |
| 36 | + return puter_response; |
| 37 | + } |
| 38 | + }, |
| 39 | + }; |
| 40 | + |
| 41 | + _create_aws_credentials () { |
| 42 | + return { |
| 43 | + accessKeyId: this.config.aws.access_key, |
| 44 | + secretAccessKey: this.config.aws.secret_key, |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + _get_client (region) { |
| 49 | + if ( ! region ) { |
| 50 | + region = this.config.aws?.region ?? this.global_config.aws?.region |
| 51 | + ?? 'us-west-2'; |
| 52 | + } |
| 53 | + if ( this.clients_[region] ) return this.clients_[region]; |
| 54 | + |
| 55 | + this.clients_[region] = new TextractClient({ |
| 56 | + credentials: this._create_aws_credentials(), |
| 57 | + region, |
| 58 | + }); |
| 59 | + |
| 60 | + return this.clients_[region]; |
| 61 | + } |
| 62 | + |
| 63 | + async analyze_document (file_facade) { |
| 64 | + const { |
| 65 | + client, document, using_s3 |
| 66 | + } = await this._get_client_and_document(file_facade); |
| 67 | + |
| 68 | + const command = new AnalyzeDocumentCommand({ |
| 69 | + Document: document, |
| 70 | + FeatureTypes: [ |
| 71 | + // 'TABLES', |
| 72 | + // 'FORMS', |
| 73 | + // 'SIGNATURES', |
| 74 | + 'LAYOUT' |
| 75 | + ], |
| 76 | + }); |
| 77 | + |
| 78 | + try { |
| 79 | + return await client.send(command); |
| 80 | + } catch (e) { |
| 81 | + if ( using_s3 && e instanceof InvalidS3ObjectException ) { |
| 82 | + const { client, document } = |
| 83 | + await this._get_client_and_document(file_facade, true); |
| 84 | + const command = new AnalyzeDocumentCommand({ |
| 85 | + Document: document, |
| 86 | + FeatureTypes: [ |
| 87 | + 'LAYOUT', |
| 88 | + ], |
| 89 | + }) |
| 90 | + return await client.send(command); |
| 91 | + } |
| 92 | + |
| 93 | + throw e; |
| 94 | + } |
| 95 | + |
| 96 | + throw new Error('expected to be unreachable'); |
| 97 | + } |
| 98 | + |
| 99 | + async _get_client_and_document (file_facade, force_buffer) { |
| 100 | + const try_s3info = await file_facade.get('s3-info'); |
| 101 | + if ( try_s3info && ! force_buffer ) { |
| 102 | + console.log('S3 INFO', try_s3info) |
| 103 | + return { |
| 104 | + using_s3: true, |
| 105 | + client: this._get_client(try_s3info.bucket_region), |
| 106 | + document: { |
| 107 | + S3Object: { |
| 108 | + Bucket: try_s3info.bucket, |
| 109 | + Name: try_s3info.key, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + const try_buffer = await file_facade.get('buffer'); |
| 116 | + if ( try_buffer ) { |
| 117 | + const base64 = try_buffer.toString('base64'); |
| 118 | + return { |
| 119 | + client: this._get_client(), |
| 120 | + document: { |
| 121 | + Bytes: try_buffer, |
| 122 | + }, |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + const fsNode = await file_facade.get('fs-node'); |
| 127 | + if ( fsNode && ! await fsNode.exists() ) { |
| 128 | + throw APIError.create('subject_does_not_exist'); |
| 129 | + } |
| 130 | + |
| 131 | + throw new Error('No suitable input for Textract'); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +module.exports = { |
| 136 | + AWSTextractService, |
| 137 | +}; |
0 commit comments