|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Example of using the experimental replicate.use() interface |
| 4 | +""" |
| 5 | + |
| 6 | +import replicate |
| 7 | + |
| 8 | +print("Testing replicate.use() functionality...") |
| 9 | + |
| 10 | +# Test 1: Simple text model |
| 11 | +print("\n1. Testing simple text model...") |
| 12 | +try: |
| 13 | + hello_world = replicate.use("replicate/hello-world") |
| 14 | + result = hello_world(text="Alice") |
| 15 | + print(f"Result: {result}") |
| 16 | +except Exception as e: |
| 17 | + print(f"Error: {type(e).__name__}: {e}") |
| 18 | + |
| 19 | +# Test 2: Image generation model |
| 20 | +print("\n2. Testing image generation model...") |
| 21 | +try: |
| 22 | + from replicate.lib._predictions_use import get_path_url |
| 23 | + |
| 24 | + flux_dev = replicate.use("black-forest-labs/flux-dev") |
| 25 | + outputs = flux_dev( |
| 26 | + prompt="a cat wearing a wizard hat, digital art", |
| 27 | + num_outputs=1, |
| 28 | + aspect_ratio="1:1", |
| 29 | + output_format="webp", |
| 30 | + guidance=3.5, |
| 31 | + num_inference_steps=28, |
| 32 | + ) |
| 33 | + print(f"Generated output: {outputs}") |
| 34 | + if isinstance(outputs, list): |
| 35 | + print(f"Generated {len(outputs)} image(s)") |
| 36 | + for i, output in enumerate(outputs): |
| 37 | + print(f" Image {i}: {output}") |
| 38 | + # Get the URL without downloading |
| 39 | + url = get_path_url(output) |
| 40 | + if url: |
| 41 | + print(f" URL: {url}") |
| 42 | + else: |
| 43 | + print(f"Single output: {outputs}") |
| 44 | + url = get_path_url(outputs) |
| 45 | + if url: |
| 46 | + print(f" URL: {url}") |
| 47 | +except Exception as e: |
| 48 | + print(f"Error: {type(e).__name__}: {e}") |
| 49 | + import traceback |
| 50 | + |
| 51 | + traceback.print_exc() |
| 52 | + |
| 53 | +# Test 3: Language model with streaming |
| 54 | +print("\n3. Testing language model with streaming...") |
| 55 | +try: |
| 56 | + llama = replicate.use("meta/meta-llama-3-8b-instruct", streaming=True) |
| 57 | + output = llama(prompt="Write a haiku about Python programming", max_tokens=50) |
| 58 | + print("Streaming output:") |
| 59 | + for chunk in output: |
| 60 | + print(chunk, end="", flush=True) |
| 61 | + print() |
| 62 | +except Exception as e: |
| 63 | + print(f"Error: {type(e).__name__}: {e}") |
| 64 | + import traceback |
| 65 | + |
| 66 | + traceback.print_exc() |
| 67 | + |
| 68 | +# Test 4: Using async |
| 69 | +print("\n4. Testing async functionality...") |
| 70 | +import asyncio |
| 71 | + |
| 72 | + |
| 73 | +async def test_async(): |
| 74 | + try: |
| 75 | + hello_world = replicate.use("replicate/hello-world", use_async=True) |
| 76 | + result = await hello_world(text="Bob") |
| 77 | + print(f"Async result: {result}") |
| 78 | + |
| 79 | + print("\n4b. Testing async streaming...") |
| 80 | + llama = replicate.use("meta/meta-llama-3-8b-instruct", streaming=True, use_async=True) |
| 81 | + output = await llama(prompt="Write a short poem about async/await", max_tokens=50) |
| 82 | + print("Async streaming output:") |
| 83 | + async for chunk in output: |
| 84 | + print(chunk, end="", flush=True) |
| 85 | + print() |
| 86 | + except Exception as e: |
| 87 | + print(f"Error: {type(e).__name__}: {e}") |
| 88 | + import traceback |
| 89 | + |
| 90 | + traceback.print_exc() |
| 91 | + |
| 92 | + |
| 93 | +asyncio.run(test_async()) |
| 94 | + |
| 95 | +print("\nDone!") |
0 commit comments