-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request: Add async support #74
Comments
+1 |
Created a PR to add support for this. cc/ @bfirsh |
@creatorrr How is the progress with #76? Do you need any help/assistance? |
It's ready for review, @0dminnimda |
Hi. Thanks for your job. Code in your branch is woked? |
This comment was marked as off-topic.
This comment was marked as off-topic.
1+ |
1+ |
Hi @creatorrr. Thank you for contributing #76. Thank you also for your patience, as this is a long time coming. I'm happy to report that async support landed in version 0.18.0. Here's an example of how to run multiple predictions concurrently and wait for their results: import asyncio
import replicate
# https://replicate.com/stability-ai/sdxl
model_version = "stability-ai/sdxl:39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b"
prompts = [
f"A chariot pulled by a team of {count} rainbow unicorns"
for count in ["two", "four", "six", "eight"]
]
async with asyncio.TaskGroup() as tg:
tasks = [
tg.create_task(replicate.async_run(model_version, input={"prompt": prompt}))
for prompt in prompts
]
results = await asyncio.gather(*tasks)
print(results) |
When using async_run, is it possible to run different model_versions with different prompts? Or is it only limited to one model_version to different prompts for now? |
@githubloader The example I shared uses the same model version for all predictions, but there's nothing to stop you from using different combinations of inputs and models and running them all in parallel. |
how does this work when reading images with
I tried a few variations of
I get either |
Hi @thomastraum. The async part of your code looks fine. I think the issue is that you're passing the raw, base64-encoded string to the image parameter. If you make it a data URI, that should work. image_string = f"data:,{base64.b64encode(image_content).decode('utf-8')}" |
Ah amazing. Thanks so much for helping a beginner |
Hi @mattt . Beginner question. I have been trying to test out
Appreciate any insights you can provide here. |
@PrateekRajan Hmm, the only reason I can think of is that your program is importing an old version of the client library. We have automated tests for |
Hi, |
This PR adds support for async operations to the Replicate client. Namespace operations like `predictions.list` and `models.create` now have async variants with the `async_` prefix (`predictions.async_list` and `models.async_create`). Here's an example of what that looks like in practice: ```python import replicate model = await replicate.models.async_get("stability-ai/sdxl") input = { "prompt": "A chariot pulled by a team of rainbow unicorns, driven by an astronaut, dramatic lighting", } output = await replicate.async_run(f"stability-ai/sdxl:{model.latest_version.id}", input) ``` <details> <summary>Output</summary> <img src="https://github.com/replicate/replicate-python/assets/7659/6927f8b4-5f92-495d-a87c-135f31aa1847"/> </details> One of the most common questions I hear is how to run a bunch of predictions in parallel. The async functionality provided by this PR makes it really straightforward: ```python import asyncio import replicate # https://replicate.com/stability-ai/sdxl model_version = "stability-ai/sdxl:39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b" prompts = [ f"A chariot pulled by a team of {count} rainbow unicorns" for count in ["two", "four", "six", "eight"] ] async with asyncio.TaskGroup() as tg: tasks = [ tg.create_task(replicate.async_run(model_version, input={"prompt": prompt})) for prompt in prompts ] results = await asyncio.gather(*tasks) print(results) ``` Under the hood, `Client` manages an `httpx.Client` and an `httpx.AsyncClient`, which handle calls to `_request` and `_async_request`, respectively. Both are created lazily, so API consumers using only sync or only async functionality won't be affected by functionality they aren't using. Implementation-wise, sync and async variants have separate code paths. This creates nontrivial amounts of duplication, but its benefits to clarity and performance justify those costs. For instance, it'd have been nice if the sync variant were implemented as a blocking call to the async variant, but that would require starting an event loop, which has additional overhead and causes problems if done within an existing event loop. Alternative to replicate/replicate-python#76 Resolves replicate/replicate-python#145 Resolves replicate/replicate-python#107 Resolves replicate/replicate-python#74 --------- Signed-off-by: Mattt Zmuda <mattt@replicate.com>
Async support (maybe via aiorequests/aiohttp?) would be very appreciated. Esp since some of the predictions can take a while to complete.
The text was updated successfully, but these errors were encountered: