How to do model I/O with files
Call models by passing a file or URL
Baseten supports a wide variety of file-based I/O approaches. These examples show our recommendations for working with files during model inference, whether local or remote, public or private, in the Truss or in your invocation code.
Files as input
Example: Send a file with JSON-serializable content
The Truss CLI has a -f
flag to pass file input. If youโre using the API endpoint via Python, get file contents with the standard f.read()
function.
Example: Send a file with non-serializable content
The -f
flag for truss predict
only applies to JSON-serializable content. For other files, like the audio files required by MusicGen Melody, the file content needs to be base64 encoded before it is sent.
Example: Send a URL to a public file
Rather than encoding and serializing a file to send in the HTTP request, you can instead write a Truss that takes a URL as input and loads the content in the preprocess()
function.
Hereโs an example from Whisper in the model library.
Files as output
Example: Save model output to local file
When saving model output to a local file, thereโs nothing Baseten-specific about the code. Just use the standard >
operator in bash or file.write()
function in Python to save the model output.
Output for some models, like image and audio generation models, may need to be decoded before you save it. See how to parse base64 output for detailed examples.