This repository has been archived by the owner on Jan 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Change document intelligence api version to 2024 preview (#76)
* fix: Change document intelligence api version to 2024 preview * refactor: remove debug statements
- Loading branch information
1 parent
bef96fb
commit 6e60f08
Showing
6 changed files
with
59 additions
and
199 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,48 +1,30 @@ | ||
import { memoryFileToDiskFile } from '$lib/fileHandling.js'; | ||
import { getAzureDocumentAnalysisClient } from '$lib/server/azure'; | ||
import { DocumentAnalysis } from '$lib/server/azure'; | ||
import { logger } from '$lib/server/utils'; | ||
import { type DocumentAnalysisClient } from '@azure/ai-form-recognizer'; | ||
import fs, { createReadStream } from 'fs'; | ||
import { PrebuiltDocumentModel } from './models'; | ||
import fs from 'fs'; | ||
|
||
export const POST = async ({ request }) => { | ||
logger.info('Document Intelligence API called'); | ||
|
||
const azureDocumentAnalysis = getAzureDocumentAnalysisClient(); | ||
const formData = await request.formData(); | ||
const file = formData.get('file') as File; | ||
if (!file) { | ||
return new Response('No file found.', { status: 422 }); | ||
} | ||
|
||
const pages = await getPages(azureDocumentAnalysis, file); | ||
if (!pages) { | ||
return new Response('No pages found in document', { status: 400 }); | ||
} | ||
|
||
let text = ''; | ||
for (const page of pages) { | ||
if (!page.lines) { | ||
continue; | ||
} | ||
for (const line of page.lines) { | ||
text += line.content + '\n'; | ||
const analysis = new DocumentAnalysis(); | ||
const tempdir = fs.mkdtempSync('temp'); | ||
const filename = `${tempdir}/${file.name}`; | ||
try { | ||
await memoryFileToDiskFile(file, filename); | ||
const response = await analysis.analyze(filename); | ||
if (!response.ok) { | ||
return new Response('Something went wrong, contact an admin.', { status: 500 }); | ||
} | ||
} | ||
return new Response(text, { status: 200 }); | ||
}; | ||
const content = (await response.json()).analyzeResult.content; | ||
|
||
async function getPages(client: DocumentAnalysisClient, file: File) { | ||
const tempDir = fs.mkdtempSync('temp'); | ||
const tempFile = `${tempDir}/${file.name}`; | ||
try { | ||
await memoryFileToDiskFile(file, tempFile); | ||
const stream = createReadStream(tempFile); | ||
const poller = await client.beginAnalyzeDocument(PrebuiltDocumentModel, stream); | ||
const { pages } = await poller.pollUntilDone(); | ||
return pages; | ||
return new Response(content, { status: 200 }); | ||
} finally { | ||
fs.unlinkSync(tempFile); | ||
fs.rmdirSync(tempDir); | ||
fs.unlinkSync(filename); | ||
} | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.