-
Notifications
You must be signed in to change notification settings - Fork 5.8k
add AWS Go SDK v2 converse stream API sample to handle image input #7458
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xueyao-bai-mike
wants to merge
14
commits into
awsdocs:main
Choose a base branch
from
xueyao-bai-mike:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+115
−0
Open
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e285882
add converse_stream_pdf.py under bedrock_runtime...anthropic_claude
xueyao-bai-mike 5d5fe64
Merge branch 'awsdocs:main' into main
xueyao-bai-mike 050b5b6
1,fix corss-region inference endpoint issue; 2,change pdf path by ask…
xueyao-bai-mike f8ead44
Merge branch 'main' into main
xueyao-bai-mike be10dbf
add converse_stream_pdf.py under bedrock_runtime...anthropic_claude
xueyao-bai-mike 8deb8c6
1,fix corss-region inference endpoint issue; 2,change pdf path by ask…
xueyao-bai-mike 1641c0a
document_OCR_with_textract_bedrock
xueyao-bai-mike 109ae2e
Merge remote-tracking branch 'refs/remotes/origin/main'
xueyao-bai-mike e59b2d5
Merge branch 'main' into main
rlhagerm e6d3a71
Merge branch 'main' of https://github.com/xueyao-bai-mike/aws-doc-sdk…
rlhagerm 2c65a8f
add converse stream api sample to handle image input
xueyao-bai-mike 356e997
Merge branch 'main' into main
beqqrry-aws 0b5771d
remove python/cross_service/document_OCR_with_textract_bedrock/
xueyao-bai-mike 607654d
Merge branch 'main' into main
beqqrry-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package actions | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime" | ||
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime/types" | ||
) | ||
|
||
const ( | ||
promptImage = `Describe what you see in this image in detail.` | ||
) | ||
|
||
func main() { | ||
modelID := "us.anthropic.claude-3-7-sonnet-20250219-v1:0" | ||
// load AWS configure | ||
cfg, err := config.LoadDefaultConfig(context.TODO(), | ||
config.WithRegion("us-west-2"), // replace your bedrock region | ||
) | ||
if err != nil { | ||
log.Fatal("unable to load SDK configuration:", err) | ||
} | ||
|
||
// create bedrock runtime client | ||
client := bedrockruntime.NewFromConfig(cfg) | ||
|
||
// read image file | ||
imagePath := "C:\\Users\\pc\\Downloads\\img.png" // Update with your image path | ||
imageBytes, err := os.ReadFile(imagePath) | ||
if err != nil { | ||
log.Fatal("unable to read image:", err) | ||
} | ||
|
||
// create image block with format specified | ||
imageBlock := types.ContentBlockMemberImage{ | ||
Value: types.ImageBlock{ | ||
Format: types.ImageFormatPng, // Add the format based on your image type | ||
Source: &types.ImageSourceMemberBytes{ | ||
Value: imageBytes, | ||
}, | ||
}, | ||
} | ||
|
||
// create message | ||
messages := types.Message{ | ||
Role: "user", | ||
Content: []types.ContentBlock{ | ||
&types.ContentBlockMemberText{ | ||
Value: promptImage, | ||
}, | ||
&imageBlock, | ||
}, | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
// Use ConverseStream instead of Converse | ||
resp, err := client.ConverseStream(ctx, &bedrockruntime.ConverseStreamInput{ | ||
ModelId: &modelID, | ||
Messages: []types.Message{messages}, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Get the event stream | ||
stream := resp.GetStream() | ||
defer stream.Close() | ||
|
||
fmt.Println("Streaming response:") | ||
var fullResponse strings.Builder | ||
|
||
// Process the streaming response | ||
for event := range stream.Events() { | ||
// Type switch to handle different event types | ||
switch v := event.(type) { | ||
case *types.ConverseStreamOutputMemberContentBlockDelta: | ||
// Handle content block delta | ||
if delta, ok := v.Value.Delta.(*types.ContentBlockDeltaMemberText); ok { | ||
text := delta.Value | ||
fmt.Print(text) | ||
fullResponse.WriteString(text) | ||
} | ||
case *types.ConverseStreamOutputMemberMessageStop: | ||
// Handle message stop event (contains usage info) | ||
fmt.Println("\n\nMessage complete") | ||
} | ||
} | ||
|
||
// Check for any errors that occurred during streaming | ||
if err := stream.Err(); err != nil { | ||
fmt.Printf("\nStream error: %v\n", err) | ||
} | ||
|
||
fmt.Println("\nDone streaming") | ||
fmt.Printf("\nFull response:\n%s\n", fullResponse.String()) | ||
} | ||
|
||
func printJSON(data interface{}) { | ||
jsonBytes, err := json.MarshalIndent(data, "", " ") | ||
if err != nil { | ||
log.Printf("Error marshaling JSON: %v", err) | ||
return | ||
} | ||
log.Printf("\nJSON:\n%s\n", string(jsonBytes)) | ||
} |
195 changes: 195 additions & 0 deletions
195
python/cross_service/document_OCR_with_textract_bedrock/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
# GenAI - OCR Document Recognition in KYC Scenarios | ||
|
||
## Background | ||
|
||
In KYC (Know Your Customers) applications, a typical scenario requires users to upload identification documents for automated information extraction. While traditional manual information extraction has become obsolete due to low efficiency, professional OCR tools have become the standard solution. | ||
|
||
### Professional OCR Tools | ||
|
||
Normally uses image preprocessing techniques (noise reduction, binarization, tilt correction), and it employs deep learning algorithms (CNN, LSTM) for text detection. | ||
|
||
#### Advantages: | ||
- Supports multi-language recognition | ||
- Can preserve table structures | ||
- Handles complex layout analysis | ||
- Adapts to low-quality images and handwriting | ||
- Easy to integrates via APIs | ||
|
||
#### Limitations: | ||
- Limited generalization capabilities | ||
- Less flexible compared to LLMs | ||
- Cannot perform accurate intent recognition | ||
- Unable to make complex inferences (e.g., you can not ask complex questions to OCR Tools) | ||
|
||
### Multimodal Large Language Models (MM-LLM) | ||
|
||
MM-LLM combined the ability to "read" images as well as the ability of thought. | ||
#### Advantages: | ||
- generalization capabilities, can answer questions directly from the image. | ||
|
||
#### Limitations (as of March 21, 2025): | ||
- Lower "visual acuity" compared to OCR, Significant accuracy drop with tilted images | ||
- Particularly challenging with user-submitted photos that may be rotated or flipped | ||
|
||
## Solution: AWS OCR + LLM Integration | ||
|
||
Based on the facts above, we introduce solution combine with OCR (Amazon Textract) + LLM (Amazon Bedrock). Solution architecture as below: | ||
<p align="center"> | ||
<img alt="YOUR-ALT-TEXT" src="images/textract+llm.png"> | ||
</p> | ||
|
||
### Architecture Flow: | ||
1. Client sends image to Amazon Textract for parsing | ||
2. Amazon Textract returns parsing results | ||
3. Client sends Textract results as part of the prompt along with original image to LLM | ||
4. LLM returns final analysis results | ||
|
||
### Implementation | ||
|
||
See app.y | ||
|
||
#### Sample Prompt | ||
```python | ||
prompt = f""" | ||
<ocr_results> | ||
{ocr_text} | ||
</ocr_results> | ||
Analyze this image and corresponding OCR results, carefully and provide the following information: | ||
1. Name: Extract the full name of the person from the document. | ||
2. CPF: Extract the CPF number (Brazilian tax identification number) if present. | ||
3. DOB: Extract the date of birth in the format YYYY-MM-DD. | ||
4. Text Language: Identify the primary language of the text in the image. | ||
Format your response as follows: | ||
Name: [Full Name] | ||
CPF: [CPF Number] | ||
DOB: [YYYY-MM-DD] | ||
Text Language: [Language] | ||
If you can't find or determine any of these fields, use 'Unknown'.Please note the picture can be tilted. | ||
""" | ||
``` | ||
|
||
## Performance Analysis | ||
|
||
### Source data: | ||
<p align="center"> | ||
<img alt="YOUR-ALT-TEXT" src="images/source.png"> | ||
</p> | ||
|
||
|
||
### Results | ||
|
||
``` | ||
(env-01) xueybai@80a99721cf60 project % python txtract+llm.py | ||
{ | ||
"fileFullName": "./images/10.jpeg", | ||
"textLanguage": "Portuguese", | ||
"Name": "Unknown", | ||
"CPF": "xxxxxxx", | ||
"DOB": "Unknown" | ||
} | ||
———————————————————————————————————————— | ||
{ | ||
"fileFullName": "./images/06.jpeg", | ||
"textLanguage": "Portuguese", | ||
"Name": "VAGNER TAVARES DE OLIVEIRA", | ||
"CPF": "xxxxxxxx", | ||
"DOB": "xxxxxxxx" | ||
} | ||
———————————————————————————————————————— | ||
{ | ||
"fileFullName": "./images/07.jpeg", | ||
"textLanguage": "Portuguese", | ||
"Name": "CICERA EUGENIA DOS SANTOS", | ||
"CPF": "xxxxxxxx", | ||
"DOB": "xxxxxxxx" | ||
} | ||
———————————————————————————————————————— | ||
{ | ||
"fileFullName": "./images/01.jpeg", | ||
"textLanguage": "Portuguese/English (Bilingual document)", | ||
"Name": "FRANCIELE DANIELA RODRIGUES", | ||
"CPF": "xxxxxxxx", | ||
"DOB": "xxxxxxxx" | ||
} | ||
———————————————————————————————————————— | ||
{ | ||
"fileFullName": "./images/11.png", | ||
"textLanguage": "Portuguese", | ||
"Name": "THALES HENRIQUE MOGARI", | ||
"CPF": "xxxxxxxx", | ||
"DOB": "xxxxxxxx" | ||
} | ||
———————————————————————————————————————— | ||
{ | ||
"fileFullName": "./images/02.jpeg", | ||
"textLanguage": "Portuguese", | ||
"Name": "LUIS FELIPE GONCALVES NOGUEIRA DA SILVA", | ||
"CPF": "xxxxxxxx", | ||
"DOB": "xxxxxxxx" | ||
} | ||
———————————————————————————————————————— | ||
{ | ||
"fileFullName": "./images/03.jpeg", | ||
"textLanguage": "Portuguese", | ||
"Name": "LUCAS DENILSON DA SILVA COSTA", | ||
"CPF": "xxxxxxxx", | ||
"DOB": "xxxxxxxx" | ||
} | ||
———————————————————————————————————————— | ||
{ | ||
"fileFullName": "./images/04.jpeg", | ||
"textLanguage": "Portuguese", | ||
"Name": "JESSICA OLIVEIRA DE SOUSA", | ||
"CPF": "xxxxxxxx", | ||
"DOB": "xxxxxxxx" | ||
} | ||
———————————————————————————————————————— | ||
{ | ||
"fileFullName": "./images/08.jpeg", | ||
"textLanguage": "Portuguese/English (Bilingual document)", | ||
"Name": "Arthur Marcel Dos Santos", | ||
"CPF": "xxxxxxxx", | ||
"DOB": "xxxxxxxx" | ||
} | ||
———————————————————————————————————————— | ||
{ | ||
"fileFullName": "./images/09.jpeg", | ||
"textLanguage": "Portuguese", | ||
"Name": "LUCAS FERNANDES DE OLIVEIRA", | ||
"CPF": "xxxxxxxx", | ||
"DOB": "xxxxxxxx" | ||
} | ||
———————————————————————————————————————— | ||
{ | ||
"fileFullName": "./images/05.jpeg", | ||
"textLanguage": "Portuguese", | ||
"Name": "THALITA ALVES SIQUEIRA", | ||
"CPF": "xxxxxxxx", | ||
"DOB": "xxxxxxxx" | ||
} | ||
———————————————————————————————————————— | ||
``` | ||
|
||
### Result summary: | ||
- Tested with 11 sample images in various orientations | ||
- 10 out of 11 images were accurately recognized | ||
- Only image #10 (with significant contamination) failed recognition | ||
- System successfully handled various image orientations | ||
|
||
The reason that #10 failed is because it was tainted so as expected | ||
|
||
<p align="center"> | ||
<img alt="YOUR-ALT-TEXT" src="images/tainted.png"> | ||
</p> | ||
|
||
|
||
### Cost Analysis | ||
|
||
|
||
For processing 1 document: | ||
| Service | Average Usage per Image | Unit Price | Total Price | Notes | | ||
|---------|------------------------|------------|-------------|--------| | ||
| Amazon Textract | 1 | $0.0015 USD | $0.0015 USD | - | | ||
| Amazon Bedrock Sonnet V2 | Input: 1300 tokens<br>Output: 500 tokens | Input: $0.003/1K tokens<br>Output: $0.015/1K tokens | $0.0105 USD | Image input tokens ≈ pixels/750 | | ||
| **Total** | - | - | **$0.012 USD** | - | | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.