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