Skip to content

Commit

Permalink
chore: move model types from main
Browse files Browse the repository at this point in the history
  • Loading branch information
drochetti committed Nov 6, 2023
1 parent 2bf3449 commit 5dd6eea
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 48 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ This client library is crafted as a lightweight layer atop platform standards li
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",
"image_size": "square_hd"
});
final result = await fal.subscribe('110602490-lora',
input: {
'prompt': 'a cute shih-tzu puppy',
'model_name': 'stabilityai/stable-diffusion-xl-base-1.0',
'image_size': 'square_hd'
},
onQueueUpdate: (update) => {print(update)}
);
```

**Notes:**
Expand Down
56 changes: 13 additions & 43 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import 'package:fal_client/fal_client.dart';
import 'package:flutter/material.dart';

import 'types.dart';

// You can use the proxyUrl to protect your credentials in production.
// final fal = FalClient.withProxy("http://localhost:3333/api/fal/proxy");
// final fal = FalClient.withProxy('http://localhost:3333/api/_fal/proxy');

// You can also use the credentials locally for development, but make sure
// you protected your credentials behind a proxy in production.
final fal = FalClient.withCredentials("FAL_KEY_ID:FAL_KEY_SECRET");
final fal = FalClient.withCredentials('FAL_KEY_ID:FAL_KEY_SECRET');

void main() {
runApp(const MyApp());
Expand All @@ -29,38 +31,6 @@ class MyApp extends StatelessWidget {
}
}

class TextToImageResult {
final List<ImageRef> images;
final int seed;

TextToImageResult({required this.images, required this.seed});

factory TextToImageResult.fromMap(Map<String, dynamic> json) {
return TextToImageResult(
images: (json['images'] as List<dynamic>)
.map((e) => ImageRef.fromMap(e as Map<String, dynamic>))
.toList(),
seed: json['seed'],
);
}
}

class ImageRef {
final String url;
final int height;
final int width;

ImageRef({required this.url, required this.height, required this.width});

factory ImageRef.fromMap(Map<String, dynamic> json) {
return ImageRef(
url: json['url'],
height: json['height'],
width: json['width'],
);
}
}

class TextoToImageScreen extends StatefulWidget {
const TextoToImageScreen({super.key, required this.title});

Expand All @@ -71,7 +41,7 @@ class TextoToImageScreen extends StatefulWidget {
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
// always marked 'final'.

final String title;

Expand All @@ -82,21 +52,21 @@ class TextoToImageScreen extends StatefulWidget {
class _TextoToImageScreenState extends State<TextoToImageScreen> {
bool _isLoading = false;
final _promptController =
TextEditingController(text: "a cute shih-tzu puppy");
TextEditingController(text: 'a cute shih-tzu puppy');
ImageRef? _image;

void _generateImage() async {
setState(() {
_isLoading = true;
_image = null;
});
final result = await fal.subscribe("110602490-lora", input: {
"prompt": _promptController.text,
"model_name": "stabilityai/stable-diffusion-xl-base-1.0",
"image_size": "square_hd"
}, onQueueUpdate: (update) => {
print(update)
});
final result = await fal.subscribe(textToImageId,
input: {
'prompt': _promptController.text,
'model_name': 'stabilityai/stable-diffusion-xl-base-1.0',
'image_size': 'square_hd'
},
onQueueUpdate: (update) => {print(update)});
setState(() {
_isLoading = false;
_image = TextToImageResult.fromMap(result).images[0];
Expand Down
33 changes: 33 additions & 0 deletions example/lib/types.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class TextToImageResult {
final List<ImageRef> images;
final int seed;

TextToImageResult({required this.images, required this.seed});

factory TextToImageResult.fromMap(Map<String, dynamic> json) {
return TextToImageResult(
images: (json['images'] as List<dynamic>)
.map((e) => ImageRef.fromMap(e as Map<String, dynamic>))
.toList(),
seed: (json['seed'] * 1).round(),
);
}
}

class ImageRef {
final String url;
final int height;
final int width;

ImageRef({required this.url, required this.height, required this.width});

factory ImageRef.fromMap(Map<String, dynamic> json) {
return ImageRef(
url: json['url'],
height: (json['height'] * 1).round(),
width: (json['width'] * 1).round(),
);
}
}

const textToImageId = '110602490-lora';

0 comments on commit 5dd6eea

Please sign in to comment.