-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_usage.py
More file actions
223 lines (173 loc) · 7.61 KB
/
basic_usage.py
File metadata and controls
223 lines (173 loc) · 7.61 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python3
"""
Basic usage examples for Seedance SDK
"""
import asyncio
import os
from seedance import SeedanceClient, Model, Resolution, create_progress_callback
async def basic_async_example():
"""Basic async example"""
print("=== Basic Async Example ===")
# Initialize client
api_key = os.getenv("SEEDANCE_API_KEY", "sk-video-xxxxx")
async with SeedanceClient(api_key) as client:
try:
# Generate video
print("Generating video...")
task = await client.generate_video(
prompt="A cinematic aerial shot of a mountain range at sunrise, golden light, volumetric clouds",
model=Model.SEEDANCE_2_0,
duration=8,
resolution=Resolution.P1080P,
generate_audio=True
)
print(f"Task created: {task.id}")
print(f"Estimated time: {task.estimated_time}s")
# Wait for completion with progress
progress_callback = create_progress_callback(verbose=True)
result = await client.wait_for_completion(
task.id,
timeout=300, # 5 minutes timeout
on_progress=progress_callback
)
print(f"\n✅ Video ready!")
print(f"URL: {result.result.video_url}")
print(f"Duration: {result.result.duration}s")
print(f"Resolution: {result.result.resolution}")
print(f"File size: {result.result.file_size} bytes")
except Exception as e:
print(f"❌ Error: {e}")
def basic_sync_example():
"""Basic sync example"""
print("\n=== Basic Sync Example ===")
api_key = os.getenv("SEEDANCE_API_KEY", "sk-video-xxxxx")
with SeedanceClient(api_key) as client:
try:
# Generate video
print("Generating video...")
task = client.generate_video_sync(
prompt="A futuristic city at night with neon lights reflecting on wet streets",
model=Model.SEEDANCE_2_0,
duration=4,
resolution=Resolution.P720P,
generate_audio=False
)
print(f"Task created: {task.id}")
# Wait for completion
result = client.wait_for_completion_sync(task.id, timeout=300)
print(f"✅ Video ready!")
print(f"URL: {result.result.video_url}")
except Exception as e:
print(f"❌ Error: {e}")
async def batch_generation_example():
"""Batch generation example"""
print("\n=== Batch Generation Example ===")
api_key = os.getenv("SEEDANCE_API_KEY", "sk-video-xxxxx")
async with SeedanceClient(api_key) as client:
prompts = [
"A cat playing piano in a cozy room",
"A dog surfing on a big wave",
"A robot cooking in a modern kitchen"
]
tasks = []
# Submit all tasks
for i, prompt in enumerate(prompts):
try:
task = await client.generate_video(
prompt=prompt,
model=Model.SEEDANCE_2_0,
duration=4,
resolution=Resolution.P720P
)
tasks.append(task)
print(f"Task {i+1} submitted: {task.id}")
except Exception as e:
print(f"❌ Task {i+1} failed: {e}")
# Wait for all tasks to complete
print(f"\nWaiting for {len(tasks)} tasks to complete...")
results = []
for i, task in enumerate(tasks):
try:
result = await client.wait_for_completion(task.id, timeout=300)
results.append(result)
print(f"✅ Task {i+1} completed: {result.result.video_url}")
except Exception as e:
print(f"❌ Task {i+1} failed: {e}")
print(f"\n{len(results)}/{len(tasks)} videos generated successfully")
async def image_to_video_example():
"""Image-to-video example"""
print("\n=== Image-to-Video Example ===")
api_key = os.getenv("SEEDANCE_API_KEY", "sk-video-xxxxx")
async with SeedanceClient(api_key) as client:
try:
# Generate video from image
task = await client.generate_video(
prompt="The person slowly turns and smiles at the camera",
image_urls=["https://example.com/portrait.jpg"],
model=Model.KLING_V2_6,
duration=5,
resolution=Resolution.P1080P
)
print(f"Image-to-video task created: {task.id}")
result = await client.wait_for_completion(task.id, timeout=300)
print(f"✅ Video ready: {result.result.video_url}")
except Exception as e:
print(f"❌ Error: {e}")
async def task_management_example():
"""Task management example"""
print("\n=== Task Management Example ===")
api_key = os.getenv("SEEDANCE_API_KEY", "sk-video-xxxxx")
async with SeedanceClient(api_key) as client:
try:
# Get credits info
credits = await client.get_credits()
print(f"Credits: {credits.remaining}/{credits.total}")
# List recent tasks
tasks_response = await client.list_tasks(page=1, limit=5)
print(f"\nRecent tasks ({len(tasks_response.data.tasks)}):")
for task in tasks_response.data.tasks:
print(f" {task.id}: {task.status.value} - {task.model}")
# Get specific task details
if tasks_response.data.tasks:
first_task = tasks_response.data.tasks[0]
task_detail = await client.get_task(first_task.id)
print(f"\nTask details for {task_detail.id}:")
print(f" Status: {task_detail.status.value}")
print(f" Created: {task_detail.created_at}")
if task_detail.credits_consumed:
print(f" Credits consumed: {task_detail.credits_consumed}")
except Exception as e:
print(f"❌ Error: {e}")
def cost_estimation_example():
"""Cost estimation example"""
print("\n=== Cost Estimation Example ===")
from seedance import calculate_estimated_cost
scenarios = [
("seedance-2.0", 4, "720p", False),
("seedance-2.0", 8, "1080p", True),
("seedance-2.0", 12, "1080p", True),
("kling-v2-6", 5, "1080p", False),
("kling-v2-6", 10, "1080p", False),
("sora-2", 10, "1080p", False),
]
print("Cost estimation for different scenarios:")
print("Model\t\tDuration\tResolution\tAudio\tCredits")
print("-" * 60)
for model, duration, resolution, with_audio in scenarios:
credits = calculate_estimated_cost(model, duration, resolution, with_audio)
print(f"{model}\t{duration}s\t\t{resolution}\t{with_audio}\t{credits}")
if __name__ == "__main__":
print("Seedance SDK Examples")
print("=" * 50)
# Run examples
asyncio.run(basic_async_example())
basic_sync_example()
asyncio.run(batch_generation_example())
asyncio.run(image_to_video_example())
asyncio.run(task_management_example())
cost_estimation_example()
print("\n" + "=" * 50)
print("Examples completed!")
print("\nTo run with your real API key:")
print("export SEEDANCE_API_KEY=sk-video-your-real-key")
print("python examples/basic_usage.py")