-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[Document Intelligence] Corrupted images #31908
base: main
Are you sure you want to change the base?
Changes from all commits
b98c8e9
e9fca6c
c1d1c75
3c11c0d
b934b02
55ab26d
039637e
fe45383
2390c9c
db21c03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
import { env } from "@azure-tools/test-recorder"; | ||
import DocumentIntelligence from "../../src/documentIntelligence.js"; | ||
import { describe, it } from "vitest"; | ||
import fs from "fs"; | ||
import path from "path"; | ||
import { getLongRunningPoller, isUnexpected } from "../../src/index.js"; | ||
import { | ||
ASSET_PATH, | ||
} from "./utils/utils.js"; | ||
|
||
describe("DocumentIntelligenceClient", () => { | ||
describe("get AnalyzeResult methods", function () { | ||
it("getAnalyzeResult figures", async function () { | ||
await analyze(); | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
const MODEL_ID = 'prebuilt-layout'; | ||
|
||
const FILE_PATH = path.join(ASSET_PATH, "test.pdf"); | ||
|
||
const API_ENDPOINT = env.DOCUMENT_INTELLIGENCE_ENDPOINT || ""; | ||
const API_KEY = env.DOCUMENT_INTELLIGENCE_API_KEY || ""; | ||
|
||
const client = DocumentIntelligence(API_ENDPOINT, { | ||
key: API_KEY, | ||
}, { | ||
additionalPolicies: [{ | ||
policy: { | ||
name: 'documentIntelligencePolicy', | ||
async sendRequest(request, next) { | ||
console.log(`Request: ${request.url}`); | ||
return next(request); | ||
} | ||
}, | ||
position: "perCall" | ||
}] | ||
} | ||
); | ||
|
||
|
||
interface AnalyzeResponse { | ||
analyzeResult?: { | ||
figures?: Figure[]; | ||
}; | ||
} | ||
|
||
interface Figure { | ||
id?: string; | ||
} | ||
|
||
async function fetchFigure(responseId: string, figureId: string): Promise<void | null> { | ||
console.log(`modelId: ${MODEL_ID}, responseId: ${responseId}, figureId: ${figureId}`); | ||
const response = await client | ||
.path(`/documentModels/{modelId}/analyzeResults/{resultId}/figures/{figureId}`, MODEL_ID, responseId, figureId) | ||
.get(); | ||
|
||
console.log(response.headers["content-type"]); | ||
console.log(response); | ||
const { body } = response; | ||
|
||
await fs.promises.writeFile(`./figures/${figureId}.png`, Buffer.from(body, "binary")); | ||
} | ||
|
||
async function analyze(): Promise<void> { | ||
const base64Source = fs.readFileSync(FILE_PATH, { encoding: 'base64' }); | ||
await fs.promises.rm(`./figures`, { recursive: true }).catch(() => { }); | ||
await fs.promises.mkdir(`./figures`); | ||
|
||
const initialResponse = await client.path(`/documentModels/{modelId}:analyze`, MODEL_ID).post({ | ||
contentType: 'application/json', | ||
body: { base64Source }, | ||
queryParameters: { | ||
output: ['figures'], | ||
}, | ||
}); | ||
|
||
const poller = await getLongRunningPoller(client, initialResponse); | ||
|
||
const response = await poller.pollUntilDone(); | ||
|
||
if (isUnexpected(response)) { | ||
console.log(response); | ||
throw new Error("The response was unexpected."); | ||
} | ||
|
||
const figures = (response.body as AnalyzeResponse).analyzeResult?.figures || []; | ||
|
||
for (const figure of figures) { | ||
if (figure.id) { | ||
|
||
await fetchFigure(poller.getOperationId(), figure.id); | ||
} | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,15 +6,15 @@ import { relativeRecordingsPath } from "@azure-tools/test-recorder"; | |
|
||
export default defineConfig({ | ||
test: { | ||
reporters: ["verbose", "basic"], | ||
reporters: ["verbose"], | ||
outputFile: { | ||
junit: "test-results.browser.xml", | ||
}, | ||
fakeTimers: { | ||
toFake: ["setTimeout", "Date"], | ||
}, | ||
watch: false, | ||
include: ["test/**/*.spec.ts"], | ||
include: ["test/**/figures.spec.ts"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we only want to keep it at one test file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, apologies about this. I was testing my changes in |
||
exclude: ["test/**/browser/*.spec.ts"], | ||
coverage: { | ||
include: ["src/**/*.ts"], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utf8 encoding corrupts the image data