Skip to content

Commit fb50bcc

Browse files
author
Bob Strahan
committed
Add OCR confidence visualization in UI
1 parent 7d9daad commit fb50bcc

File tree

7 files changed

+44
-13
lines changed

7 files changed

+44
-13
lines changed

lib/idp_common_pkg/idp_common/appsync/service.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def _document_to_update_input(self, document: Document) -> Dict[str, Any]:
117117
"Class": page.classification or "",
118118
"ImageUri": page.image_uri or "",
119119
"TextUri": page.parsed_text_uri or page.raw_text_uri or "",
120+
"TextConfidenceUri": page.text_confidence_uri or "",
120121
}
121122
pages_data.append(page_data)
122123

@@ -290,6 +291,7 @@ def _appsync_to_document(self, appsync_data: Dict[str, Any]) -> Document:
290291
page_id=page_id,
291292
image_uri=page_data.get("ImageUri"),
292293
raw_text_uri=page_data.get("TextUri"),
294+
text_confidence_uri=page_data.get("TextConfidenceUri"),
293295
classification=page_data.get("Class"),
294296
)
295297

src/api/schema.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Page @aws_cognito_user_pools @aws_iam {
4646
Class: String
4747
ImageUri: String
4848
TextUri: String
49+
TextConfidenceUri: String
4950
}
5051

5152
type DocumentList @aws_cognito_user_pools @aws_iam {
@@ -125,6 +126,7 @@ input PageInput {
125126
Class: String
126127
ImageUri: String
127128
TextUri: String
129+
TextConfidenceUri: String
128130
}
129131

130132
type CopyToBaselineResponse @aws_cognito_user_pools {

src/ui/src/components/common/map-document-attributes.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ const mapDocumentsAttributes = (documents) => {
8888
workflowStatus,
8989
duration: getDuration(completionTime, initialEventTime),
9090
sections,
91-
pages,
91+
pages:
92+
pages?.map((page) => ({
93+
...page,
94+
TextConfidenceUri: page.TextConfidenceUri || null,
95+
})) || [],
9296
pageCount,
9397
metering,
9498
evaluationReportUri,

src/ui/src/components/document-viewer/MarkdownJsonViewer.jsx

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,17 @@ const TextEditorView = ({ fileContent, onChange, isReadOnly, fileType }) => {
9696
);
9797
};
9898

99-
const FileEditorView = ({ fileContent, onChange, isReadOnly = true, fileType = 'text' }) => {
99+
const FileEditorView = ({
100+
fileContent,
101+
onChange,
102+
isReadOnly = true,
103+
fileType = 'text',
104+
viewMode,
105+
onViewModeChange,
106+
textConfidenceUri,
107+
}) => {
100108
const [isValid, setIsValid] = useState(true);
101109
const [jsonData, setJsonData] = useState(null);
102-
const [viewMode, setViewMode] = useState('markdown');
103110

104111
useEffect(() => {
105112
if (fileType === 'json') {
@@ -132,19 +139,16 @@ const FileEditorView = ({ fileContent, onChange, isReadOnly = true, fileType = '
132139
}
133140
};
134141

135-
const handleViewModeChange = ({ detail }) => {
136-
setViewMode(detail.selectedId);
137-
};
138-
139142
return (
140143
<Box>
141144
<SpaceBetween direction="vertical" size="xs">
142145
<SegmentedControl
143146
selectedId={viewMode}
144-
onChange={handleViewModeChange}
147+
onChange={onViewModeChange}
145148
options={[
146149
{ id: 'markdown', text: 'Markdown View' },
147150
{ id: 'text', text: 'Text View' },
151+
...(textConfidenceUri ? [{ id: 'confidence', text: 'Text Confidence View' }] : []),
148152
]}
149153
/>
150154

@@ -155,7 +159,7 @@ const FileEditorView = ({ fileContent, onChange, isReadOnly = true, fileType = '
155159
)}
156160
</SpaceBetween>
157161

158-
{viewMode === 'markdown' ? (
162+
{viewMode === 'markdown' || viewMode === 'confidence' ? (
159163
<MarkdownViewer
160164
simple
161165
content={
@@ -185,23 +189,25 @@ const FileEditorView = ({ fileContent, onChange, isReadOnly = true, fileType = '
185189
);
186190
};
187191

188-
const MarkdownJsonViewer = ({ fileUri, fileType = 'text', buttonText = 'View File' }) => {
192+
const MarkdownJsonViewer = ({ fileUri, textConfidenceUri, fileType = 'text', buttonText = 'View File' }) => {
189193
const [fileContent, setFileContent] = useState(null);
190194
const [isLoading, setIsLoading] = useState(false);
191195
const [error, setError] = useState(null);
192196
const [success, setSuccess] = useState(null);
193197
const [isEditing, setIsEditing] = useState(false);
194198
const [editedContent, setEditedContent] = useState(null);
199+
const [viewMode, setViewMode] = useState('markdown');
195200

196201
const fetchContent = async () => {
197202
setIsLoading(true);
198203
setError(null);
199204
try {
200-
logger.info('Fetching content:', fileUri);
205+
const uriToFetch = viewMode === 'confidence' && textConfidenceUri ? textConfidenceUri : fileUri;
206+
logger.info('Fetching content:', uriToFetch);
201207

202208
const response = await API.graphql({
203209
query: getFileContents,
204-
variables: { s3Uri: fileUri },
210+
variables: { s3Uri: uriToFetch },
205211
});
206212

207213
// Handle the updated response structure
@@ -321,6 +327,13 @@ const MarkdownJsonViewer = ({ fileUri, fileType = 'text', buttonText = 'View Fil
321327
);
322328
}
323329

330+
const handleViewModeChange = ({ detail }) => {
331+
setViewMode(detail.selectedId);
332+
// Clear content when switching views to force re-fetch
333+
setFileContent(null);
334+
setEditedContent(null);
335+
};
336+
324337
return (
325338
<Box className="w-full">
326339
{!fileContent && (
@@ -366,6 +379,9 @@ const MarkdownJsonViewer = ({ fileUri, fileType = 'text', buttonText = 'View Fil
366379
onChange={handleContentChange}
367380
isReadOnly={!isEditing}
368381
fileType={fileType}
382+
viewMode={viewMode}
383+
onViewModeChange={handleViewModeChange}
384+
textConfidenceUri={textConfidenceUri}
369385
/>
370386
</div>
371387
</SpaceBetween>

src/ui/src/components/pages-panel/PagesPanel.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ const ThumbnailCell = ({ imageUrl }) => (
4343

4444
const ActionsCell = ({ item }) =>
4545
item.TextUri ? (
46-
<MarkdownJsonViewer fileUri={item.TextUri} fileType="text" buttonText="View/Edit Data" />
46+
<MarkdownJsonViewer
47+
fileUri={item.TextUri}
48+
textConfidenceUri={item.TextConfidenceUri}
49+
fileType="text"
50+
buttonText="View/Edit Data"
51+
/>
4752
) : (
4853
<Box color="text-status-inactive">No text available</Box>
4954
);

src/ui/src/graphql/queries/getDocument.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default gql`
3030
Class
3131
ImageUri
3232
TextUri
33+
TextConfidenceUri
3334
}
3435
Metering
3536
EvaluationReportUri

src/ui/src/graphql/queries/onUpdateDocument.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default gql`
3030
Class
3131
ImageUri
3232
TextUri
33+
TextConfidenceUri
3334
}
3435
EvaluationReportUri
3536
EvaluationStatus

0 commit comments

Comments
 (0)