cloudflare_ai 1.1.0 copy "cloudflare_ai: ^1.1.0" to clipboard
cloudflare_ai: ^1.1.0 copied to clipboard

This is a Dart package for Cloudflare Workers AI. It currently supports Text Generation and Image Generation, Text Summarization, and Image Generation Models.

example/cloudflare_ai_example.dart

import 'dart:io';
import 'dart:typed_data';
import 'package:cloudflare_ai/cloudflare_ai.dart';

void main() async {
  String accountId = "YOUR_ACCOUNT_ID";
  String apiKey = "YOUR_API_KEY";

  // Text Generation
  // Initialize a TextGenerationModel
  TextGenerationModel textGenModel = TextGenerationModel(
    accountId: accountId,
    apiKey: apiKey,
    model: TextGenerationModels.GEMMA_7B_IT,
  );

  // Generate Text for a prompt
  TextGenerationResponse textGenRes = await textGenModel
      .generateText("Write a story about a robot, living on the moon");

  if (textGenRes.success) {
    print(textGenRes.result.response);
  } else {
    print(textGenRes.errors);
  }

  // Text Summarization
  // Initialize a TextSummarizationModel
  TextSummarizationModel textSummarizationModel = TextSummarizationModel(
    accountId: accountId,
    apiKey: apiKey,
    model: TextSummarizationModels
        .BART_LARGE_CNN, // Bart Large CNN is default, hence this parameter is optional
  );

  // Summarize Text
  TextSummarizationResponse textSummarizationRes =
      await textSummarizationModel.summarize(
    "Your very long text....",
    maxLength: 1024, // 1024 is default, hence this parameter is optional
  );

  if (textSummarizationRes.success) {
    print(textSummarizationRes.result.response);
  } else {
    print(textSummarizationRes.errors);
  }

  // Text to Image
  // Initialize a TextToImageModel
  TextToImageModel textToImageModel = TextToImageModel(
    accountId: accountId,
    apiKey: apiKey,
    model: TextToImageModels.DREAMSHAPER_8_LCM,
  );

  // Generate Image
  Uint8List textToImageResult =
      await textToImageModel.generateImage("An alien on the moon");

  // Save the image to a file
  File("image.png").writeAsBytes(textToImageResult);

  // Text Classification
  // Initialize a TextClassificationModel
  TextClassificationModel textClassificationModel = TextClassificationModel(
      accountId: accountId,
      apiKey: apiKey,
      model: TextClassificationModels
          .DISTILBERT_SST_2_INT8 // DISTILBERT_SST_2_INT8 is default, hence this parameter is optional
      );

  // Initialize TextClassificationResponse
  TextClassificationResponse textClassificationResponse =
      await textClassificationModel.classifyText(
    "Test Prompt",
  );

  // Printing the output
  if (textClassificationResponse.success) {
    print(
        'Positive Confidence level: ${textClassificationResponse.result.positive}');
    print(
        'Negative Confidence level: ${textClassificationResponse.result.negative}');
  } else {
    print(textSummarizationRes.errors);
  }
}
3
likes
0
pub points
37%
popularity

Publisher

verified publishermkdir.software

This is a Dart package for Cloudflare Workers AI. It currently supports Text Generation and Image Generation, Text Summarization, and Image Generation Models.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

dio

More

Packages that depend on cloudflare_ai