runapi-hailuo 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- runapi_hailuo-0.1.0/.gitignore +29 -0
- runapi_hailuo-0.1.0/PKG-INFO +84 -0
- runapi_hailuo-0.1.0/README.md +71 -0
- runapi_hailuo-0.1.0/pyproject.toml +30 -0
- runapi_hailuo-0.1.0/src/runapi/hailuo/__init__.py +24 -0
- runapi_hailuo-0.1.0/src/runapi/hailuo/client.py +29 -0
- runapi_hailuo-0.1.0/src/runapi/hailuo/py.typed +0 -0
- runapi_hailuo-0.1.0/src/runapi/hailuo/resources/__init__.py +4 -0
- runapi_hailuo-0.1.0/src/runapi/hailuo/resources/image_to_video.py +102 -0
- runapi_hailuo-0.1.0/src/runapi/hailuo/resources/text_to_video.py +77 -0
- runapi_hailuo-0.1.0/src/runapi/hailuo/types.py +38 -0
- runapi_hailuo-0.1.0/tests/test_client.py +214 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Build artifacts
|
|
2
|
+
dist/
|
|
3
|
+
build/
|
|
4
|
+
*.egg-info/
|
|
5
|
+
*.egg
|
|
6
|
+
|
|
7
|
+
# Bytecode
|
|
8
|
+
__pycache__/
|
|
9
|
+
*.py[cod]
|
|
10
|
+
|
|
11
|
+
# Virtual environments
|
|
12
|
+
.venv/
|
|
13
|
+
venv/
|
|
14
|
+
|
|
15
|
+
# uv
|
|
16
|
+
uv.lock
|
|
17
|
+
|
|
18
|
+
# Test / type caches
|
|
19
|
+
.pytest_cache/
|
|
20
|
+
.mypy_cache/
|
|
21
|
+
.ruff_cache/
|
|
22
|
+
.coverage
|
|
23
|
+
htmlcov/
|
|
24
|
+
|
|
25
|
+
# IDE / OS
|
|
26
|
+
.idea/
|
|
27
|
+
.vscode/
|
|
28
|
+
*.swp
|
|
29
|
+
.DS_Store
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: runapi-hailuo
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Hailuo text-to-video and image-to-video client for RunAPI
|
|
5
|
+
Project-URL: Homepage, https://runapi.ai/models/hailuo
|
|
6
|
+
Project-URL: Documentation, https://runapi.ai/docs#sdk-hailuo
|
|
7
|
+
Author-email: RunAPI <contact@runapi.ai>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
Keywords: ai,hailuo,image-to-video,runapi,sdk,text-to-video
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Requires-Dist: runapi-core
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# Hailuo Python SDK for RunAPI
|
|
15
|
+
|
|
16
|
+
The Hailuo Python SDK is the language-specific package for Hailuo on RunAPI. Use
|
|
17
|
+
it for text-to-video and image-to-video flows when your application needs JSON
|
|
18
|
+
request bodies, task status lookup, and consistent RunAPI errors in Python.
|
|
19
|
+
|
|
20
|
+
For model details, use https://runapi.ai/models/hailuo; for API reference, use
|
|
21
|
+
https://runapi.ai/docs#hailuo; for SDK docs, use https://runapi.ai/docs#sdk-hailuo.
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install runapi-hailuo
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick start
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from runapi.hailuo import HailuoClient
|
|
33
|
+
|
|
34
|
+
client = HailuoClient() # reads RUNAPI_API_KEY, or pass api_key="sk-..."
|
|
35
|
+
|
|
36
|
+
task = client.text_to_video.create(
|
|
37
|
+
model="hailuo-02-text-to-video-standard",
|
|
38
|
+
prompt="A cinematic drone shot over a coastal city at dusk",
|
|
39
|
+
)
|
|
40
|
+
status = client.text_to_video.get(task.id)
|
|
41
|
+
|
|
42
|
+
video = client.image_to_video.create(
|
|
43
|
+
model="hailuo-2.3-image-to-video-standard",
|
|
44
|
+
prompt="Slowly zoom in as the waves roll",
|
|
45
|
+
first_frame_image_url="https://example.com/source.jpg",
|
|
46
|
+
)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Use `create` to submit a task and return quickly, `get` to fetch the latest task
|
|
50
|
+
state, and `run` to create and poll until completion:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
result = client.text_to_video.run(
|
|
54
|
+
model="hailuo-02-text-to-video-standard",
|
|
55
|
+
prompt="A futuristic cityscape at dusk",
|
|
56
|
+
)
|
|
57
|
+
print(result.videos[0].url)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
In web request handlers, prefer `create` plus webhook or later `get` polling so a
|
|
61
|
+
worker is not held open.
|
|
62
|
+
|
|
63
|
+
RunAPI-generated file URLs are temporary. Download and store generated videos in
|
|
64
|
+
your own durable storage within 7 days; do not treat returned URLs as long-term
|
|
65
|
+
assets.
|
|
66
|
+
|
|
67
|
+
## Language notes
|
|
68
|
+
|
|
69
|
+
Pass parameters as keyword arguments and catch the `runapi.hailuo` error classes
|
|
70
|
+
when building video jobs or scripts. The available resources are `text_to_video`
|
|
71
|
+
and `image_to_video`. Keep `RUNAPI_API_KEY` in the environment or your secret
|
|
72
|
+
manager; never commit API keys or callback secrets.
|
|
73
|
+
|
|
74
|
+
## Links
|
|
75
|
+
|
|
76
|
+
- Model page: https://runapi.ai/models/hailuo
|
|
77
|
+
- SDK docs: https://runapi.ai/docs#sdk-hailuo
|
|
78
|
+
- Product docs: https://runapi.ai/docs#hailuo
|
|
79
|
+
- Pricing and rate limits: https://runapi.ai/models/hailuo/02-text-to-video-pro
|
|
80
|
+
- Full catalog: https://runapi.ai/models
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
Licensed under the Apache License, Version 2.0.
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# Hailuo Python SDK for RunAPI
|
|
2
|
+
|
|
3
|
+
The Hailuo Python SDK is the language-specific package for Hailuo on RunAPI. Use
|
|
4
|
+
it for text-to-video and image-to-video flows when your application needs JSON
|
|
5
|
+
request bodies, task status lookup, and consistent RunAPI errors in Python.
|
|
6
|
+
|
|
7
|
+
For model details, use https://runapi.ai/models/hailuo; for API reference, use
|
|
8
|
+
https://runapi.ai/docs#hailuo; for SDK docs, use https://runapi.ai/docs#sdk-hailuo.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install runapi-hailuo
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Quick start
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
from runapi.hailuo import HailuoClient
|
|
20
|
+
|
|
21
|
+
client = HailuoClient() # reads RUNAPI_API_KEY, or pass api_key="sk-..."
|
|
22
|
+
|
|
23
|
+
task = client.text_to_video.create(
|
|
24
|
+
model="hailuo-02-text-to-video-standard",
|
|
25
|
+
prompt="A cinematic drone shot over a coastal city at dusk",
|
|
26
|
+
)
|
|
27
|
+
status = client.text_to_video.get(task.id)
|
|
28
|
+
|
|
29
|
+
video = client.image_to_video.create(
|
|
30
|
+
model="hailuo-2.3-image-to-video-standard",
|
|
31
|
+
prompt="Slowly zoom in as the waves roll",
|
|
32
|
+
first_frame_image_url="https://example.com/source.jpg",
|
|
33
|
+
)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Use `create` to submit a task and return quickly, `get` to fetch the latest task
|
|
37
|
+
state, and `run` to create and poll until completion:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
result = client.text_to_video.run(
|
|
41
|
+
model="hailuo-02-text-to-video-standard",
|
|
42
|
+
prompt="A futuristic cityscape at dusk",
|
|
43
|
+
)
|
|
44
|
+
print(result.videos[0].url)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
In web request handlers, prefer `create` plus webhook or later `get` polling so a
|
|
48
|
+
worker is not held open.
|
|
49
|
+
|
|
50
|
+
RunAPI-generated file URLs are temporary. Download and store generated videos in
|
|
51
|
+
your own durable storage within 7 days; do not treat returned URLs as long-term
|
|
52
|
+
assets.
|
|
53
|
+
|
|
54
|
+
## Language notes
|
|
55
|
+
|
|
56
|
+
Pass parameters as keyword arguments and catch the `runapi.hailuo` error classes
|
|
57
|
+
when building video jobs or scripts. The available resources are `text_to_video`
|
|
58
|
+
and `image_to_video`. Keep `RUNAPI_API_KEY` in the environment or your secret
|
|
59
|
+
manager; never commit API keys or callback secrets.
|
|
60
|
+
|
|
61
|
+
## Links
|
|
62
|
+
|
|
63
|
+
- Model page: https://runapi.ai/models/hailuo
|
|
64
|
+
- SDK docs: https://runapi.ai/docs#sdk-hailuo
|
|
65
|
+
- Product docs: https://runapi.ai/docs#hailuo
|
|
66
|
+
- Pricing and rate limits: https://runapi.ai/models/hailuo/02-text-to-video-pro
|
|
67
|
+
- Full catalog: https://runapi.ai/models
|
|
68
|
+
|
|
69
|
+
## License
|
|
70
|
+
|
|
71
|
+
Licensed under the Apache License, Version 2.0.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "runapi-hailuo"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Hailuo text-to-video and image-to-video client for RunAPI"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
authors = [{ name = "RunAPI", email = "contact@runapi.ai" }]
|
|
13
|
+
keywords = ["runapi", "hailuo", "text-to-video", "image-to-video", "ai", "sdk"]
|
|
14
|
+
dependencies = ["runapi-core"]
|
|
15
|
+
|
|
16
|
+
[project.urls]
|
|
17
|
+
Homepage = "https://runapi.ai/models/hailuo"
|
|
18
|
+
Documentation = "https://runapi.ai/docs#sdk-hailuo"
|
|
19
|
+
|
|
20
|
+
[tool.hatch.build.targets.wheel]
|
|
21
|
+
packages = ["src/runapi"]
|
|
22
|
+
|
|
23
|
+
[tool.uv]
|
|
24
|
+
package = true
|
|
25
|
+
|
|
26
|
+
[tool.uv.sources]
|
|
27
|
+
runapi-core = { path = "../runapi-core", editable = true }
|
|
28
|
+
|
|
29
|
+
[dependency-groups]
|
|
30
|
+
dev = ["pytest>=8"]
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""Hailuo client for RunAPI."""
|
|
2
|
+
|
|
3
|
+
from runapi.core import (
|
|
4
|
+
AuthenticationError,
|
|
5
|
+
InsufficientCreditsError,
|
|
6
|
+
NotFoundError,
|
|
7
|
+
RateLimitError,
|
|
8
|
+
TaskFailedError,
|
|
9
|
+
TaskTimeoutError,
|
|
10
|
+
ValidationError,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
from .client import HailuoClient
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"HailuoClient",
|
|
17
|
+
"AuthenticationError",
|
|
18
|
+
"RateLimitError",
|
|
19
|
+
"InsufficientCreditsError",
|
|
20
|
+
"NotFoundError",
|
|
21
|
+
"ValidationError",
|
|
22
|
+
"TaskFailedError",
|
|
23
|
+
"TaskTimeoutError",
|
|
24
|
+
]
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Hailuo client."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Optional
|
|
6
|
+
|
|
7
|
+
from runapi.core import ClientOptions, HttpClient, resolve_api_key
|
|
8
|
+
|
|
9
|
+
from .resources.image_to_video import ImageToVideo
|
|
10
|
+
from .resources.text_to_video import TextToVideo
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class HailuoClient:
|
|
14
|
+
"""Hailuo text-to-video and image-to-video client.
|
|
15
|
+
|
|
16
|
+
Example::
|
|
17
|
+
|
|
18
|
+
client = HailuoClient(api_key="sk-...")
|
|
19
|
+
result = client.text_to_video.run(
|
|
20
|
+
model="hailuo-02-text-to-video-standard", prompt="A neon city street"
|
|
21
|
+
)
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(self, api_key: Optional[str] = None, **options: Any) -> None:
|
|
25
|
+
resolved_api_key = resolve_api_key(api_key)
|
|
26
|
+
client_options = ClientOptions(api_key=resolved_api_key, **options)
|
|
27
|
+
http = client_options.http_client or HttpClient(client_options)
|
|
28
|
+
self.text_to_video = TextToVideo(http)
|
|
29
|
+
self.image_to_video = ImageToVideo(http)
|
|
File without changes
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"""Hailuo image-to-video resource."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Dict, Sequence
|
|
6
|
+
|
|
7
|
+
from runapi.core import Resource, ValidationError
|
|
8
|
+
|
|
9
|
+
from ..types import (
|
|
10
|
+
DURATIONS,
|
|
11
|
+
IMAGE_02_RESOLUTIONS,
|
|
12
|
+
IMAGE_23_RESOLUTIONS,
|
|
13
|
+
IMAGE_TO_VIDEO_MODELS,
|
|
14
|
+
CompletedVideoTaskResponse,
|
|
15
|
+
VideoTaskResponse,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ImageToVideo(Resource):
|
|
20
|
+
"""Generate videos from a source image with Hailuo models."""
|
|
21
|
+
|
|
22
|
+
ENDPOINT = "/api/v1/hailuo/image_to_video"
|
|
23
|
+
|
|
24
|
+
RESPONSE_CLASS = VideoTaskResponse
|
|
25
|
+
COMPLETED_RESPONSE_CLASS = CompletedVideoTaskResponse
|
|
26
|
+
|
|
27
|
+
def run(self, **params: Any) -> Any:
|
|
28
|
+
"""Create an image-to-video task and poll until it completes.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
**params: Image-to-video parameters (model, prompt, ...).
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
The completed task with videos.
|
|
35
|
+
"""
|
|
36
|
+
task = self.create(**params)
|
|
37
|
+
return self._poll_until_complete(lambda: self.get(task.id))
|
|
38
|
+
|
|
39
|
+
def create(self, **params: Any) -> Any:
|
|
40
|
+
"""Create an image-to-video task and return immediately with an ``id``.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
**params: Image-to-video parameters (model, prompt, ...).
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
The task creation result with an id.
|
|
47
|
+
"""
|
|
48
|
+
compacted = self._compact_params(params)
|
|
49
|
+
self._validate_params(compacted)
|
|
50
|
+
return self._request("post", self.ENDPOINT, body=compacted)
|
|
51
|
+
|
|
52
|
+
def get(self, id: str) -> Any:
|
|
53
|
+
"""Fetch the current status of an image-to-video task.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
id: The task id.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
The current task status.
|
|
60
|
+
"""
|
|
61
|
+
return self._request("get", f"{self.ENDPOINT}/{id}")
|
|
62
|
+
|
|
63
|
+
def _validate_params(self, params: Dict[str, Any]) -> None:
|
|
64
|
+
model = params.get("model")
|
|
65
|
+
if model not in IMAGE_TO_VIDEO_MODELS:
|
|
66
|
+
raise ValidationError("model is required")
|
|
67
|
+
if not params.get("prompt"):
|
|
68
|
+
raise ValidationError("prompt is required")
|
|
69
|
+
if not params.get("first_frame_image_url"):
|
|
70
|
+
raise ValidationError("first_frame_image_url is required")
|
|
71
|
+
|
|
72
|
+
duration_seconds = params.get("duration_seconds")
|
|
73
|
+
output_resolution = params.get("output_resolution")
|
|
74
|
+
|
|
75
|
+
if duration_seconds and duration_seconds not in DURATIONS:
|
|
76
|
+
raise ValidationError(
|
|
77
|
+
f"duration_seconds must be one of: {', '.join(str(d) for d in DURATIONS)}"
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
if model == "hailuo-02-image-to-video-pro":
|
|
81
|
+
if duration_seconds:
|
|
82
|
+
raise ValidationError(f"duration_seconds is not supported for {model}")
|
|
83
|
+
if output_resolution:
|
|
84
|
+
raise ValidationError(f"output_resolution is not supported for {model}")
|
|
85
|
+
elif model == "hailuo-02-image-to-video-standard":
|
|
86
|
+
if output_resolution:
|
|
87
|
+
self._validate_output_resolution(output_resolution, IMAGE_02_RESOLUTIONS)
|
|
88
|
+
else:
|
|
89
|
+
if output_resolution:
|
|
90
|
+
self._validate_output_resolution(output_resolution, IMAGE_23_RESOLUTIONS)
|
|
91
|
+
if params.get("last_frame_image_url"):
|
|
92
|
+
raise ValidationError(f"last_frame_image_url is not supported for {model}")
|
|
93
|
+
if params.get("prompt_optimizer"):
|
|
94
|
+
raise ValidationError(f"prompt_optimizer is not supported for {model}")
|
|
95
|
+
if duration_seconds == 10 and str(output_resolution) == "1080p":
|
|
96
|
+
raise ValidationError("1080p does not support 10-second duration")
|
|
97
|
+
|
|
98
|
+
@staticmethod
|
|
99
|
+
def _validate_output_resolution(output_resolution: Any, allowed: Sequence[str]) -> None:
|
|
100
|
+
if str(output_resolution) in allowed:
|
|
101
|
+
return
|
|
102
|
+
raise ValidationError(f"output_resolution must be one of: {', '.join(allowed)}")
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""Hailuo text-to-video resource."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, Dict
|
|
6
|
+
|
|
7
|
+
from runapi.core import Resource, ValidationError
|
|
8
|
+
|
|
9
|
+
from ..types import (
|
|
10
|
+
DURATIONS,
|
|
11
|
+
TEXT_TO_VIDEO_MODELS,
|
|
12
|
+
CompletedVideoTaskResponse,
|
|
13
|
+
VideoTaskResponse,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class TextToVideo(Resource):
|
|
18
|
+
"""Generate videos from text prompts with Hailuo models."""
|
|
19
|
+
|
|
20
|
+
ENDPOINT = "/api/v1/hailuo/text_to_video"
|
|
21
|
+
|
|
22
|
+
RESPONSE_CLASS = VideoTaskResponse
|
|
23
|
+
COMPLETED_RESPONSE_CLASS = CompletedVideoTaskResponse
|
|
24
|
+
|
|
25
|
+
def run(self, **params: Any) -> Any:
|
|
26
|
+
"""Create a text-to-video task and poll until it completes.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
**params: Text-to-video parameters (model, prompt, ...).
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
The completed task with videos.
|
|
33
|
+
"""
|
|
34
|
+
task = self.create(**params)
|
|
35
|
+
return self._poll_until_complete(lambda: self.get(task.id))
|
|
36
|
+
|
|
37
|
+
def create(self, **params: Any) -> Any:
|
|
38
|
+
"""Create a text-to-video task and return immediately with an ``id``.
|
|
39
|
+
|
|
40
|
+
Args:
|
|
41
|
+
**params: Text-to-video parameters (model, prompt, ...).
|
|
42
|
+
|
|
43
|
+
Returns:
|
|
44
|
+
The task creation result with an id.
|
|
45
|
+
"""
|
|
46
|
+
compacted = self._compact_params(params)
|
|
47
|
+
self._validate_params(compacted)
|
|
48
|
+
return self._request("post", self.ENDPOINT, body=compacted)
|
|
49
|
+
|
|
50
|
+
def get(self, id: str) -> Any:
|
|
51
|
+
"""Fetch the current status of a text-to-video task.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
id: The task id.
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
The current task status.
|
|
58
|
+
"""
|
|
59
|
+
return self._request("get", f"{self.ENDPOINT}/{id}")
|
|
60
|
+
|
|
61
|
+
def _validate_params(self, params: Dict[str, Any]) -> None:
|
|
62
|
+
model = params.get("model")
|
|
63
|
+
if model not in TEXT_TO_VIDEO_MODELS:
|
|
64
|
+
raise ValidationError("model is required")
|
|
65
|
+
if not params.get("prompt"):
|
|
66
|
+
raise ValidationError("prompt is required")
|
|
67
|
+
|
|
68
|
+
duration_seconds = params.get("duration_seconds")
|
|
69
|
+
if not duration_seconds:
|
|
70
|
+
return
|
|
71
|
+
|
|
72
|
+
if duration_seconds not in DURATIONS:
|
|
73
|
+
raise ValidationError(
|
|
74
|
+
f"duration_seconds must be one of: {', '.join(str(d) for d in DURATIONS)}"
|
|
75
|
+
)
|
|
76
|
+
if model == "hailuo-02-text-to-video-pro":
|
|
77
|
+
raise ValidationError(f"duration_seconds is not supported for {model}")
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Hailuo model lists, enums, and response models."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from runapi.core import BaseModel, TaskResponse, optional, required
|
|
6
|
+
|
|
7
|
+
TEXT_TO_VIDEO_MODELS = [
|
|
8
|
+
"hailuo-02-text-to-video-pro",
|
|
9
|
+
"hailuo-02-text-to-video-standard",
|
|
10
|
+
]
|
|
11
|
+
IMAGE_TO_VIDEO_MODELS = [
|
|
12
|
+
"hailuo-02-image-to-video-pro",
|
|
13
|
+
"hailuo-02-image-to-video-standard",
|
|
14
|
+
"hailuo-2.3-image-to-video-pro",
|
|
15
|
+
"hailuo-2.3-image-to-video-standard",
|
|
16
|
+
]
|
|
17
|
+
DURATIONS = [6, 10]
|
|
18
|
+
IMAGE_02_RESOLUTIONS = ["512p", "768p"]
|
|
19
|
+
IMAGE_23_RESOLUTIONS = ["768p", "1080p"]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class MediaUrl(BaseModel):
|
|
23
|
+
url = optional(str)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class VideoTaskResponse(TaskResponse):
|
|
27
|
+
"""Response for a video generation task."""
|
|
28
|
+
|
|
29
|
+
id = required(str)
|
|
30
|
+
status = optional(str, enum=lambda: TaskResponse.Status.ALL)
|
|
31
|
+
videos = optional([lambda: MediaUrl])
|
|
32
|
+
error = optional(str)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class CompletedVideoTaskResponse(VideoTaskResponse):
|
|
36
|
+
"""Narrowed response from ``run()`` once polling observes completion."""
|
|
37
|
+
|
|
38
|
+
videos = required([lambda: MediaUrl])
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from runapi.core import config
|
|
4
|
+
from runapi.core.errors import AuthenticationError, ValidationError
|
|
5
|
+
from runapi.hailuo import HailuoClient
|
|
6
|
+
from runapi.hailuo.resources.image_to_video import ImageToVideo
|
|
7
|
+
from runapi.hailuo.resources.text_to_video import TextToVideo
|
|
8
|
+
from runapi.hailuo.types import CompletedVideoTaskResponse, VideoTaskResponse
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class FakeHttp:
|
|
12
|
+
def __init__(self, *responses):
|
|
13
|
+
self._responses = list(responses)
|
|
14
|
+
self.calls = []
|
|
15
|
+
|
|
16
|
+
def request(self, method, path, body=None, options=None):
|
|
17
|
+
self.calls.append((method, path, body))
|
|
18
|
+
if self._responses:
|
|
19
|
+
return self._responses.pop(0)
|
|
20
|
+
return {"id": "task_1", "status": "pending"}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@pytest.fixture(autouse=True)
|
|
24
|
+
def reset_config(monkeypatch):
|
|
25
|
+
monkeypatch.delenv("RUNAPI_API_KEY", raising=False)
|
|
26
|
+
monkeypatch.setattr(config, "api_key", None)
|
|
27
|
+
yield
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
# --- auth -----------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def test_accepts_api_key_parameter():
|
|
34
|
+
assert isinstance(HailuoClient(api_key="k", http_client=FakeHttp()), HailuoClient)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_falls_back_to_global(monkeypatch):
|
|
38
|
+
monkeypatch.setattr(config, "api_key", "global-key")
|
|
39
|
+
assert isinstance(HailuoClient(http_client=FakeHttp()), HailuoClient)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_falls_back_to_env(monkeypatch):
|
|
43
|
+
monkeypatch.setenv("RUNAPI_API_KEY", "env-key")
|
|
44
|
+
assert isinstance(HailuoClient(http_client=FakeHttp()), HailuoClient)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_raises_without_api_key():
|
|
48
|
+
with pytest.raises(AuthenticationError, match="API key is required"):
|
|
49
|
+
HailuoClient()
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
# --- injection / accessors ------------------------------------------------
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_uses_injected_http_client():
|
|
56
|
+
fake = FakeHttp()
|
|
57
|
+
client = HailuoClient(api_key="k", http_client=fake)
|
|
58
|
+
assert client.text_to_video._http is fake
|
|
59
|
+
assert client.image_to_video._http is fake
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def test_exposes_resource_accessors():
|
|
63
|
+
client = HailuoClient(api_key="k", http_client=FakeHttp())
|
|
64
|
+
assert isinstance(client.text_to_video, TextToVideo)
|
|
65
|
+
assert isinstance(client.image_to_video, ImageToVideo)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
# --- request shapes -------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def test_create_posts_compacted_body():
|
|
72
|
+
fake = FakeHttp({"id": "t1", "status": "pending"})
|
|
73
|
+
client = HailuoClient(api_key="k", http_client=fake)
|
|
74
|
+
result = client.text_to_video.create(
|
|
75
|
+
model="hailuo-02-text-to-video-standard",
|
|
76
|
+
prompt="hello world",
|
|
77
|
+
duration_seconds=6,
|
|
78
|
+
output_resolution=None,
|
|
79
|
+
)
|
|
80
|
+
assert fake.calls == [
|
|
81
|
+
(
|
|
82
|
+
"post",
|
|
83
|
+
"/api/v1/hailuo/text_to_video",
|
|
84
|
+
{"model": "hailuo-02-text-to-video-standard", "prompt": "hello world", "duration_seconds": 6},
|
|
85
|
+
),
|
|
86
|
+
]
|
|
87
|
+
assert isinstance(result, VideoTaskResponse)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def test_image_to_video_create_posts_compacted_body():
|
|
91
|
+
fake = FakeHttp({"id": "t1", "status": "pending"})
|
|
92
|
+
client = HailuoClient(api_key="k", http_client=fake)
|
|
93
|
+
client.image_to_video.create(
|
|
94
|
+
model="hailuo-2.3-image-to-video-standard",
|
|
95
|
+
prompt="pan the camera",
|
|
96
|
+
first_frame_image_url="https://x/a.png",
|
|
97
|
+
output_resolution="768p",
|
|
98
|
+
)
|
|
99
|
+
assert fake.calls == [
|
|
100
|
+
(
|
|
101
|
+
"post",
|
|
102
|
+
"/api/v1/hailuo/image_to_video",
|
|
103
|
+
{
|
|
104
|
+
"model": "hailuo-2.3-image-to-video-standard",
|
|
105
|
+
"prompt": "pan the camera",
|
|
106
|
+
"first_frame_image_url": "https://x/a.png",
|
|
107
|
+
"output_resolution": "768p",
|
|
108
|
+
},
|
|
109
|
+
),
|
|
110
|
+
]
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def test_get_fetches_by_id():
|
|
114
|
+
fake = FakeHttp({"id": "t1", "status": "processing"})
|
|
115
|
+
client = HailuoClient(api_key="k", http_client=fake)
|
|
116
|
+
client.text_to_video.get("t1")
|
|
117
|
+
assert fake.calls == [("get", "/api/v1/hailuo/text_to_video/t1", None)]
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def test_run_narrows_completed_type():
|
|
121
|
+
fake = FakeHttp(
|
|
122
|
+
{"id": "t1", "status": "pending"},
|
|
123
|
+
{"id": "t1", "status": "completed", "videos": [{"url": "https://x/y.mp4"}]},
|
|
124
|
+
)
|
|
125
|
+
client = HailuoClient(api_key="k", http_client=fake)
|
|
126
|
+
result = client.text_to_video.run(
|
|
127
|
+
model="hailuo-02-text-to-video-standard", prompt="a serene lake"
|
|
128
|
+
)
|
|
129
|
+
assert isinstance(result, CompletedVideoTaskResponse)
|
|
130
|
+
assert result.videos[0].url == "https://x/y.mp4"
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
# --- validation -----------------------------------------------------------
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def test_rejects_unknown_model():
|
|
137
|
+
client = HailuoClient(api_key="k", http_client=FakeHttp())
|
|
138
|
+
with pytest.raises(ValidationError, match="model is required"):
|
|
139
|
+
client.text_to_video.create(model="nope", prompt="hi there")
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def test_requires_prompt():
|
|
143
|
+
client = HailuoClient(api_key="k", http_client=FakeHttp())
|
|
144
|
+
with pytest.raises(ValidationError, match="prompt is required"):
|
|
145
|
+
client.text_to_video.create(model="hailuo-02-text-to-video-standard")
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def test_text_to_video_duration_enum():
|
|
149
|
+
client = HailuoClient(api_key="k", http_client=FakeHttp())
|
|
150
|
+
with pytest.raises(ValidationError, match="duration_seconds must be one of: 6, 10"):
|
|
151
|
+
client.text_to_video.create(
|
|
152
|
+
model="hailuo-02-text-to-video-standard", prompt="hi there", duration_seconds=7
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def test_text_to_video_pro_rejects_duration():
|
|
157
|
+
client = HailuoClient(api_key="k", http_client=FakeHttp())
|
|
158
|
+
with pytest.raises(ValidationError, match="duration_seconds is not supported"):
|
|
159
|
+
client.text_to_video.create(
|
|
160
|
+
model="hailuo-02-text-to-video-pro", prompt="hi there", duration_seconds=6
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def test_image_to_video_requires_first_frame():
|
|
165
|
+
client = HailuoClient(api_key="k", http_client=FakeHttp())
|
|
166
|
+
with pytest.raises(ValidationError, match="first_frame_image_url is required"):
|
|
167
|
+
client.image_to_video.create(
|
|
168
|
+
model="hailuo-02-image-to-video-standard", prompt="hi there"
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def test_image_to_video_pro_rejects_resolution():
|
|
173
|
+
client = HailuoClient(api_key="k", http_client=FakeHttp())
|
|
174
|
+
with pytest.raises(ValidationError, match="output_resolution is not supported"):
|
|
175
|
+
client.image_to_video.create(
|
|
176
|
+
model="hailuo-02-image-to-video-pro",
|
|
177
|
+
prompt="hi there",
|
|
178
|
+
first_frame_image_url="https://x/a.png",
|
|
179
|
+
output_resolution="512p",
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def test_image_to_video_02_standard_resolution_enum():
|
|
184
|
+
client = HailuoClient(api_key="k", http_client=FakeHttp())
|
|
185
|
+
with pytest.raises(ValidationError, match="output_resolution must be one of: 512p, 768p"):
|
|
186
|
+
client.image_to_video.create(
|
|
187
|
+
model="hailuo-02-image-to-video-standard",
|
|
188
|
+
prompt="hi there",
|
|
189
|
+
first_frame_image_url="https://x/a.png",
|
|
190
|
+
output_resolution="1080p",
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def test_image_to_video_23_rejects_last_frame():
|
|
195
|
+
client = HailuoClient(api_key="k", http_client=FakeHttp())
|
|
196
|
+
with pytest.raises(ValidationError, match="last_frame_image_url is not supported"):
|
|
197
|
+
client.image_to_video.create(
|
|
198
|
+
model="hailuo-2.3-image-to-video-standard",
|
|
199
|
+
prompt="hi there",
|
|
200
|
+
first_frame_image_url="https://x/a.png",
|
|
201
|
+
last_frame_image_url="https://x/b.png",
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def test_image_to_video_23_1080p_no_10_second():
|
|
206
|
+
client = HailuoClient(api_key="k", http_client=FakeHttp())
|
|
207
|
+
with pytest.raises(ValidationError, match="1080p does not support 10-second duration"):
|
|
208
|
+
client.image_to_video.create(
|
|
209
|
+
model="hailuo-2.3-image-to-video-standard",
|
|
210
|
+
prompt="hi there",
|
|
211
|
+
first_frame_image_url="https://x/a.png",
|
|
212
|
+
duration_seconds=10,
|
|
213
|
+
output_resolution="1080p",
|
|
214
|
+
)
|