Skip to main content

Python client for fal.ai

Project description

fal.ai Python client

This is a Python client library for interacting with ML models deployed on fal.ai.

Getting started

To install the client, run:

pip install fal-client

To use the client, you need to have an API key. You can get one by signing up at fal.ai. Once you have it, set it as an environment variable:

export FAL_KEY=your-api-key

Now you can use the client to interact with your models. Here's an example of how to use it:

import fal_client

response = fal_client.run("fal-ai/fast-sdxl", arguments={"prompt": "a cute cat, realistic, orange"})
print(response["images"][0]["url"])

Asynchronous requests

The client also supports asynchronous requests out of the box. Here's an example:

import asyncio
import fal_client

async def main():
    response = await fal_client.run_async("fal-ai/fast-sdxl", arguments={"prompt": "a cute cat, realistic, orange"})
    print(response["images"][0]["url"])


asyncio.run(main())

Uploading files

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:

import fal_client

audio_url = fal_client.upload_file("path/to/audio.wav")
response = fal_client.run("fal-ai/whisper", arguments={"audio_url": audio_url})
print(response["text"])

Encoding files as in-memory data URLs

If you don't want to upload your file to our CDN service (for latency reasons, for example), you can encode it as a data URL and pass it directly to the client. Here's an example:

import fal_client

audio_data_url = fal_client.encode_file("path/to/audio.wav")
response = fal_client.run("fal-ai/whisper", arguments={"audio_url": audio_data_url})
print(response["text"])

Queuing requests

When you want to send a request and keep receiving updates on its status, you can use the submit method. Here's an example:

import asyncio
import fal_client

async def main():
    response = await fal_client.submit_async("fal-ai/fast-sdxl", arguments={"prompt": "a cute cat, realistic, orange"})

    logs_index = 0
    async for event in response.iter_events(with_logs=True):
        if isinstance(event, fal_client.Queued):
            print("Queued. Position:", event.position)
        elif isinstance(event, (fal_client.InProgress, fal_client.Completed)):
            new_logs = event.logs[logs_index:]
            for log in new_logs:
                print(log["message"])
            logs_index = len(event.logs)

    result = await response.get()
    print(result["images"][0]["url"])


asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fal_client-0.5.2.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

fal_client-0.5.2-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file fal_client-0.5.2.tar.gz.

File metadata

  • Download URL: fal_client-0.5.2.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for fal_client-0.5.2.tar.gz
Algorithm Hash digest
SHA256 2a89d4b9a526affa0a3ed4b639a38a9545989a627afd6a9c518382b27c06073d
MD5 444da3448abe100897ca6e3cb3c980be
BLAKE2b-256 413433ed6383c7f3f2bfda4d981502b4804ba5a19c8e1cee0a07ad24ab79ae0f

See more details on using hashes here.

File details

Details for the file fal_client-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: fal_client-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for fal_client-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 432579a1b83548159b121f553ed2f06f1991d08b0e36b4007c31083672b5fe7d
MD5 010b438977d1e925b9e0262205a9d464
BLAKE2b-256 b993a1ba8cc0b4a551cec9d18acf23d0ffe376b1f922b4c9218b832d6588cbef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page