Giter VIP home page Giter VIP logo

fal-swift's Introduction

The fal.ai Swift Client

Swift Build License

About the Project

The FalClient is a robust and user-friendly Swift package designed for seamless integration of fal serverless functions into Swift projects. This library, developed in pure Swift, provides developers with simple APIs to interact with AI models, suitable for both iOS and macOS platforms.

Getting Started

The FalClient library serves as a client for fal serverless Python functions. Before using this library, ensure you've set up your serverless functions as per the quickstart guide.

Client Library

This Swift client library is crafted as a lightweight layer atop Swift's networking standards like URLSession. It ensures hassle-free integration into your existing Swift codebase. The library is designed to address the nuances of Swift and Apple's ecosystem, ensuring smooth operation across different Apple platforms.

Note: Make sure to review the fal-serverless getting started guide to acquire your credentials and register your functions.

  1. Add FalClient as a dependency in your Swift Package Manager.

  2. Set up the client instance:

    import FalClient
    let fal = FalClient.withCredentials(.keyPair("FAL_KEY_ID:FAL_KEY_SECRET"))
    
    // You can also use a proxy to protect your credentials
    // let fal = FalClient.withProxy("http://localhost:3333/api/fal/proxy")
  3. Use fal.subscribe to dispatch requests to the model API:

    let result = try await fal.subscribe(to: "text-to-image",
        input: [
            "prompt": "a cute shih-tzu puppy",
            "model_name": "stabilityai/stable-diffusion-xl-base-1.0",
            "image_size": "square_hd"
        ]) { update in
            print(update)
        }

Notes:

  • Replace text-to-image with a valid model id. Check fal.ai/models for all available models.
  • It fully relies on async/await for asynchronous programming.
  • The result type in Swift will be a [String: Any] and the entries depend on the API output schema.
  • The Swift client also supports typed inputs and outputs through Codable.

Real-time

The client supports real-time model APIs. Checkout the FalRealtimeSampleApp for more details.

let connection = try fal.realtime.connect(
    to: OptimizedLatentConsistency,
    connectionKey: "PencilKitDemo",
    throttleInterval: .milliseconds(128)
) { (result: Result<LcmResponse, Error>)  in
    if case let .success(data) = result,
        let image = data.images.first {
        let data = try? Data(contentsOf: URL(string: image.url)!)
        DispatchQueue.main.async {
            self.currentImage = data
        }
    }
}

try connection.send(LcmInput(
    prompt: prompt,
    imageUrl: "data:image/jpeg;base64,\(drawing.base64EncodedString())",
    seed: 6_252_023,
    syncMode: true
))

Sample apps

Check the Sources/Samples folder for a handful of sample applications using the FalClient.

Open them with xed to quickly start playing with

xed Sources/Samples/FalSampleApp

Roadmap

See the open feature requests for a list of proposed features and join the discussion.

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make to the Swift version of the client are greatly appreciated.

License

Distributed under the MIT License. See LICENSE for more information.

fal-swift's People

Contributors

drochetti avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.