generateStream method
Generate an image for prompt, reporting progress per denoising step.
Throws SDKException into the consumer when generation fails.
Implementation
Stream<ImageEvent> generateStream(
String prompt, {
ImageOptions? options,
}) async* {
final effective = options ?? ImageOptions();
await ModelGate.ensureLoaded(
modelId: null,
category: ModelCategory.MODEL_CATEGORY_IMAGE_GENERATION,
);
yield const ImageStarted();
await for (final event in RunAnywhereDiffusion.shared.generateImageStream(
effective.toProto(prompt),
)) {
switch (event.kind) {
case DiffusionStreamEventKind.DIFFUSION_STREAM_EVENT_KIND_PROGRESS:
case DiffusionStreamEventKind
.DIFFUSION_STREAM_EVENT_KIND_INTERMEDIATE_IMAGE:
if (event.hasProgress()) {
yield ImageProgress(
step: event.progress.currentStep,
totalSteps: event.progress.totalSteps,
partialImage: event.progress.hasIntermediateImageData()
? Uint8List.fromList(event.progress.intermediateImageData)
: null,
);
}
case DiffusionStreamEventKind.DIFFUSION_STREAM_EVENT_KIND_COMPLETED:
if (event.hasResult()) {
yield ImageCompleted(
ImageResult.fromProto(event.result, steps: effective.steps),
);
}
case DiffusionStreamEventKind.DIFFUSION_STREAM_EVENT_KIND_ERROR:
throw SDKException.processingFailed(
event.hasError() && event.error.message.isNotEmpty
? event.error.message
: 'Image generation failed',
);
case DiffusionStreamEventKind.DIFFUSION_STREAM_EVENT_KIND_STARTED:
case DiffusionStreamEventKind.DIFFUSION_STREAM_EVENT_KIND_UNSPECIFIED:
break;
}
}
}