-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathdocumentQuestionAnswering.ts
42 lines (40 loc) · 1.55 KB
/
documentQuestionAnswering.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type {
DocumentQuestionAnsweringInput,
DocumentQuestionAnsweringInputData,
DocumentQuestionAnsweringOutput,
} from "@huggingface/tasks";
import { resolveProvider } from "../../lib/getInferenceProviderMapping";
import { getProviderHelper } from "../../lib/getProviderHelper";
import type { BaseArgs, Options, RequestArgs } from "../../types";
import { base64FromBytes } from "../../utils/base64FromBytes";
import { innerRequest } from "../../utils/request";
/// Override the type to properly set inputs.image as Blob
export type DocumentQuestionAnsweringArgs = BaseArgs &
DocumentQuestionAnsweringInput & { inputs: DocumentQuestionAnsweringInputData & { image: Blob } };
/**
* Answers a question on a document image. Recommended model: impira/layoutlm-document-qa.
*/
export async function documentQuestionAnswering(
args: DocumentQuestionAnsweringArgs,
options?: Options
): Promise<DocumentQuestionAnsweringOutput[number]> {
const provider = await resolveProvider(args.provider, args.model);
const providerHelper = getProviderHelper(provider, "document-question-answering");
const reqArgs: RequestArgs = {
...args,
inputs: {
question: args.inputs.question,
// convert Blob or ArrayBuffer to base64
image: base64FromBytes(new Uint8Array(await args.inputs.image.arrayBuffer())),
},
} as RequestArgs;
const { data: res } = await innerRequest<DocumentQuestionAnsweringOutput | DocumentQuestionAnsweringOutput[number]>(
reqArgs,
providerHelper,
{
...options,
task: "document-question-answering",
}
);
return providerHelper.getResponse(res);
}