1+ const { createFile } = require ( "./files" ) ;
2+
13/**
24 * Create a new prediction
35 *
1113 * @returns {Promise<object> } Resolves with the created prediction
1214 */
1315async function createPrediction ( options ) {
14- const { model, version, stream, ...data } = options ;
16+ const { model, version, stream, input , ...data } = options ;
1517
1618 if ( data . webhook ) {
1719 try {
@@ -22,16 +24,27 @@ async function createPrediction(options) {
2224 }
2325 }
2426
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+
2538 let response ;
2639 if ( version ) {
2740 response = await this . request ( "/predictions" , {
2841 method : "POST" ,
29- data : { ...data , stream, version } ,
42+ data : { ...data , input , stream, version } ,
3043 } ) ;
3144 } else if ( model ) {
3245 response = await this . request ( `/models/${ model } /predictions` , {
3346 method : "POST" ,
34- data : { ...data , stream } ,
47+ data : { ...data , input , stream } ,
3548 } ) ;
3649 } else {
3750 throw new Error ( "Either model or version must be specified" ) ;
0 commit comments