stability_ai_dart 1.0.3 copy "stability_ai_dart: ^1.0.3" to clipboard
stability_ai_dart: ^1.0.3 copied to clipboard

A comprehensive Dart library for the Stability AI REST API. Supports text-to-image, upscaling, and SD 3.0/3.5.

example/stability_ai_dart_example.dart

import 'dart:io';
import 'dart:convert';
import 'package:stability_ai_dart/stability_ai_dart.dart';

// This example demonstrates basic usage of the Stability AI Dart package
void main() async {
  // Initialize the client
  final client = StabilityAiFactory.create(
    apiKey: 'your-api-key-here',
  );

  try {
    // List available engines
    print('\nšŸ“‹ Listing available engines:');
    final engines = await client.listEngines();
    for (final engine in engines) {
      print('${engine.id}: ${engine.name}');
      print('Description: ${engine.description}');
      print('Type: ${engine.type}');
      print('Status: ${engine.ready ? 'Ready' : 'Not Ready'}\n');
    }

    // Generate an image using text prompts
    print('šŸŽØ Generating an image from text...');
    final request = TextToImageRequest(
      textPrompts: [
        TextPrompt(text: 'A serene mountain lake at sunset'),
        TextPrompt(text: 'vibrant colors', weight: 0.5),
      ],
      height: 512,
      width: 512,
      numberOfSamples: 1,
      numberOfSteps: 50,
      cfgScale: 7.0,
    );

    final response = await client.generateImage(
      engineId: 'stable-diffusion-v1-5',
      request: request,
    );

    // Save the generated image
    for (final artifact in response.artifacts) {
      final imageBytes = base64Decode(artifact.base64);
      await File('example_output.png').writeAsBytes(imageBytes);
      print('āœ… Image generated and saved as example_output.png');
      print('Seed used: ${artifact.seed}');
    }

    // Generate an image using the Core API
    print('\nšŸ–¼ļø Generating an image using Core API...');
    final coreRequest = CoreImageRequest(
      prompt: 'A lighthouse on a cliff overlooking the ocean',
      negativePrompt: 'blur, haze, fog',
      aspectRatio: AspectRatio.ratio16x9,
      outputFormat: OutputFormat.png,
      stylePreset: StylePreset.photographic,
    );

    final coreResult = await client.generateCoreImage(
      request: coreRequest,
      returnJson: false,
    );

    if (coreResult is CoreImageBytes) {
      await File('example_core_output.png').writeAsBytes(coreResult.bytes);
      print('āœ… Core image generated and saved as example_core_output.png');
    }
  } on StabilityAiException catch (e) {
    print('āŒ API error: ${e.statusCode} - ${e.message}');
    if (e.id != null) print('Error ID: ${e.id}');
    if (e.name != null) print('Error name: ${e.name}');
  } finally {
    // Clean up
    client.close();
  }
}
0
likes
160
points
40
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A comprehensive Dart library for the Stability AI REST API. Supports text-to-image, upscaling, and SD 3.0/3.5.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

http, http_parser, json_annotation

More

Packages that depend on stability_ai_dart