11import { expect , it , describe , assert } from "vitest" ;
22
3- import type { ChatCompletionStreamOutput } from "@huggingface/tasks" ;
3+ import type { ChatCompletionStreamOutput , VisualQuestionAnsweringInput } from "@huggingface/tasks" ;
44
55import { chatCompletion , HfInference } from "../src" ;
66import "./vcr" ;
@@ -87,13 +87,14 @@ describe.concurrent("HfInference", () => {
8787 context : "The capital of France is Paris." ,
8888 } ,
8989 } ) ;
90-
91- expect ( res ) . toMatchObject ( [ {
92- answer : "Paris" ,
93- score : expect . any ( Number ) ,
94- start : expect . any ( Number ) ,
95- end : expect . any ( Number ) ,
96- } ] ) ;
90+ expect ( res ) . toMatchObject ( [
91+ {
92+ answer : "Paris" ,
93+ score : expect . any ( Number ) ,
94+ start : expect . any ( Number ) ,
95+ end : expect . any ( Number ) ,
96+ } ,
97+ ] ) ;
9798 } ) ;
9899
99100 it ( "tableQuestionAnswering" , async ( ) => {
@@ -110,30 +111,31 @@ describe.concurrent("HfInference", () => {
110111 } ,
111112 } ,
112113 } )
113- ) . toMatchObject ( {
114- answer : "AVERAGE > 36542" ,
115- coordinates : [ [ 0 , 1 ] ] ,
116- cells : [ "36542" ] ,
117- aggregator : "AVERAGE" ,
118- } ) ;
114+ ) . toMatchObject ( [
115+ {
116+ answer : "AVERAGE > 36542" ,
117+ coordinates : [ [ 0 , 1 ] ] ,
118+ cells : [ "36542" ] ,
119+ aggregator : "AVERAGE" ,
120+ } ,
121+ ] ) ;
119122 } ) ;
120123
121124 it ( "documentQuestionAnswering" , async ( ) => {
122- expect (
123- await hf . documentQuestionAnswering ( {
124- model : "impira/layoutlm-document-qa" ,
125- inputs : {
126- question : "Invoice number?" ,
127- image : new Blob ( [ readTestFile ( "invoice.png" ) ] , { type : "image/png" } ) ,
128- } ,
129- } )
130- ) . toMatchObject ( {
131- answer : "us-001" ,
132- score : expect . any ( Number ) ,
133- // not sure what start/end refers to in this case
134- start : expect . any ( Number ) ,
135- end : expect . any ( Number ) ,
125+ const res = await hf . documentQuestionAnswering ( {
126+ model : "impira/layoutlm-document-qa" ,
127+ inputs : {
128+ question : "Invoice number?" ,
129+ image : new Blob ( [ readTestFile ( "invoice.png" ) ] , { type : "image/png" } ) ,
130+ } ,
136131 } ) ;
132+ expect ( res ) . toBeInstanceOf ( Array ) ;
133+ for ( const elem of res ) {
134+ expect ( elem ) . toMatchObject ( {
135+ answer : expect . any ( String ) ,
136+ score : expect . any ( Number ) ,
137+ } ) ;
138+ }
137139 } ) ;
138140
139141 // Errors with "Error: If you are using a VisionEncoderDecoderModel, you must provide a feature extractor"
@@ -152,18 +154,20 @@ describe.concurrent("HfInference", () => {
152154 } ) ;
153155
154156 it ( "visualQuestionAnswering" , async ( ) => {
155- expect (
156- await hf . visualQuestionAnswering ( {
157- model : "dandelin/vilt-b32-finetuned-vqa" ,
158- inputs : {
159- question : "How many cats are lying down?" ,
160- image : new Blob ( [ readTestFile ( "cats.png" ) ] , { type : "image/png" } ) ,
161- } ,
162- } )
163- ) . toMatchObject ( {
164- answer : "2" ,
165- score : expect . any ( Number ) ,
166- } ) ;
157+ const res = await hf . visualQuestionAnswering ( {
158+ model : "dandelin/vilt-b32-finetuned-vqa" ,
159+ inputs : {
160+ question : "How many cats are lying down?" ,
161+ image : new Blob ( [ readTestFile ( "cats.png" ) ] , { type : "image/png" } ) ,
162+ } ,
163+ } satisfies VisualQuestionAnsweringInput ) ;
164+ expect ( res ) . toBeInstanceOf ( Array ) ;
165+ for ( const elem of res ) {
166+ expect ( elem ) . toMatchObject ( {
167+ answer : expect . any ( String ) ,
168+ score : expect . any ( Number ) ,
169+ } ) ;
170+ }
167171 } ) ;
168172
169173 it ( "textClassification" , async ( ) => {
@@ -451,7 +455,9 @@ describe.concurrent("HfInference", () => {
451455 model : "espnet/kan-bayashi_ljspeech_vits" ,
452456 inputs : "hello there!" ,
453457 } )
454- ) . toSatisfy ( ( out ) => typeof out === "object" && ! ! out && "image" in out && out . image instanceof Blob ) ;
458+ ) . toMatchObject ( {
459+ audio : expect . any ( Blob ) ,
460+ } ) ;
455461 } ) ;
456462
457463 it ( "imageClassification" , async ( ) => {
@@ -473,7 +479,7 @@ describe.concurrent("HfInference", () => {
473479 it ( "zeroShotImageClassification" , async ( ) => {
474480 expect (
475481 await hf . zeroShotImageClassification ( {
476- inputs : { image : new Blob ( [ readTestFile ( "cheetah.png" ) ] , { type : "image/png" } ) } ,
482+ inputs : new Blob ( [ readTestFile ( "cheetah.png" ) ] , { type : "image/png" } ) ,
477483 model : "openai/clip-vit-large-patch14-336" ,
478484 parameters : {
479485 candidate_labels : [ "animal" , "toy" , "car" ] ,
0 commit comments