Skip to content

Commit cea2cb0

Browse files
committed
chore: document run() in readme
1 parent b6608ca commit cea2cb0

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,39 @@ asyncio.run(main())
6868

6969
Functionality between the synchronous and asynchronous clients is otherwise identical.
7070

71+
## Running models
72+
73+
```python
74+
import replicate
75+
76+
output = replicate.run(
77+
"black-forest-labs/flux-schnell", input={"prompt": "astronaut riding a rocket like a horse"}
78+
)
79+
80+
# Write the output to a file
81+
for index, item in enumerate(output):
82+
with open(f"output_{index}.webp", "wb") as file:
83+
file.write(item.read())
84+
```
85+
86+
`replicate.run()` raises `ModelError` if the prediction fails. You can access the exception's `prediction` property to get more information about the failure:
87+
88+
```python
89+
import replicate
90+
from replicate import ModelError
91+
92+
try:
93+
output = replicate.run(
94+
"stability-ai/stable-diffusion-3", input={"prompt": "An astronaut riding a rainbow unicorn"}
95+
)
96+
except ModelError as e:
97+
if "(some known issue)" in e.prediction.logs:
98+
pass
99+
print("Failed prediction: " + e.prediction.id)
100+
```
101+
102+
By default the client will wait up to 60 seconds for the prediction to complete. The timeout can be configured by passing `wait=x` where `x` is a timeout in seconds between 1 and 60. To disable waiting, pass `wait=False`.
103+
71104
## Using types
72105

73106
Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev) which also provide helper methods for things like:

src/replicate/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from .lib._files import FileOutput as FileOutput, AsyncFileOutput as AsyncFileOutput
2727
from ._exceptions import (
2828
APIError,
29+
ModelError,
2930
ConflictError,
3031
NotFoundError,
3132
APIStatusError,
@@ -68,6 +69,7 @@
6869
"UnprocessableEntityError",
6970
"RateLimitError",
7071
"InternalServerError",
72+
"ModelError",
7173
"Timeout",
7274
"RequestOptions",
7375
"Client",

0 commit comments

Comments
 (0)