fal-client 0.2.0__tar.gz → 0.2.2__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.
- fal_client-0.2.2/PKG-INFO +96 -0
- fal_client-0.2.2/README.md +84 -0
- {fal_client-0.2.0 → fal_client-0.2.2}/pyproject.toml +1 -1
- {fal_client-0.2.0 → fal_client-0.2.2}/src/fal_client/__init__.py +6 -0
- {fal_client-0.2.0 → fal_client-0.2.2}/src/fal_client/client.py +102 -17
- fal_client-0.2.2/src/fal_client.egg-info/PKG-INFO +96 -0
- fal_client-0.2.0/PKG-INFO +0 -18
- fal_client-0.2.0/README.md +0 -6
- fal_client-0.2.0/src/fal_client.egg-info/PKG-INFO +0 -18
- {fal_client-0.2.0 → fal_client-0.2.2}/setup.cfg +0 -0
- {fal_client-0.2.0 → fal_client-0.2.2}/src/fal_client/auth.py +0 -0
- {fal_client-0.2.0 → fal_client-0.2.2}/src/fal_client/py.typed +0 -0
- {fal_client-0.2.0 → fal_client-0.2.2}/src/fal_client.egg-info/SOURCES.txt +0 -0
- {fal_client-0.2.0 → fal_client-0.2.2}/src/fal_client.egg-info/dependency_links.txt +0 -0
- {fal_client-0.2.0 → fal_client-0.2.2}/src/fal_client.egg-info/requires.txt +0 -0
- {fal_client-0.2.0 → fal_client-0.2.2}/src/fal_client.egg-info/top_level.txt +0 -0
- {fal_client-0.2.0 → fal_client-0.2.2}/tests/test_async_client.py +0 -0
- {fal_client-0.2.0 → fal_client-0.2.2}/tests/test_sync_client.py +0 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: fal_client
|
|
3
|
+
Version: 0.2.2
|
|
4
|
+
Summary: Python client for fal.ai
|
|
5
|
+
Author: fal <hello@fal.ai>
|
|
6
|
+
Project-URL: homepage, https://fal.ai
|
|
7
|
+
Project-URL: repository, https://github.com/fal-ai/fal
|
|
8
|
+
Requires-Python: >=3.8
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: httpx<1,>=0.21.0
|
|
11
|
+
Requires-Dist: httpx-sse<0.5,>=0.4.0
|
|
12
|
+
|
|
13
|
+
# fal.ai Python client
|
|
14
|
+
|
|
15
|
+
This is a Python client library for interacting with ML models deployed on [fal.ai](https://fal.ai).
|
|
16
|
+
|
|
17
|
+
## Getting started
|
|
18
|
+
|
|
19
|
+
To install the client, run:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install fal-client
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
To use the client, you need to have an API key. You can get one by signing up at [fal.ai](https://fal.ai). Once you have it, set
|
|
26
|
+
it as an environment variable:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
export FAL_KEY=your-api-key
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Now you can use the client to interact with your models. Here's an example of how to use it:
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import fal_client
|
|
36
|
+
|
|
37
|
+
response = fal_client.run("fal-ai/fast-sdxl", arguments={"prompt": "a cute cat, realistic, orange"})
|
|
38
|
+
print(response["images"][0]["url"])
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Asynchronous requests
|
|
42
|
+
|
|
43
|
+
The client also supports asynchronous requests out of the box. Here's an example:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
import asyncio
|
|
47
|
+
import fal_client
|
|
48
|
+
|
|
49
|
+
async def main():
|
|
50
|
+
response = await fal_client.run_async("fal-ai/fast-sdxl", arguments={"prompt": "a cute cat, realistic, orange"})
|
|
51
|
+
print(response["images"][0]["url"])
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
asyncio.run(main())
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Uploading files
|
|
58
|
+
|
|
59
|
+
If the model requires files as input, you can upload them directly to fal.media (our CDN) and pass the URLs to the client. Here's an example:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import fal_client
|
|
63
|
+
|
|
64
|
+
audio_url = fal_client.upload_file("path/to/audio.wav")
|
|
65
|
+
response = fal_client.run("fal-ai/whisper", arguments={"audio_url": audio_url})
|
|
66
|
+
print(response["text"])
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Queuing requests
|
|
70
|
+
|
|
71
|
+
When you want to send a request and keep receiving updates on its status, you can use the `submit` method. Here's an example:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
import asyncio
|
|
75
|
+
import fal_client
|
|
76
|
+
|
|
77
|
+
async def main():
|
|
78
|
+
response = await fal_client.submit_async("fal-ai/fast-sdxl", arguments={"prompt": "a cute cat, realistic, orange"})
|
|
79
|
+
|
|
80
|
+
logs_index = 0
|
|
81
|
+
async for event in response.iter_events(with_logs=True):
|
|
82
|
+
if isinstance(event, fal_client.Queued):
|
|
83
|
+
print("Queued. Position:", event.position)
|
|
84
|
+
elif isinstance(event, (fal_client.InProgress, fal_client.Completed)):
|
|
85
|
+
new_logs = event.logs[logs_index:]
|
|
86
|
+
for log in new_logs:
|
|
87
|
+
print(log["message"])
|
|
88
|
+
logs_index = len(event.logs)
|
|
89
|
+
|
|
90
|
+
result = await response.get()
|
|
91
|
+
print(result["images"][0]["url"])
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
asyncio.run(main())
|
|
95
|
+
```
|
|
96
|
+
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# fal.ai Python client
|
|
2
|
+
|
|
3
|
+
This is a Python client library for interacting with ML models deployed on [fal.ai](https://fal.ai).
|
|
4
|
+
|
|
5
|
+
## Getting started
|
|
6
|
+
|
|
7
|
+
To install the client, run:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install fal-client
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
To use the client, you need to have an API key. You can get one by signing up at [fal.ai](https://fal.ai). Once you have it, set
|
|
14
|
+
it as an environment variable:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
export FAL_KEY=your-api-key
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Now you can use the client to interact with your models. Here's an example of how to use it:
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
import fal_client
|
|
24
|
+
|
|
25
|
+
response = fal_client.run("fal-ai/fast-sdxl", arguments={"prompt": "a cute cat, realistic, orange"})
|
|
26
|
+
print(response["images"][0]["url"])
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Asynchronous requests
|
|
30
|
+
|
|
31
|
+
The client also supports asynchronous requests out of the box. Here's an example:
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
import asyncio
|
|
35
|
+
import fal_client
|
|
36
|
+
|
|
37
|
+
async def main():
|
|
38
|
+
response = await fal_client.run_async("fal-ai/fast-sdxl", arguments={"prompt": "a cute cat, realistic, orange"})
|
|
39
|
+
print(response["images"][0]["url"])
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
asyncio.run(main())
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Uploading files
|
|
46
|
+
|
|
47
|
+
If the model requires files as input, you can upload them directly to fal.media (our CDN) and pass the URLs to the client. Here's an example:
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
import fal_client
|
|
51
|
+
|
|
52
|
+
audio_url = fal_client.upload_file("path/to/audio.wav")
|
|
53
|
+
response = fal_client.run("fal-ai/whisper", arguments={"audio_url": audio_url})
|
|
54
|
+
print(response["text"])
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Queuing requests
|
|
58
|
+
|
|
59
|
+
When you want to send a request and keep receiving updates on its status, you can use the `submit` method. Here's an example:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import asyncio
|
|
63
|
+
import fal_client
|
|
64
|
+
|
|
65
|
+
async def main():
|
|
66
|
+
response = await fal_client.submit_async("fal-ai/fast-sdxl", arguments={"prompt": "a cute cat, realistic, orange"})
|
|
67
|
+
|
|
68
|
+
logs_index = 0
|
|
69
|
+
async for event in response.iter_events(with_logs=True):
|
|
70
|
+
if isinstance(event, fal_client.Queued):
|
|
71
|
+
print("Queued. Position:", event.position)
|
|
72
|
+
elif isinstance(event, (fal_client.InProgress, fal_client.Completed)):
|
|
73
|
+
new_logs = event.logs[logs_index:]
|
|
74
|
+
for log in new_logs:
|
|
75
|
+
print(log["message"])
|
|
76
|
+
logs_index = len(event.logs)
|
|
77
|
+
|
|
78
|
+
result = await response.get()
|
|
79
|
+
print(result["images"][0]["url"])
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
asyncio.run(main())
|
|
83
|
+
```
|
|
84
|
+
|
|
@@ -30,8 +30,14 @@ sync_client = SyncClient()
|
|
|
30
30
|
run = sync_client.run
|
|
31
31
|
submit = sync_client.submit
|
|
32
32
|
stream = sync_client.stream
|
|
33
|
+
upload = sync_client.upload
|
|
34
|
+
upload_file = sync_client.upload_file
|
|
35
|
+
upload_image = sync_client.upload_image
|
|
33
36
|
|
|
34
37
|
async_client = AsyncClient()
|
|
35
38
|
run_async = async_client.run
|
|
36
39
|
submit_async = async_client.submit
|
|
37
40
|
stream_async = async_client.stream
|
|
41
|
+
upload_async = async_client.upload
|
|
42
|
+
upload_file_async = async_client.upload_file
|
|
43
|
+
upload_image_async = async_client.upload_image
|
|
@@ -2,14 +2,17 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
import io
|
|
4
4
|
import os
|
|
5
|
-
import httpx
|
|
6
5
|
import mimetypes
|
|
7
|
-
|
|
6
|
+
import asyncio
|
|
7
|
+
import time
|
|
8
8
|
from dataclasses import dataclass, field
|
|
9
9
|
from functools import cached_property
|
|
10
|
-
from fal_client.auth import FAL_RUN_HOST, fetch_credentials
|
|
11
10
|
from typing import Any, AsyncIterator, Iterator, TYPE_CHECKING
|
|
12
11
|
|
|
12
|
+
import httpx
|
|
13
|
+
from httpx_sse import aconnect_sse, connect_sse
|
|
14
|
+
from fal_client.auth import FAL_RUN_HOST, fetch_credentials
|
|
15
|
+
|
|
13
16
|
if TYPE_CHECKING:
|
|
14
17
|
from PIL import Image
|
|
15
18
|
|
|
@@ -19,6 +22,7 @@ RUN_URL_FORMAT = f"https://{FAL_RUN_HOST}/"
|
|
|
19
22
|
QUEUE_URL_FORMAT = f"https://queue.{FAL_RUN_HOST}/"
|
|
20
23
|
REALTIME_URL_FORMAT = f"wss://{FAL_RUN_HOST}/"
|
|
21
24
|
CDN_URL = "https://fal.media"
|
|
25
|
+
USER_AGENT = "fal-client/0.2.2 (python)"
|
|
22
26
|
|
|
23
27
|
|
|
24
28
|
@dataclass
|
|
@@ -27,16 +31,29 @@ class Status: ...
|
|
|
27
31
|
|
|
28
32
|
@dataclass
|
|
29
33
|
class Queued(Status):
|
|
34
|
+
"""Indicates the request is enqueued and waiting to be processed. The position
|
|
35
|
+
field indicates the relative position in the queue (0-indexed)."""
|
|
36
|
+
|
|
30
37
|
position: int
|
|
31
38
|
|
|
32
39
|
|
|
33
40
|
@dataclass
|
|
34
41
|
class InProgress(Status):
|
|
42
|
+
"""Indicates the request is currently being processed. If the status operation called
|
|
43
|
+
with the `with_logs` parameter set to True, the logs field will be a list of
|
|
44
|
+
log objects."""
|
|
45
|
+
|
|
46
|
+
# TODO: Type the log object structure so we can offer editor completion
|
|
35
47
|
logs: list[dict[str, Any]] | None = field()
|
|
36
48
|
|
|
37
49
|
|
|
38
50
|
@dataclass
|
|
39
51
|
class Completed(Status):
|
|
52
|
+
"""Indicates the request has been completed and the result can be gathered. The logs field will
|
|
53
|
+
contain the logs if the status operation was called with the `with_logs` parameter set to True. Metrics
|
|
54
|
+
might contain the inference time, and other internal metadata (number of tokens
|
|
55
|
+
processed, etc.)."""
|
|
56
|
+
|
|
40
57
|
logs: list[dict[str, Any]] | None = field()
|
|
41
58
|
metrics: dict[str, Any] = field()
|
|
42
59
|
|
|
@@ -48,7 +65,7 @@ class _BaseRequestHandle:
|
|
|
48
65
|
status_url: str = field(repr=False)
|
|
49
66
|
cancel_url: str = field(repr=False)
|
|
50
67
|
|
|
51
|
-
def
|
|
68
|
+
def _parse_status(self, data: AnyJSON) -> Status:
|
|
52
69
|
if data["status"] == "IN_QUEUE":
|
|
53
70
|
return Queued(position=data["queue_position"])
|
|
54
71
|
elif data["status"] == "IN_PROGRESS":
|
|
@@ -64,8 +81,9 @@ class SyncRequestHandle(_BaseRequestHandle):
|
|
|
64
81
|
client: httpx.Client = field(repr=False)
|
|
65
82
|
|
|
66
83
|
def status(self, *, with_logs: bool = False) -> Status:
|
|
67
|
-
"""
|
|
68
|
-
|
|
84
|
+
"""Returns the status of the request (which can be one of the following:
|
|
85
|
+
Queued, InProgress, Completed). If `with_logs` is True, logs will be included
|
|
86
|
+
for InProgress and Completed statuses."""
|
|
69
87
|
|
|
70
88
|
response = self.client.get(
|
|
71
89
|
self.status_url,
|
|
@@ -75,18 +93,25 @@ class SyncRequestHandle(_BaseRequestHandle):
|
|
|
75
93
|
)
|
|
76
94
|
response.raise_for_status()
|
|
77
95
|
|
|
78
|
-
return self.
|
|
96
|
+
return self._parse_status(response.json())
|
|
97
|
+
|
|
98
|
+
def iter_events(
|
|
99
|
+
self, *, with_logs: bool = False, interval: float = 0.1
|
|
100
|
+
) -> Iterator[Status]:
|
|
101
|
+
"""Continuously poll for the status of the request and yield it at each interval till
|
|
102
|
+
the request is completed. If `with_logs` is True, logs will be included in the response.
|
|
103
|
+
"""
|
|
79
104
|
|
|
80
|
-
def iter_events(self, *, with_logs: bool = False) -> Iterator[Status]:
|
|
81
|
-
"""Yield all events regarding the given task till its completed."""
|
|
82
105
|
while True:
|
|
83
106
|
status = self.status(with_logs=with_logs)
|
|
84
107
|
yield status
|
|
85
108
|
if isinstance(status, Completed):
|
|
86
109
|
break
|
|
87
110
|
|
|
111
|
+
time.sleep(interval)
|
|
112
|
+
|
|
88
113
|
def get(self) -> AnyJSON:
|
|
89
|
-
"""Wait till the request is completed and return the result."""
|
|
114
|
+
"""Wait till the request is completed and return the result of the inference call."""
|
|
90
115
|
for _ in self.iter_events(with_logs=False):
|
|
91
116
|
continue
|
|
92
117
|
|
|
@@ -100,8 +125,9 @@ class AsyncRequestHandle(_BaseRequestHandle):
|
|
|
100
125
|
client: httpx.AsyncClient = field(repr=False)
|
|
101
126
|
|
|
102
127
|
async def status(self, *, with_logs: bool = False) -> Status:
|
|
103
|
-
"""
|
|
104
|
-
|
|
128
|
+
"""Returns the status of the request (which can be one of the following:
|
|
129
|
+
Queued, InProgress, Completed). If `with_logs` is True, logs will be included
|
|
130
|
+
for InProgress and Completed statuses."""
|
|
105
131
|
|
|
106
132
|
response = await self.client.get(
|
|
107
133
|
self.status_url,
|
|
@@ -111,16 +137,23 @@ class AsyncRequestHandle(_BaseRequestHandle):
|
|
|
111
137
|
)
|
|
112
138
|
response.raise_for_status()
|
|
113
139
|
|
|
114
|
-
return self.
|
|
140
|
+
return self._parse_status(response.json())
|
|
141
|
+
|
|
142
|
+
async def iter_events(
|
|
143
|
+
self, *, with_logs: bool = False, interval: float = 0.1
|
|
144
|
+
) -> AsyncIterator[Status]:
|
|
145
|
+
"""Continuously poll for the status of the request and yield it at each interval till
|
|
146
|
+
the request is completed. If `with_logs` is True, logs will be included in the response.
|
|
147
|
+
"""
|
|
115
148
|
|
|
116
|
-
async def iter_events(self, *, with_logs: bool = False) -> AsyncIterator[Status]:
|
|
117
|
-
"""Yield all events regarding the given task till its completed."""
|
|
118
149
|
while True:
|
|
119
150
|
status = await self.status(with_logs=with_logs)
|
|
120
151
|
yield status
|
|
121
152
|
if isinstance(status, Completed):
|
|
122
153
|
break
|
|
123
154
|
|
|
155
|
+
await asyncio.sleep(interval)
|
|
156
|
+
|
|
124
157
|
async def get(self) -> AnyJSON:
|
|
125
158
|
"""Wait till the request is completed and return the result."""
|
|
126
159
|
async for _ in self.iter_events(with_logs=False):
|
|
@@ -143,7 +176,12 @@ class AsyncClient:
|
|
|
143
176
|
else:
|
|
144
177
|
key = self.key
|
|
145
178
|
|
|
146
|
-
return httpx.AsyncClient(
|
|
179
|
+
return httpx.AsyncClient(
|
|
180
|
+
headers={
|
|
181
|
+
"Authorization": f"Key {key}",
|
|
182
|
+
"User-Agent": USER_AGENT,
|
|
183
|
+
}
|
|
184
|
+
)
|
|
147
185
|
|
|
148
186
|
async def run(
|
|
149
187
|
self,
|
|
@@ -152,6 +190,10 @@ class AsyncClient:
|
|
|
152
190
|
*,
|
|
153
191
|
path: str = "",
|
|
154
192
|
) -> AnyJSON:
|
|
193
|
+
"""Run an application with the given arguments (which will be JSON serialized). The path parameter can be used to
|
|
194
|
+
specify a subpath when applicable. This method will return the result of the inference call directly.
|
|
195
|
+
"""
|
|
196
|
+
|
|
155
197
|
url = RUN_URL_FORMAT + application
|
|
156
198
|
if path:
|
|
157
199
|
url += "/" + path.lstrip("/")
|
|
@@ -171,6 +213,10 @@ class AsyncClient:
|
|
|
171
213
|
*,
|
|
172
214
|
path: str = "",
|
|
173
215
|
) -> AsyncRequestHandle:
|
|
216
|
+
"""Submit an application with the given arguments (which will be JSON serialized). The path parameter can be used to
|
|
217
|
+
specify a subpath when applicable. This method will return a handle to the request that can be used to check the status
|
|
218
|
+
and retrieve the result of the inference call when it is done."""
|
|
219
|
+
|
|
174
220
|
url = QUEUE_URL_FORMAT + application
|
|
175
221
|
if path:
|
|
176
222
|
url += "/" + path.lstrip("/")
|
|
@@ -198,6 +244,13 @@ class AsyncClient:
|
|
|
198
244
|
*,
|
|
199
245
|
path: str = "/stream",
|
|
200
246
|
) -> AsyncIterator[dict[str, Any]]:
|
|
247
|
+
"""Stream the output of an application with the given arguments (which will be JSON serialized). This is only supported
|
|
248
|
+
at a few select applications at the moment, so be sure to first consult with the documentation of individual applications
|
|
249
|
+
to see if this is supported.
|
|
250
|
+
|
|
251
|
+
The function will iterate over each event that is streamed from the server.
|
|
252
|
+
"""
|
|
253
|
+
|
|
201
254
|
url = RUN_URL_FORMAT + application
|
|
202
255
|
if path:
|
|
203
256
|
url += "/" + path.lstrip("/")
|
|
@@ -207,6 +260,9 @@ class AsyncClient:
|
|
|
207
260
|
yield event.json()
|
|
208
261
|
|
|
209
262
|
async def upload(self, data: str | bytes, content_type: str) -> str:
|
|
263
|
+
"""Upload the given data blob to the CDN and return the access URL. The content type should be specified
|
|
264
|
+
as the second argument. Use upload_file or upload_image for convenience."""
|
|
265
|
+
|
|
210
266
|
response = await self._client.post(
|
|
211
267
|
CDN_URL + "/files/upload",
|
|
212
268
|
data=data,
|
|
@@ -217,6 +273,8 @@ class AsyncClient:
|
|
|
217
273
|
return response.json()["access_url"]
|
|
218
274
|
|
|
219
275
|
async def upload_file(self, path: os.PathLike) -> str:
|
|
276
|
+
"""Upload a file from the local filesystem to the CDN and return the access URL."""
|
|
277
|
+
|
|
220
278
|
mime_type, _ = mimetypes.guess_type(path)
|
|
221
279
|
if mime_type is None:
|
|
222
280
|
mime_type = "application/octet-stream"
|
|
@@ -225,6 +283,8 @@ class AsyncClient:
|
|
|
225
283
|
return await self.upload(file.read(), mime_type)
|
|
226
284
|
|
|
227
285
|
async def upload_image(self, image: Image.Image, format: str = "jpeg") -> str:
|
|
286
|
+
"""Upload a pillow image object to the CDN and return the access URL."""
|
|
287
|
+
|
|
228
288
|
with io.BytesIO() as buffer:
|
|
229
289
|
image.save(buffer, format=format)
|
|
230
290
|
return await self.upload(buffer.getvalue(), f"image/{format}")
|
|
@@ -242,7 +302,10 @@ class SyncClient:
|
|
|
242
302
|
else:
|
|
243
303
|
key = self.key
|
|
244
304
|
return httpx.Client(
|
|
245
|
-
headers={
|
|
305
|
+
headers={
|
|
306
|
+
"Authorization": f"Key {key}",
|
|
307
|
+
"User-Agent": USER_AGENT,
|
|
308
|
+
},
|
|
246
309
|
timeout=self.default_timeout,
|
|
247
310
|
)
|
|
248
311
|
|
|
@@ -254,6 +317,10 @@ class SyncClient:
|
|
|
254
317
|
path: str = "",
|
|
255
318
|
timeout: float | None = None,
|
|
256
319
|
) -> AnyJSON:
|
|
320
|
+
"""Run an application with the given arguments (which will be JSON serialized). The path parameter can be used to
|
|
321
|
+
specify a subpath when applicable. This method will return the result of the inference call directly.
|
|
322
|
+
"""
|
|
323
|
+
|
|
257
324
|
url = RUN_URL_FORMAT + application
|
|
258
325
|
if path:
|
|
259
326
|
url += "/" + path.lstrip("/")
|
|
@@ -273,6 +340,10 @@ class SyncClient:
|
|
|
273
340
|
*,
|
|
274
341
|
path: str = "",
|
|
275
342
|
) -> SyncRequestHandle:
|
|
343
|
+
"""Submit an application with the given arguments (which will be JSON serialized). The path parameter can be used to
|
|
344
|
+
specify a subpath when applicable. This method will return a handle to the request that can be used to check the status
|
|
345
|
+
and retrieve the result of the inference call when it is done."""
|
|
346
|
+
|
|
276
347
|
url = QUEUE_URL_FORMAT + application
|
|
277
348
|
if path:
|
|
278
349
|
url += "/" + path.lstrip("/")
|
|
@@ -300,6 +371,13 @@ class SyncClient:
|
|
|
300
371
|
*,
|
|
301
372
|
path: str = "/stream",
|
|
302
373
|
) -> Iterator[dict[str, Any]]:
|
|
374
|
+
"""Stream the output of an application with the given arguments (which will be JSON serialized). This is only supported
|
|
375
|
+
at a few select applications at the moment, so be sure to first consult with the documentation of individual applications
|
|
376
|
+
to see if this is supported.
|
|
377
|
+
|
|
378
|
+
The function will iterate over each event that is streamed from the server.
|
|
379
|
+
"""
|
|
380
|
+
|
|
303
381
|
url = RUN_URL_FORMAT + application
|
|
304
382
|
if path:
|
|
305
383
|
url += "/" + path.lstrip("/")
|
|
@@ -309,6 +387,9 @@ class SyncClient:
|
|
|
309
387
|
yield event.json()
|
|
310
388
|
|
|
311
389
|
def upload(self, data: str | bytes, content_type: str) -> str:
|
|
390
|
+
"""Upload the given data blob to the CDN and return the access URL. The content type should be specified
|
|
391
|
+
as the second argument. Use upload_file or upload_image for convenience."""
|
|
392
|
+
|
|
312
393
|
response = self._client.post(
|
|
313
394
|
CDN_URL + "/files/upload",
|
|
314
395
|
data=data,
|
|
@@ -319,6 +400,8 @@ class SyncClient:
|
|
|
319
400
|
return response.json()["access_url"]
|
|
320
401
|
|
|
321
402
|
def upload_file(self, path: os.PathLike) -> str:
|
|
403
|
+
"""Upload a file from the local filesystem to the CDN and return the access URL."""
|
|
404
|
+
|
|
322
405
|
mime_type, _ = mimetypes.guess_type(path)
|
|
323
406
|
if mime_type is None:
|
|
324
407
|
mime_type = "application/octet-stream"
|
|
@@ -327,6 +410,8 @@ class SyncClient:
|
|
|
327
410
|
return self.upload(file.read(), mime_type)
|
|
328
411
|
|
|
329
412
|
def upload_image(self, image: Image.Image, format: str = "jpeg") -> str:
|
|
413
|
+
"""Upload a pillow image object to the CDN and return the access URL."""
|
|
414
|
+
|
|
330
415
|
with io.BytesIO() as buffer:
|
|
331
416
|
image.save(buffer, format=format)
|
|
332
417
|
return self.upload(buffer.getvalue(), f"image/{format}")
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: fal_client
|
|
3
|
+
Version: 0.2.2
|
|
4
|
+
Summary: Python client for fal.ai
|
|
5
|
+
Author: fal <hello@fal.ai>
|
|
6
|
+
Project-URL: homepage, https://fal.ai
|
|
7
|
+
Project-URL: repository, https://github.com/fal-ai/fal
|
|
8
|
+
Requires-Python: >=3.8
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
Requires-Dist: httpx<1,>=0.21.0
|
|
11
|
+
Requires-Dist: httpx-sse<0.5,>=0.4.0
|
|
12
|
+
|
|
13
|
+
# fal.ai Python client
|
|
14
|
+
|
|
15
|
+
This is a Python client library for interacting with ML models deployed on [fal.ai](https://fal.ai).
|
|
16
|
+
|
|
17
|
+
## Getting started
|
|
18
|
+
|
|
19
|
+
To install the client, run:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install fal-client
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
To use the client, you need to have an API key. You can get one by signing up at [fal.ai](https://fal.ai). Once you have it, set
|
|
26
|
+
it as an environment variable:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
export FAL_KEY=your-api-key
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Now you can use the client to interact with your models. Here's an example of how to use it:
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import fal_client
|
|
36
|
+
|
|
37
|
+
response = fal_client.run("fal-ai/fast-sdxl", arguments={"prompt": "a cute cat, realistic, orange"})
|
|
38
|
+
print(response["images"][0]["url"])
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Asynchronous requests
|
|
42
|
+
|
|
43
|
+
The client also supports asynchronous requests out of the box. Here's an example:
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
import asyncio
|
|
47
|
+
import fal_client
|
|
48
|
+
|
|
49
|
+
async def main():
|
|
50
|
+
response = await fal_client.run_async("fal-ai/fast-sdxl", arguments={"prompt": "a cute cat, realistic, orange"})
|
|
51
|
+
print(response["images"][0]["url"])
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
asyncio.run(main())
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Uploading files
|
|
58
|
+
|
|
59
|
+
If the model requires files as input, you can upload them directly to fal.media (our CDN) and pass the URLs to the client. Here's an example:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import fal_client
|
|
63
|
+
|
|
64
|
+
audio_url = fal_client.upload_file("path/to/audio.wav")
|
|
65
|
+
response = fal_client.run("fal-ai/whisper", arguments={"audio_url": audio_url})
|
|
66
|
+
print(response["text"])
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Queuing requests
|
|
70
|
+
|
|
71
|
+
When you want to send a request and keep receiving updates on its status, you can use the `submit` method. Here's an example:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
import asyncio
|
|
75
|
+
import fal_client
|
|
76
|
+
|
|
77
|
+
async def main():
|
|
78
|
+
response = await fal_client.submit_async("fal-ai/fast-sdxl", arguments={"prompt": "a cute cat, realistic, orange"})
|
|
79
|
+
|
|
80
|
+
logs_index = 0
|
|
81
|
+
async for event in response.iter_events(with_logs=True):
|
|
82
|
+
if isinstance(event, fal_client.Queued):
|
|
83
|
+
print("Queued. Position:", event.position)
|
|
84
|
+
elif isinstance(event, (fal_client.InProgress, fal_client.Completed)):
|
|
85
|
+
new_logs = event.logs[logs_index:]
|
|
86
|
+
for log in new_logs:
|
|
87
|
+
print(log["message"])
|
|
88
|
+
logs_index = len(event.logs)
|
|
89
|
+
|
|
90
|
+
result = await response.get()
|
|
91
|
+
print(result["images"][0]["url"])
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
asyncio.run(main())
|
|
95
|
+
```
|
|
96
|
+
|
fal_client-0.2.0/PKG-INFO
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: fal_client
|
|
3
|
-
Version: 0.2.0
|
|
4
|
-
Summary: Python client for fal.ai
|
|
5
|
-
Author: fal <hello@fal.ai>
|
|
6
|
-
Project-URL: homepage, https://fal.ai
|
|
7
|
-
Project-URL: repository, https://github.com/fal-ai/fal
|
|
8
|
-
Requires-Python: >=3.8
|
|
9
|
-
Description-Content-Type: text/markdown
|
|
10
|
-
Requires-Dist: httpx<1,>=0.21.0
|
|
11
|
-
Requires-Dist: httpx-sse<0.5,>=0.4.0
|
|
12
|
-
|
|
13
|
-
# fal.ai Python client
|
|
14
|
-
|
|
15
|
-
This is a Python client library for interacting with ML models deployed on [fal.ai](https://fal.ai).
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
fal_client-0.2.0/README.md
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: fal_client
|
|
3
|
-
Version: 0.2.0
|
|
4
|
-
Summary: Python client for fal.ai
|
|
5
|
-
Author: fal <hello@fal.ai>
|
|
6
|
-
Project-URL: homepage, https://fal.ai
|
|
7
|
-
Project-URL: repository, https://github.com/fal-ai/fal
|
|
8
|
-
Requires-Python: >=3.8
|
|
9
|
-
Description-Content-Type: text/markdown
|
|
10
|
-
Requires-Dist: httpx<1,>=0.21.0
|
|
11
|
-
Requires-Dist: httpx-sse<0.5,>=0.4.0
|
|
12
|
-
|
|
13
|
-
# fal.ai Python client
|
|
14
|
-
|
|
15
|
-
This is a Python client library for interacting with ML models deployed on [fal.ai](https://fal.ai).
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|