Skip to content

Commit 6c3e813

Browse files
GothReigenmattt
authored andcommitted
Add base64 example
Co-authored-by: GothReigen <[email protected]> Signed-off-by: Mattt Zmuda <[email protected]>
1 parent bd2c225 commit 6c3e813

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,34 @@ Some models, like [methexis-inc/img2prompt](https://replicate.com/methexis-inc/i
7979
> print(results)
8080
> ```
8181
82+
To run a model that takes a file input, pass a URL to a publicly accessible file. Or, for smaller files (<10MB), you can convert file data into a base64-encoded data URI and pass that directly:
83+
84+
```
85+
# Run andreasjansson/blip-2:f677695e by providing a base64 encoded image from local file system
86+
import replicate
87+
import base64
88+
89+
# Download `gg_bridge.jpeg` from https://replicate.delivery/pbxt/IJEPmgAlL2zNBNDoRRKFegTEcxnlRhoQxlNjPHSZEy0pSIKn/gg_bridge.jpeg
90+
# This reads example image `gg_bridge.jpeg` from current working directory into a buffer
91+
with open("gg_bridge.jpeg", "rb") as image_file:
92+
# Then encodes the buffer into a base64 string
93+
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
94+
# Finally, sets the MIME type and adds the data URI scheme to the encoded string
95+
encoded_string = f"data:image/jpeg;base64,{encoded_string}"
96+
97+
output = replicate.run(
98+
"andreasjansson/blip-2:f677695e5e89f8b236e52ecd1d3f01beb44c34606419bcc19345e046d8f786f9",
99+
input={
100+
"image": encoded_string,
101+
"caption": False,
102+
"question": "what body of water does this bridge cross?",
103+
"temperature": 1,
104+
"use_nucleus_sampling": False
105+
}
106+
)
107+
print(output)
108+
```
109+
82110
## Run a model and stream its output
83111
84112
Replicate’s API supports server-sent event streams (SSEs) for language models.

0 commit comments

Comments
 (0)