Skip to content

Commit 4fdd78f

Browse files
authored
Remove mention of data URLs from README (#391)
This commit incorporates the changes from #386 as well as removing mention of the data URLs added in 86c5e14.
1 parent 86c5e14 commit 4fdd78f

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

README.md

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22

33
This is a Python client for [Replicate](https://replicate.com). It lets you run models from your Python code or Jupyter notebook, and do various other things on Replicate.
44

5+
## Breaking Changes in 1.0.0
6+
7+
The 1.0.0 release contains breaking changes:
8+
9+
- The `replicate.run()` method now returns `FileOutput`s instead of URL strings by default for models that output files. `FileOutput` implements an iterable interface similar to `httpx.Response`, making it easier to work with files efficiently.
10+
11+
To revert to the previous behavior, you can opt out of `FileOutput` by passing `use_file_output=False` to `replicate.run()`:
12+
13+
```python
14+
output = replicate.run("acmecorp/acme-model", use_file_output=False)
15+
```
16+
17+
In most cases, updating existing applications to call `output.url` should resolve any issues. But we recommend using the `FileOutput` objects directly as we have further improvements planned to this API and this approach is guaranteed to give the fastest results.
18+
19+
> [!TIP]
520
> **👋** Check out an interactive version of this tutorial on [Google Colab](https://colab.research.google.com/drive/1K91q4p-OhL96FHBAVLsv9FlwFdu6Pn3c).
621
>
722
> [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1K91q4p-OhL96FHBAVLsv9FlwFdu6Pn3c)
@@ -30,17 +45,18 @@ We recommend not adding the token directly to your source code, because you don'
3045

3146
## Run a model
3247

33-
Create a new Python file and add the following code,
34-
replacing the model identifier and input with your own:
48+
Create a new Python file and add the following code, replacing the model identifier and input with your own:
3549

3650
```python
3751
>>> import replicate
38-
>>> replicate.run(
39-
"stability-ai/stable-diffusion:27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
40-
input={"prompt": "a 19th century portrait of a wombat gentleman"}
52+
>>> outputs = replicate.run(
53+
"black-forest-labs/flux-schnell",
54+
input={"prompt": "astronaut riding a rocket like a horse"}
4155
)
42-
4356
[<replicate.helpers.FileOutput object at 0x107179b50>]
57+
>>> for index, output in enumerate(outputs):
58+
with open(f"output_{index}.webp", "wb") as file:
59+
file.write(output.read())
4460
```
4561

4662
`replicate.run` raises `ModelError` if the prediction fails.
@@ -63,12 +79,10 @@ except ModelError as e
6379
> [!NOTE]
6480
> By default the Replicate client will hold the connection open for up to 60 seconds while waiting
6581
> for the prediction to complete. This is designed to optimize getting the model output back to the
66-
> client as quickly as possible. For models that output files the file data will be inlined into
67-
> the response as a data-uri.
82+
> client as quickly as possible.
6883
>
6984
> The timeout can be configured by passing `wait=x` to `replicate.run()` where `x` is a timeout
70-
> in seconds between 1 and 60. To disable the sync mode and the data-uri response you can pass
71-
> `wait=False` to `replicate.run()`.
85+
> in seconds between 1 and 60. To disable the sync mode you can pass `wait=False`.
7286
7387
## AsyncIO support
7488

@@ -152,7 +166,7 @@ For more information, see
152166

153167
## Run a model in the background
154168

155-
You can start a model and run it in the background:
169+
You can start a model and run it in the background using async mode:
156170

157171
```python
158172
>>> model = replicate.models.get("kvfrans/clipdraw")
@@ -187,6 +201,9 @@ iteration: 30, render:loss: -1.3994140625
187201

188202
>>> prediction.output
189203
<replicate.helpers.FileOutput object at 0x107179b50>
204+
205+
>>> with open("output.png", "wb") as file:
206+
file.write(prediction.output.read())
190207
```
191208

192209
## Run a model in the background and get a webhook
@@ -295,19 +312,12 @@ background = Image.open(output[0])
295312

296313
### FileOutput
297314

298-
Is a file-like object returned from the `replicate.run()` method that makes it easier to work with models
299-
that output files. It implements `Iterator` and `AsyncIterator` for reading the file data in chunks as well
300-
as `read` and `aread()` to read the entire file into memory.
301-
302-
Lastly, the underlying datasource is available on the `url` attribute.
315+
Is a [file-like](https://docs.python.org/3/glossary.html#term-file-object) object returned from the `replicate.run()` method that makes it easier to work with models that output files. It implements `Iterator` and `AsyncIterator` for reading the file data in chunks as well as `read()` and `aread()` to read the entire file into memory.
303316

304317
> [!NOTE]
305-
> The `url` attribute can vary between a remote URL and a data-uri depending on whether the server has
306-
> optimized the request. For small files <5mb using the syncronous API data-uris will be returned to
307-
> remove the need to make subsequent requests for the file data. To disable this pass `wait=false`
308-
> to the replicate.run() function.
318+
> It is worth noting that at this time `read()` and `aread()` do not currently accept a `size` argument to read up to `size` bytes.
309319
310-
To access the file URL:
320+
Lastly, the URL of the underlying data source is available on the `url` attribute though we recommend you use the object as an iterator or use its `read()` or `aread()` methods, as the `url` property may not always return HTTP URLs in future.
311321

312322
```python
313323
print(output.url) #=> "data:image/png;base64,xyz123..." or "https://delivery.replicate.com/..."
@@ -439,13 +449,9 @@ Here's how to list of all the available hardware for running models on Replicate
439449

440450
## Fine-tune a model
441451

442-
Use the [training API](https://replicate.com/docs/fine-tuning)
443-
to fine-tune models to make them better at a particular task.
444-
To see what **language models** currently support fine-tuning,
445-
check out Replicate's [collection of trainable language models](https://replicate.com/collections/trainable-language-models).
452+
Use the [training API](https://replicate.com/docs/fine-tuning) to fine-tune models to make them better at a particular task. To see what **language models** currently support fine-tuning, check out Replicate's [collection of trainable language models](https://replicate.com/collections/trainable-language-models).
446453

447-
If you're looking to fine-tune **image models**,
448-
check out Replicate's [guide to fine-tuning image models](https://replicate.com/docs/guides/fine-tune-an-image-model).
454+
If you're looking to fine-tune **image models**, check out Replicate's [guide to fine-tuning image models](https://replicate.com/docs/guides/fine-tune-an-image-model).
449455

450456
Here's how to fine-tune a model on Replicate:
451457

@@ -467,24 +473,19 @@ training = replicate.trainings.create(
467473

468474
## Customize client behavior
469475

470-
The `replicate` package exports a default shared client.
471-
This client is initialized with an API token
472-
set by the `REPLICATE_API_TOKEN` environment variable.
476+
The `replicate` package exports a default shared client. This client is initialized with an API token set by the `REPLICATE_API_TOKEN` environment variable.
473477

474-
You can create your own client instance to
475-
pass a different API token value,
476-
add custom headers to requests,
477-
or control the behavior of the underlying [HTTPX client](https://www.python-httpx.org/api/#client):
478+
You can create your own client instance to pass a different API token value, add custom headers to requests, or control the behavior of the underlying [HTTPX client](https://www.python-httpx.org/api/#client):
478479

479480
```python
480481
import os
481482
from replicate.client import Client
482483

483484
replicate = Client(
484-
api_token=os.environ["SOME_OTHER_REPLICATE_API_TOKEN"]
485-
headers={
486-
"User-Agent": "my-app/1.0"
487-
}
485+
api_token=os.environ["SOME_OTHER_REPLICATE_API_TOKEN"]
486+
headers={
487+
"User-Agent": "my-app/1.0"
488+
}
488489
)
489490
```
490491

0 commit comments

Comments
 (0)