Skip to content

Commit

Permalink
feat: web support (#2)
Browse files Browse the repository at this point in the history
* chore: update package topics

* fix: pubspec description length

* fix: package metadata

* fix: docs

* feat: proper web support

* chore: disable publish workflow
  • Loading branch information
drochetti authored Nov 5, 2023
1 parent 8555b6d commit 2dc4c6f
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 66 deletions.
13 changes: 0 additions & 13 deletions .github/workflows/publish.yml

This file was deleted.

2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### v0.1.0
## v0.1.0

### What's Changed
* feat: add build workflow by @drochetti in https://github.com/fal-ai/serverless-client-dart/pull/1
Expand Down
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,32 @@ This client library is crafted as a lightweight layer atop platform standards li
1. Start by adding `fal_client` as a dependency:

```sh
flutter pub add fal_client
```
```sh
flutter pub add fal_client
```

2. Setup the client instance:

```dart
import 'package:fal_client/client.dart';
```dart
import 'package:fal_client/client.dart';
final fal = FalClient.withCredentials("FAL_KEY_ID:FAL_KEY_SECRET");
```
final fal = FalClient.withCredentials('FAL_KEY_ID:FAL_KEY_SECRET');
```

3. Now use `fal.subcribe` to dispatch requests to the model API:

```dart
final result = await fal.subscribe('text-to-image', input: {
'prompt': 'a cute shih-tzu puppy',
'model_name': 'stabilityai/stable-diffusion-xl-base-1.0',
});
```
```dart
final result = await fal.subscribe('text-to-image', input: {
'prompt': 'a cute shih-tzu puppy',
'model_name': 'stabilityai/stable-diffusion-xl-base-1.0',
});
```

The result type is a `Map<String, dynamic>` and the entries depend on the API output schema. Types in Python are mapped to their corresponding types in Dart. Check [fal.ai/models](https://fal.ai/models) for all available model APIs.
**Notes:**

- Replace `text-to-image` with a valid model id. Check [fal.ai/models](https://fal.ai/models) for all available models.
- The result type is a `Map<String, dynamic>` and the entries depend on the API output schema.
- Types in Python are mapped to their corresponding types in Dart (e.g. `str` -> `String`).

## Roadmap

Expand Down
10 changes: 5 additions & 5 deletions lib/http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:http/http.dart' as http;

import './config.dart';
import './exception.dart';
import './runtime.dart';
import './runtime/platform.dart';

bool isValidUrl(String url) {
try {
Expand All @@ -14,6 +14,8 @@ bool isValidUrl(String url) {
}
}

final platform = PlatformInfo();

/// Builds a URL for the given [id], an optional [path], using the [config] to determine
/// the host and input when present.
String buildUrl(
Expand Down Expand Up @@ -59,14 +61,12 @@ Future<Map<String, dynamic>> sendRequest(
'Accept': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
};
headers[platform.userAgentHeader] = platform.userAgent;
if (config.credentials.trim().isNotEmpty) {
headers['Authorization'] = 'Key ${config.credentials}';
}
if (config.proxyUrl != null) {
headers['x-fal-target-url'] = url;
}
if (getUserAgent() != null) {
headers['User-Agent'] = getUserAgent()!;
headers['X-Fal-Target-Url'] = url;
}

final request = http.Request(
Expand Down
24 changes: 0 additions & 24 deletions lib/runtime.dart

This file was deleted.

3 changes: 3 additions & 0 deletions lib/runtime/platform.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'platform/interface.dart'
if (dart.library.html) 'platform/html.dart'
if (dart.library.io) 'platform/io.dart';
9 changes: 9 additions & 0 deletions lib/runtime/platform/base.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import '../version.dart';

abstract class BasePlatformInfo {
String get userAgent;

String get userAgentHeader;
}

const userAgentPrefix = 'fal.ai/dart-client@${packageVersion}';
18 changes: 18 additions & 0 deletions lib/runtime/platform/html.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:html';

import './base.dart';

class PlatformInfo extends BasePlatformInfo {
String? _memoizedUserAgent;

@override
String get userAgent {
if (_memoizedUserAgent == null) {
_memoizedUserAgent = '$userAgentPrefix - ${window.navigator.userAgent}';
}
return _memoizedUserAgent!;
}

@override
String get userAgentHeader => "X-Fal-User-Agent";
}
9 changes: 9 additions & 0 deletions lib/runtime/platform/interface.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import './base.dart';

class PlatformInfo extends BasePlatformInfo {
@override
String get userAgent => throw UnimplementedError();

@override
String get userAgentHeader => throw UnimplementedError();
}
19 changes: 19 additions & 0 deletions lib/runtime/platform/io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'dart:io';

import './base.dart';

class PlatformInfo extends BasePlatformInfo {
String? _memoizedUserAgent;

@override
String get userAgent {
if (_memoizedUserAgent == null) {
_memoizedUserAgent =
'$userAgentPrefix - ${Platform.operatingSystem} ${Platform.operatingSystemVersion}';
}
return _memoizedUserAgent!;
}

@override
String get userAgentHeader => "User-Agent";
}
1 change: 1 addition & 0 deletions lib/runtime/version.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const packageVersion = '0.2.0';
16 changes: 7 additions & 9 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
name: fal_client
description: A client library for fal.ai ML APIs
version: 0.1.0
description: >
The Dart client library for fal.ai model APIs.
You can use it to call multiple AI models on your Dart and Flutter apps.
version: 0.2.0
homepage: https://fal.ai
repository: https://github.com/fal-ai/serverless-client-dart
issue_tracker: https://github.com/fal-ai/serverless-client-dart/issues?q=is%3Aissue+is%3Aopen
topics:
- machine learning
- artificial intelligence
- ml
- ai
- fal
- stable-diffusion
- sdxl
- whisper
- text-to-image
- image-to-text
- speech-to-text

environment:
sdk: '>=3.1.5 <4.0.0'

dependencies:
cross_file: ^0.3.3+6
http: ^1.1.0
platform: ^3.1.3

dev_dependencies:

0 comments on commit 2dc4c6f

Please sign in to comment.