|
5 | 5 | import pytest
|
6 | 6 | import respx
|
7 | 7 |
|
| 8 | +from typing import cast |
8 | 9 | import replicate
|
9 | 10 | from replicate.client import Client
|
10 | 11 | from replicate.exceptions import ModelError, ReplicateError
|
| 12 | +from replicate.stream import FileOutput |
11 | 13 |
|
12 | 14 |
|
13 | 15 | @pytest.mark.vcr("run.yaml")
|
@@ -73,7 +75,7 @@ async def test_run_concurrently(mock_replicate_api_token, record_mode):
|
73 | 75 | results = await asyncio.gather(*tasks)
|
74 | 76 | assert len(results) == len(prompts)
|
75 | 77 | assert all(isinstance(result, list) for result in results)
|
76 |
| - assert all(len(result) > 0 for result in results) |
| 78 | + assert all(len(results) > 0 for result in results) |
77 | 79 |
|
78 | 80 |
|
79 | 81 | @pytest.mark.vcr("run.yaml")
|
@@ -253,3 +255,174 @@ def prediction_with_status(status: str) -> dict:
|
253 | 255 | assert str(excinfo.value) == "OOM"
|
254 | 256 | assert excinfo.value.prediction.error == "OOM"
|
255 | 257 | assert excinfo.value.prediction.status == "failed"
|
| 258 | + |
| 259 | + |
| 260 | +@pytest.mark.asyncio |
| 261 | +async def test_run_with_file_output(mock_replicate_api_token): |
| 262 | + def prediction_with_status( |
| 263 | + status: str, output: str | list[str] | None = None |
| 264 | + ) -> dict: |
| 265 | + return { |
| 266 | + "id": "p1", |
| 267 | + "model": "test/example", |
| 268 | + "version": "v1", |
| 269 | + "urls": { |
| 270 | + "get": "https://api.replicate.com/v1/predictions/p1", |
| 271 | + "cancel": "https://api.replicate.com/v1/predictions/p1/cancel", |
| 272 | + }, |
| 273 | + "created_at": "2023-10-05T12:00:00.000000Z", |
| 274 | + "source": "api", |
| 275 | + "status": status, |
| 276 | + "input": {"text": "world"}, |
| 277 | + "output": output, |
| 278 | + "error": "OOM" if status == "failed" else None, |
| 279 | + "logs": "", |
| 280 | + } |
| 281 | + |
| 282 | + router = respx.Router(base_url="https://api.replicate.com/v1") |
| 283 | + router.route(method="POST", path="/predictions").mock( |
| 284 | + return_value=httpx.Response( |
| 285 | + 201, |
| 286 | + json=prediction_with_status("processing"), |
| 287 | + ) |
| 288 | + ) |
| 289 | + router.route(method="GET", path="/predictions/p1").mock( |
| 290 | + return_value=httpx.Response( |
| 291 | + 200, |
| 292 | + json=prediction_with_status( |
| 293 | + "succeeded", "https://api.replicate.com/v1/assets/output.txt" |
| 294 | + ), |
| 295 | + ) |
| 296 | + ) |
| 297 | + router.route( |
| 298 | + method="GET", |
| 299 | + path="/models/test/example/versions/v1", |
| 300 | + ).mock( |
| 301 | + return_value=httpx.Response( |
| 302 | + 201, |
| 303 | + json={ |
| 304 | + "id": "f2d6b24e6002f25f77ae89c2b0a5987daa6d0bf751b858b94b8416e8542434d1", |
| 305 | + "created_at": "2024-07-18T00:35:56.210272Z", |
| 306 | + "cog_version": "0.9.10", |
| 307 | + "openapi_schema": { |
| 308 | + "openapi": "3.0.2", |
| 309 | + }, |
| 310 | + }, |
| 311 | + ) |
| 312 | + ) |
| 313 | + router.route(method="GET", path="/assets/output.txt").mock( |
| 314 | + return_value=httpx.Response(200, content=b"Hello, world!") |
| 315 | + ) |
| 316 | + |
| 317 | + client = Client( |
| 318 | + api_token="test-token", transport=httpx.MockTransport(router.handler) |
| 319 | + ) |
| 320 | + client.poll_interval = 0.001 |
| 321 | + |
| 322 | + output = cast( |
| 323 | + FileOutput, |
| 324 | + client.run( |
| 325 | + "test/example:v1", |
| 326 | + input={ |
| 327 | + "text": "Hello, world!", |
| 328 | + }, |
| 329 | + use_file_output=True, |
| 330 | + ), |
| 331 | + ) |
| 332 | + |
| 333 | + assert output.url == "https://api.replicate.com/v1/assets/output.txt" |
| 334 | + |
| 335 | + assert output.read() == b"Hello, world!" |
| 336 | + for chunk in output: |
| 337 | + assert chunk == b"Hello, world!" |
| 338 | + |
| 339 | + assert await output.aread() == b"Hello, world!" |
| 340 | + async for chunk in output: |
| 341 | + assert chunk == b"Hello, world!" |
| 342 | + |
| 343 | + |
| 344 | +@pytest.mark.asyncio |
| 345 | +async def test_run_with_file_output_array(mock_replicate_api_token): |
| 346 | + def prediction_with_status( |
| 347 | + status: str, output: str | list[str] | None = None |
| 348 | + ) -> dict: |
| 349 | + return { |
| 350 | + "id": "p1", |
| 351 | + "model": "test/example", |
| 352 | + "version": "v1", |
| 353 | + "urls": { |
| 354 | + "get": "https://api.replicate.com/v1/predictions/p1", |
| 355 | + "cancel": "https://api.replicate.com/v1/predictions/p1/cancel", |
| 356 | + }, |
| 357 | + "created_at": "2023-10-05T12:00:00.000000Z", |
| 358 | + "source": "api", |
| 359 | + "status": status, |
| 360 | + "input": {"text": "world"}, |
| 361 | + "output": output, |
| 362 | + "error": "OOM" if status == "failed" else None, |
| 363 | + "logs": "", |
| 364 | + } |
| 365 | + |
| 366 | + router = respx.Router(base_url="https://api.replicate.com/v1") |
| 367 | + router.route(method="POST", path="/predictions").mock( |
| 368 | + return_value=httpx.Response( |
| 369 | + 201, |
| 370 | + json=prediction_with_status("processing"), |
| 371 | + ) |
| 372 | + ) |
| 373 | + router.route(method="GET", path="/predictions/p1").mock( |
| 374 | + return_value=httpx.Response( |
| 375 | + 200, |
| 376 | + json=prediction_with_status( |
| 377 | + "succeeded", |
| 378 | + [ |
| 379 | + "https://api.replicate.com/v1/assets/hello.txt", |
| 380 | + "https://api.replicate.com/v1/assets/world.txt", |
| 381 | + ], |
| 382 | + ), |
| 383 | + ) |
| 384 | + ) |
| 385 | + router.route( |
| 386 | + method="GET", |
| 387 | + path="/models/test/example/versions/v1", |
| 388 | + ).mock( |
| 389 | + return_value=httpx.Response( |
| 390 | + 201, |
| 391 | + json={ |
| 392 | + "id": "f2d6b24e6002f25f77ae89c2b0a5987daa6d0bf751b858b94b8416e8542434d1", |
| 393 | + "created_at": "2024-07-18T00:35:56.210272Z", |
| 394 | + "cog_version": "0.9.10", |
| 395 | + "openapi_schema": { |
| 396 | + "openapi": "3.0.2", |
| 397 | + }, |
| 398 | + }, |
| 399 | + ) |
| 400 | + ) |
| 401 | + router.route(method="GET", path="/assets/hello.txt").mock( |
| 402 | + return_value=httpx.Response(200, content=b"Hello,") |
| 403 | + ) |
| 404 | + router.route(method="GET", path="/assets/world.txt").mock( |
| 405 | + return_value=httpx.Response(200, content=b" world!") |
| 406 | + ) |
| 407 | + |
| 408 | + client = Client( |
| 409 | + api_token="test-token", transport=httpx.MockTransport(router.handler) |
| 410 | + ) |
| 411 | + client.poll_interval = 0.001 |
| 412 | + |
| 413 | + [output1, output2] = cast( |
| 414 | + list[FileOutput], |
| 415 | + client.run( |
| 416 | + "test/example:v1", |
| 417 | + input={ |
| 418 | + "text": "Hello, world!", |
| 419 | + }, |
| 420 | + use_file_output=True, |
| 421 | + ), |
| 422 | + ) |
| 423 | + |
| 424 | + assert output1.url == "https://api.replicate.com/v1/assets/hello.txt" |
| 425 | + assert output2.url == "https://api.replicate.com/v1/assets/world.txt" |
| 426 | + |
| 427 | + assert output1.read() == b"Hello," |
| 428 | + assert output2.read() == b" world!" |
0 commit comments