runapi-recraft 0.1.0__py3-none-any.whl
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/recraft/__init__.py +24 -0
- runapi/recraft/client.py +30 -0
- runapi/recraft/contract_gen.py +22 -0
- runapi/recraft/py.typed +0 -0
- runapi/recraft/resources/__init__.py +4 -0
- runapi/recraft/resources/remove_background.py +58 -0
- runapi/recraft/resources/upscale_image.py +58 -0
- runapi/recraft/types.py +24 -0
- runapi_recraft-0.1.0.dist-info/METADATA +83 -0
- runapi_recraft-0.1.0.dist-info/RECORD +11 -0
- runapi_recraft-0.1.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""Recraft 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 RecraftClient
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"RecraftClient",
|
|
17
|
+
"AuthenticationError",
|
|
18
|
+
"RateLimitError",
|
|
19
|
+
"InsufficientCreditsError",
|
|
20
|
+
"NotFoundError",
|
|
21
|
+
"ValidationError",
|
|
22
|
+
"TaskFailedError",
|
|
23
|
+
"TaskTimeoutError",
|
|
24
|
+
]
|
runapi/recraft/client.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""Recraft 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.remove_background import RemoveBackground
|
|
10
|
+
from .resources.upscale_image import UpscaleImage
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class RecraftClient:
|
|
14
|
+
"""Recraft image upscale and background-removal client.
|
|
15
|
+
|
|
16
|
+
Example::
|
|
17
|
+
|
|
18
|
+
client = RecraftClient(api_key="sk-...")
|
|
19
|
+
result = client.upscale_image.run(
|
|
20
|
+
model="recraft-crisp-upscale",
|
|
21
|
+
source_image_url="https://example.com/source.jpg",
|
|
22
|
+
)
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def __init__(self, api_key: Optional[str] = None, **options: Any) -> None:
|
|
26
|
+
resolved_api_key = resolve_api_key(api_key)
|
|
27
|
+
client_options = ClientOptions(api_key=resolved_api_key, **options)
|
|
28
|
+
http = client_options.http_client or HttpClient(client_options)
|
|
29
|
+
self.upscale_image = UpscaleImage(http)
|
|
30
|
+
self.remove_background = RemoveBackground(http)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
CONTRACT = {
|
|
2
|
+
"remove-background": {
|
|
3
|
+
"models": ["recraft-remove-background"],
|
|
4
|
+
"fields_by_model": {
|
|
5
|
+
"recraft-remove-background": {
|
|
6
|
+
"source_image_url": {
|
|
7
|
+
"required": True
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"upscale-image": {
|
|
13
|
+
"models": ["recraft-crisp-upscale"],
|
|
14
|
+
"fields_by_model": {
|
|
15
|
+
"recraft-crisp-upscale": {
|
|
16
|
+
"source_image_url": {
|
|
17
|
+
"required": True
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
runapi/recraft/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Recraft remove-background resource."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from runapi.core import Resource
|
|
8
|
+
|
|
9
|
+
from ..contract_gen import CONTRACT
|
|
10
|
+
from ..types import (
|
|
11
|
+
CompletedImageTaskResponse,
|
|
12
|
+
ImageTaskResponse,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class RemoveBackground(Resource):
|
|
17
|
+
"""Remove image backgrounds with Recraft models."""
|
|
18
|
+
|
|
19
|
+
ENDPOINT = "/api/v1/recraft/remove_background"
|
|
20
|
+
|
|
21
|
+
RESPONSE_CLASS = ImageTaskResponse
|
|
22
|
+
COMPLETED_RESPONSE_CLASS = CompletedImageTaskResponse
|
|
23
|
+
|
|
24
|
+
def run(self, **params: Any) -> Any:
|
|
25
|
+
"""Run a remove-background task and poll until it completes.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
**params: remove-background parameters (model, ...).
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
The completed (narrowed) remove-background response.
|
|
32
|
+
"""
|
|
33
|
+
task = self.create(**params)
|
|
34
|
+
return self._poll_until_complete(lambda: self.get(task.id))
|
|
35
|
+
|
|
36
|
+
def create(self, **params: Any) -> Any:
|
|
37
|
+
"""Create a remove-background task and return immediately with an id.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
**params: remove-background parameters (model, ...).
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
The task creation result with an id.
|
|
44
|
+
"""
|
|
45
|
+
compacted = self._compact_params(params)
|
|
46
|
+
self._validate_contract(CONTRACT["remove-background"], compacted)
|
|
47
|
+
return self._request("post", self.ENDPOINT, body=compacted)
|
|
48
|
+
|
|
49
|
+
def get(self, id: str) -> Any:
|
|
50
|
+
"""Fetch the current status of a remove-background task.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
id: The task id returned by ``create``.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
The current task status.
|
|
57
|
+
"""
|
|
58
|
+
return self._request("get", f"{self.ENDPOINT}/{id}")
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"""Recraft upscale-image resource."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from runapi.core import Resource
|
|
8
|
+
|
|
9
|
+
from ..contract_gen import CONTRACT
|
|
10
|
+
from ..types import (
|
|
11
|
+
CompletedImageTaskResponse,
|
|
12
|
+
ImageTaskResponse,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class UpscaleImage(Resource):
|
|
17
|
+
"""Upscale images with Recraft models."""
|
|
18
|
+
|
|
19
|
+
ENDPOINT = "/api/v1/recraft/upscale_image"
|
|
20
|
+
|
|
21
|
+
RESPONSE_CLASS = ImageTaskResponse
|
|
22
|
+
COMPLETED_RESPONSE_CLASS = CompletedImageTaskResponse
|
|
23
|
+
|
|
24
|
+
def run(self, **params: Any) -> Any:
|
|
25
|
+
"""Run an image upscale task and poll until it completes.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
**params: image upscale parameters (model, ...).
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
The completed (narrowed) image upscale response.
|
|
32
|
+
"""
|
|
33
|
+
task = self.create(**params)
|
|
34
|
+
return self._poll_until_complete(lambda: self.get(task.id))
|
|
35
|
+
|
|
36
|
+
def create(self, **params: Any) -> Any:
|
|
37
|
+
"""Create an image upscale task and return immediately with an id.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
**params: image upscale parameters (model, ...).
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
The task creation result with an id.
|
|
44
|
+
"""
|
|
45
|
+
compacted = self._compact_params(params)
|
|
46
|
+
self._validate_contract(CONTRACT["upscale-image"], compacted)
|
|
47
|
+
return self._request("post", self.ENDPOINT, body=compacted)
|
|
48
|
+
|
|
49
|
+
def get(self, id: str) -> Any:
|
|
50
|
+
"""Fetch the current status of an image upscale task.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
id: The task id returned by ``create``.
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
The current task status.
|
|
57
|
+
"""
|
|
58
|
+
return self._request("get", f"{self.ENDPOINT}/{id}")
|
runapi/recraft/types.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""Recraft model lists and response models."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from runapi.core import BaseModel, TaskResponse, optional, required
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Image(BaseModel):
|
|
9
|
+
url = optional(str)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class ImageTaskResponse(TaskResponse):
|
|
13
|
+
"""Recraft image task status response."""
|
|
14
|
+
|
|
15
|
+
id = required(str)
|
|
16
|
+
status = optional(str, enum=lambda: TaskResponse.Status.ALL)
|
|
17
|
+
images = optional([lambda: Image])
|
|
18
|
+
error = optional(str)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class CompletedImageTaskResponse(ImageTaskResponse):
|
|
22
|
+
"""Narrowed response from ``run()`` once polling observes completion."""
|
|
23
|
+
|
|
24
|
+
images = required([lambda: Image])
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: runapi-recraft
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Recraft image upscale and background-removal client for RunAPI
|
|
5
|
+
Project-URL: Homepage, https://runapi.ai/models/recraft
|
|
6
|
+
Project-URL: Documentation, https://runapi.ai/docs#sdk-recraft
|
|
7
|
+
Author-email: RunAPI <contact@runapi.ai>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
Keywords: ai,recraft,remove-background,runapi,sdk,upscale-image
|
|
10
|
+
Requires-Python: >=3.9
|
|
11
|
+
Requires-Dist: runapi-core
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
|
|
14
|
+
# Recraft Python SDK for RunAPI
|
|
15
|
+
|
|
16
|
+
The Recraft Python SDK is the language-specific package for Recraft on RunAPI.
|
|
17
|
+
Use it for image upscale and background-removal flows when your application needs
|
|
18
|
+
JSON request bodies, task status lookup, and consistent RunAPI errors in Python.
|
|
19
|
+
|
|
20
|
+
For model details, use https://runapi.ai/models/recraft; for API reference, use
|
|
21
|
+
https://runapi.ai/docs#recraft; for SDK docs, use https://runapi.ai/docs#sdk-recraft.
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install runapi-recraft
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick start
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from runapi.recraft import RecraftClient
|
|
33
|
+
|
|
34
|
+
client = RecraftClient() # reads RUNAPI_API_KEY, or pass api_key="sk-..."
|
|
35
|
+
|
|
36
|
+
task = client.upscale_image.create(
|
|
37
|
+
model="recraft-crisp-upscale",
|
|
38
|
+
source_image_url="https://example.com/source.jpg",
|
|
39
|
+
)
|
|
40
|
+
status = client.upscale_image.get(task.id)
|
|
41
|
+
|
|
42
|
+
removed = client.remove_background.create(
|
|
43
|
+
model="recraft-remove-background",
|
|
44
|
+
source_image_url="https://example.com/source.jpg",
|
|
45
|
+
)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Use `create` to submit a task and return quickly, `get` to fetch the latest task
|
|
49
|
+
state, and `run` to create and poll until completion:
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
result = client.upscale_image.run(
|
|
53
|
+
model="recraft-crisp-upscale",
|
|
54
|
+
source_image_url="https://example.com/source.jpg",
|
|
55
|
+
)
|
|
56
|
+
print(result.images[0].url)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
In web request handlers, prefer `create` plus webhook or later `get` polling so a
|
|
60
|
+
worker is not held open.
|
|
61
|
+
|
|
62
|
+
RunAPI-generated file URLs are temporary. Download and store generated images in
|
|
63
|
+
your own durable storage within 7 days; do not treat returned URLs as long-term
|
|
64
|
+
assets.
|
|
65
|
+
|
|
66
|
+
## Language notes
|
|
67
|
+
|
|
68
|
+
Pass parameters as keyword arguments and catch the `runapi.recraft` error
|
|
69
|
+
classes when building image jobs or scripts. The available resources are
|
|
70
|
+
`upscale_image` and `remove_background`. Keep `RUNAPI_API_KEY` in the environment
|
|
71
|
+
or your secret manager; never commit API keys or callback secrets.
|
|
72
|
+
|
|
73
|
+
## Links
|
|
74
|
+
|
|
75
|
+
- Model page: https://runapi.ai/models/recraft
|
|
76
|
+
- SDK docs: https://runapi.ai/docs#sdk-recraft
|
|
77
|
+
- Product docs: https://runapi.ai/docs#recraft
|
|
78
|
+
- Pricing and rate limits: https://runapi.ai/models/recraft
|
|
79
|
+
- Full catalog: https://runapi.ai/models
|
|
80
|
+
|
|
81
|
+
## License
|
|
82
|
+
|
|
83
|
+
Licensed under the Apache License, Version 2.0.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
runapi/recraft/__init__.py,sha256=WpRITiBp2AA1_uUyWtSg8OwDu6GT_G0mw2NrO1jS20c,463
|
|
2
|
+
runapi/recraft/client.py,sha256=npY8VshJxNbCUTdbz6ehjzeyOk5xrTQgsjrTlxHDhAE,968
|
|
3
|
+
runapi/recraft/contract_gen.py,sha256=hzlVmcX5IkmTXWmcPY-WE2lrLj6GcK_-SwrsLREFdOY,546
|
|
4
|
+
runapi/recraft/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
runapi/recraft/types.py,sha256=NwSfiakzoqmXeyN24CbhS1DZAuYpetJH0h30FW2iVQo,615
|
|
6
|
+
runapi/recraft/resources/__init__.py,sha256=zVY8SPzJ9lGQuZw6UsNHZG6hb3x-oHhrLRQejfh7r_M,136
|
|
7
|
+
runapi/recraft/resources/remove_background.py,sha256=hs_giaVQ3713cbDpzm7gdHOdwJ_i5WdB58sQ_WnCzGo,1654
|
|
8
|
+
runapi/recraft/resources/upscale_image.py,sha256=ib-vFJptYEUsR2PnWHciAYQI0FtkPgvbu5HMT136j4A,1607
|
|
9
|
+
runapi_recraft-0.1.0.dist-info/METADATA,sha256=JKhh2nMQFrKRFtR6Un4pwIaOtxAUC1ddaQ9EopWbmJM,2644
|
|
10
|
+
runapi_recraft-0.1.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
11
|
+
runapi_recraft-0.1.0.dist-info/RECORD,,
|