1
+ const { createFile } = require ( "./files" ) ;
2
+
1
3
/**
2
4
* Create a new prediction
3
5
*
11
13
* @returns {Promise<object> } Resolves with the created prediction
12
14
*/
13
15
async function createPrediction ( options ) {
14
- const { model, version, stream, ...data } = options ;
16
+ const { model, version, stream, input , ...data } = options ;
15
17
16
18
if ( data . webhook ) {
17
19
try {
@@ -22,16 +24,27 @@ async function createPrediction(options) {
22
24
}
23
25
}
24
26
27
+ // Automatically upload any files and replace them with URLs
28
+ if ( typeof input === "object" && input !== null ) {
29
+ for ( const key in input ) {
30
+ if ( input [ key ] instanceof File || input [ key ] instanceof Blob ) {
31
+ input [ key ] = createFile ( input [ key ] ) . then ( ( file ) => file . urls . get ) ;
32
+ }
33
+ }
34
+ }
35
+ // Wait for all file uploads to complete
36
+ await Promise . all ( Object . values ( input ) ) ;
37
+
25
38
let response ;
26
39
if ( version ) {
27
40
response = await this . request ( "/predictions" , {
28
41
method : "POST" ,
29
- data : { ...data , stream, version } ,
42
+ data : { ...data , input , stream, version } ,
30
43
} ) ;
31
44
} else if ( model ) {
32
45
response = await this . request ( `/models/${ model } /predictions` , {
33
46
method : "POST" ,
34
- data : { ...data , stream } ,
47
+ data : { ...data , input , stream } ,
35
48
} ) ;
36
49
} else {
37
50
throw new Error ( "Either model or version must be specified" ) ;
0 commit comments