-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunway_video_examples.py
More file actions
executable file
Β·226 lines (180 loc) Β· 7.36 KB
/
runway_video_examples.py
File metadata and controls
executable file
Β·226 lines (180 loc) Β· 7.36 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
224
225
226
#!/usr/bin/env python3
"""
Runway Video Generation Examples
This script demonstrates how to use the Runway video generation functionality
both directly with the processor and via the API endpoints.
"""
import json
import requests
import time
from pathlib import Path
# Configuration
API_BASE_URL = "http://localhost:8000"
EXAMPLE_IMAGE_URL = "https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=800"
def test_api_video_generation():
"""Test video generation via the API endpoint."""
print("π¬ Testing video generation via API...")
# Prepare request
payload = {
"image_url": EXAMPLE_IMAGE_URL,
"prompt_text": "A serene mountain landscape with clouds slowly drifting across the sky",
"ratio": "1280:720",
"duration": 5,
"model": "gen4_turbo"
}
try:
print(f"π€ Sending request to {API_BASE_URL}/generate-video")
print(f" Image: {payload['image_url']}")
print(f" Prompt: {payload['prompt_text']}")
print(f" Duration: {payload['duration']}s")
response = requests.post(
f"{API_BASE_URL}/generate-video",
json=payload,
headers={"Content-Type": "application/json"},
timeout=360 # 6 minutes timeout for video generation
)
if response.status_code == 200:
result = response.json()
print("β
Video generation successful!")
print(f" Status: {result['status']}")
print(f" Task ID: {result.get('task_id', 'N/A')}")
print(f" Video URL: {result.get('video_url', 'N/A')}")
print(f" Processing time: {result['processing_time_seconds']:.1f}s")
# Save result to file
timestamp = time.strftime("%Y%m%d_%H%M%S")
output_file = f"runway_video_result_{timestamp}.json"
with open(output_file, 'w') as f:
json.dump(result, f, indent=2)
print(f"π Result saved to: {output_file}")
else:
print(f"β Request failed with status {response.status_code}")
print(f" Error: {response.text}")
except requests.exceptions.Timeout:
print("β° Request timed out - video generation may still be processing")
except requests.exceptions.RequestException as e:
print(f"β Request failed: {e}")
def test_direct_processor():
"""Test video generation using the processor directly."""
print("π¬ Testing video generation via direct processor...")
try:
# Import the necessary components
from src.text_extractor import TextExtractor
from src.config import Config
# Check if Runway API key is configured
if not Config.RUNWAY_API_KEY:
print("β RUNWAY_API_KEY not configured. Please set it in your .env file.")
return
# Initialize extractor
extractor = TextExtractor()
if not hasattr(extractor, 'runway_processor') or not extractor.runway_processor:
print("β Runway processor not available. Please check your configuration.")
return
print("β
Runway processor initialized")
# Generate video
result_json = extractor.generate_video_from_image(
image_path=EXAMPLE_IMAGE_URL,
prompt_text="A peaceful ocean scene with gentle waves and seabirds",
ratio="1280:720",
duration=5
)
result = json.loads(result_json)
print("β
Video generation completed!")
print(f" Status: {result['status']}")
print(f" Video URL: {result.get('video_url', 'N/A')}")
print(f" Processing time: {result.get('processing_time_seconds', 0):.1f}s")
# Save result
timestamp = time.strftime("%Y%m%d_%H%M%S")
output_file = f"runway_direct_result_{timestamp}.json"
with open(output_file, 'w') as f:
json.dump(result, f, indent=2)
print(f"π Result saved to: {output_file}")
except ImportError as e:
print(f"β Import error: {e}")
print(" Make sure you're running from the project root directory")
except Exception as e:
print(f"β Error: {e}")
def test_health_check():
"""Test if the API is running and healthy."""
print("π Checking API health...")
try:
response = requests.get(f"{API_BASE_URL}/health", timeout=10)
if response.status_code == 200:
health = response.json()
print("β
API is healthy")
print(f" Status: {health['status']}")
print(f" Version: {health['version']}")
# Check API keys
api_keys = health.get('api_keys_configured', {})
print(f" OpenAI configured: {api_keys.get('openai', False)}")
print(f" Gemini configured: {api_keys.get('gemini', False)}")
return True
else:
print(f"β API health check failed: {response.status_code}")
return False
except requests.exceptions.RequestException as e:
print(f"β Cannot connect to API: {e}")
print(f" Make sure the API server is running at {API_BASE_URL}")
return False
def generate_curl_examples():
"""Generate curl command examples for video generation."""
print("π CURL Command Examples for Video Generation:")
print()
# Basic example
print("# Basic video generation:")
curl_cmd = f'''curl -X POST "{API_BASE_URL}/generate-video" \\
-H "Content-Type: application/json" \\
-d '{{
"image_url": "{EXAMPLE_IMAGE_URL}",
"prompt_text": "A serene mountain landscape with clouds slowly moving",
"ratio": "1280:720",
"duration": 5
}}\''''
print(curl_cmd)
print()
# Advanced example
print("# Advanced video generation with custom settings:")
curl_cmd_advanced = f'''curl -X POST "{API_BASE_URL}/generate-video" \\
-H "Content-Type: application/json" \\
-d '{{
"image_url": "https://your-image-url.com/image.jpg",
"prompt_text": "A dynamic urban cityscape with traffic and people moving",
"ratio": "1024:1024",
"duration": 8,
"model": "gen4_turbo"
}}\''''
print(curl_cmd_advanced)
print()
# Save to file
with open("runway_curl_examples.sh", 'w') as f:
f.write("#!/bin/bash\n")
f.write("# Runway Video Generation CURL Examples\n\n")
f.write("# Basic video generation\n")
f.write(curl_cmd + "\n\n")
f.write("# Advanced video generation\n")
f.write(curl_cmd_advanced + "\n")
print("π CURL examples saved to: runway_curl_examples.sh")
def main():
"""Main function to run all tests."""
print("π Runway Video Generation Test Suite")
print("=" * 50)
# Test API health first
if test_health_check():
print()
# Test API endpoint
test_api_video_generation()
print()
# Generate curl examples
generate_curl_examples()
print()
# Test direct processor (works even if API is down)
test_direct_processor()
print()
print("π Test suite completed!")
print()
print("π Notes:")
print(" - Video generation can take 1-5 minutes depending on complexity")
print(" - Make sure your RUNWAY_API_KEY is set in your .env file")
print(" - The API server needs to be running for endpoint tests")
print(" - Generated videos will be available at the returned URL")
if __name__ == "__main__":
main()