Skip to content

Commit 1f441a2

Browse files
author
Replicate Staff
authored
Update example to use files.py for base64 encode
1 parent 6c3e813 commit 1f441a2

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

README.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,23 @@ Some models, like [methexis-inc/img2prompt](https://replicate.com/methexis-inc/i
8282
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:
8383
8484
```
85-
# Run andreasjansson/blip-2:f677695e by providing a base64 encoded image from local file system
85+
# Run andreasjansson/blip-2:f677695e by providing an image from local file system which is base64 encoded by files.py
8686
import replicate
8787
import base64
8888
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
89+
# This reads an example image `gg_bridge.jpeg` from current working directory into a buffer and into the prediction request
9190
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)
91+
output = replicate.run(
92+
"andreasjansson/blip-2:f677695e5e89f8b236e52ecd1d3f01beb44c34606419bcc19345e046d8f786f9",
93+
input={
94+
"image": image_file,
95+
"caption": False,
96+
"question": "what body of water does this bridge cross?",
97+
"temperature": 1,
98+
"use_nucleus_sampling": False
99+
}
100+
)
101+
print(output)
108102
```
109103
110104
## Run a model and stream its output

0 commit comments

Comments
 (0)