Skip to content

Commit

Permalink
chore: add build workflow (#1)
Browse files Browse the repository at this point in the history
* chore: add build workflow

* chore: test macos runtime

* fix: swift version on build

* fix: swift version 5.9

* chore: add swiftformat

* fix: remove build sample app for now

* chore: add license etc

* chore: sample working version
  • Loading branch information
drochetti authored Nov 26, 2023
1 parent eab88fe commit b56e8d2
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 8 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Build

on:
push:
branches:
- main
pull_request:

jobs:
build:
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- uses: swift-actions/setup-swift@v1
with:
swift-version: "5.9"
- name: Check format
run: swift package plugin --allow-writing-to-package-directory swiftformat .
- name: Build Library
run: swift build --target FalClient --configuration release
# - name: Build Sample App
# run: xcodebuild -project Sources/FalSampleApp/FalSampleApp.xcodeproj -scheme FalSampleApp
132 changes: 132 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Contributor Covenant Code of Conduct

## Our Pledge

We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, caste, color, religion, or sexual
identity and orientation.

We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.

## Our Standards

Examples of behavior that contributes to a positive environment for our
community include:

* Demonstrating empathy and kindness toward other people
* Being respectful of differing opinions, viewpoints, and experiences
* Giving and gracefully accepting constructive feedback
* Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
* Focusing on what is best not just for us as individuals, but for the overall
community

Examples of unacceptable behavior include:

* The use of sexualized language or imagery, and sexual attention or advances of
any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email address,
without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Enforcement Responsibilities

Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.

Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.

## Scope

This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
[INSERT CONTACT METHOD].
All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the
reporter of any incident.

## Enforcement Guidelines

Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:

### 1. Correction

**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.

**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.

### 2. Warning

**Community Impact**: A violation through a single incident or series of
actions.

**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or permanent
ban.

### 3. Temporary Ban

**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.

**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.

### 4. Permanent Ban

**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.

**Consequence**: A permanent ban from any sort of public interaction within the
community.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.1, available at
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].

Community Impact Guidelines were inspired by
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].

For answers to common questions about this code of conduct, see the FAQ at
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
[https://www.contributor-covenant.org/translations][translations].

[homepage]: https://www.contributor-covenant.org
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
[Mozilla CoC]: https://github.com/mozilla/diversity
[FAQ]: https://www.contributor-covenant.org/faq
[translations]: https://www.contributor-covenant.org/translations
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2023 https://fal.ai

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14 changes: 14 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "swiftformat",
"kind" : "remoteSourceControl",
"location" : "https://github.com/nicklockwood/SwiftFormat",
"state" : {
"revision" : "d37a477177d5d4ff2a3ae6328eaaab5bf793e702",
"version" : "0.52.9"
}
}
],
"version" : 2
}
3 changes: 3 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ let package = Package(
targets: ["FalClient"]
),
],
dependencies: [
.package(url: "https://github.com/nicklockwood/SwiftFormat", from: "0.50.4"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
Expand Down
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## The fal.ai Swift Client

![FalClient Swift package](https://img.shields.io/badge/swift-package-brightgreen)
![Build](https://img.shields.io/badge/build-passing-brightgreen)
![License](https://img.shields.io/badge/license-MIT-blue)

## 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](https://fal.ai/docs).

### 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](https://fal.ai/docs) 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:
```swift
import FalClient
let fal = FalClient(withCredentials: "FAL_KEY_ID:FAL_KEY_SECRET")
```

3. Use `fal.subscribe` to dispatch requests to the model API:
```swift
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](https://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`.

## Roadmap

See the [open feature requests](https://github.com/fal-ai/serverless-client-swift/labels/enhancement) 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](https://github.com/fal-ai/serverless-client-swift/blob/main/LICENSE) for more information.
27 changes: 23 additions & 4 deletions Sources/FalClient/FalClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ func buildUrl(fromId id: String, path: String? = nil) -> String {
"https://\(id).gateway.alpha.fal.ai" + (path ?? "")
}

/// The main client class that provides access to simple API model usage,
/// as well as access to the `queue` and `storage` APIs.
///
/// Example:
///
/// ```swift
/// import FalClient
///
/// let fal = FalClient.withCredentials("fal_key_id:fal_key_secret");
///
/// void main() async {
/// // check https://fal.ai/models for the available models
/// final result = await fal.subscribe(to: 'text-to-image', input: {
/// 'prompt': 'a cute shih-tzu puppy',
/// 'model_name': 'stabilityai/stable-diffusion-xl-base-1.0',
/// });
/// print(result);
/// }
/// ```
public struct FalClient: Client {
public let config: ClientConfig

Expand Down Expand Up @@ -49,10 +68,10 @@ public struct FalClient: Client {
}
return try await queue.response(id, of: requestId)
}
}

public static let shared: Client = Self(config: ClientConfig())

public static func withProxy(_ url: String) -> Client {
return Self(config: ClientConfig(requestProxy: url))
public extension FalClient {
static func withProxy(_ url: String) -> FalClient {
return FalClient(config: ClientConfig(requestProxy: url))
}
}
4 changes: 1 addition & 3 deletions Sources/FalSampleApp/FalSampleApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ struct ContentView: View {
print("Generate image...")
isLoading = true
do {
let result = try await fal.subscribe("110602490-lora", input: [
"model_name": "stabilityai/stable-diffusion-xl-base-1.0",
"image_size": "square_hd",
let result = try await fal.subscribe("110602490-fast-sdxl", input: [
"prompt": PROMPT,
], includeLogs: true) { update in
print(update)
Expand Down
2 changes: 1 addition & 1 deletion Sources/FalSampleApp/FalSampleApp/fal.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import FalClient

let fal = FalClient.withProxy("http://localhost:3333/api/_fal/proxy")
let fal = FalClient.withProxy("http://localhost:3333/api/fal/proxy")

0 comments on commit b56e8d2

Please sign in to comment.