Scaled down and converted images before sending them to AI models (#1101)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-10-22 10:57:04 +02:00
1 parent 1c8632bbe0
commit ba99bf6fae
33 files changed
+1550 -264

No files matched your search

@@ -26,4 +26,34 @@ public sealed partial class RustService
return await response.Content.ReadFromJsonAsync<ImagePrepareResponse>(this.jsonRustSerializerOptions, token)
?? throw new InvalidDataException("The Rust image preparation returned an empty response.");
}
/// <summary>
/// Prepares a local image for an AI model in the Rust runtime.
/// </summary>
/// <remarks>
/// The runtime turns the image upright, scales it down, and encodes it anew as JPEG or PNG,
/// within the given number of bytes. An image it cannot prepare, e.g., in an unsupported format,
/// comes back with an error code. So does a failed call to the runtime, as UNKNOWN, and never as
/// an empty image.
/// </remarks>
/// <param name="path">The absolute path of the image file.</param>
/// <param name="maxBytes">The largest number of bytes the encoded image may have, before base64.</param>
/// <param name="token">The cancellation token.</param>
/// <returns>The prepared image, or why it could not be prepared.</returns>
public async Task<PreparedModelImage> PrepareImageForModelAsync(string path, long maxBytes, CancellationToken token = default)
{
try
{
using var response = await this.http.PostAsJsonAsync("/image/prepare-for-model", new { path, maxBytes }, this.jsonRustSerializerOptions, token);
response.EnsureSuccessStatusCode();
return await response.Content.ReadFromJsonAsync<PreparedModelImage>(this.jsonRustSerializerOptions, token)
?? PreparedModelImage.Failed(ModelImageErrorCode.UNKNOWN);
}
catch (Exception e) when (e is not OperationCanceledException)
{
this.logger?.LogError(e, "Preparing the image '{ImagePath}' for an AI model failed in the runtime.", path);
return PreparedModelImage.Failed(ModelImageErrorCode.UNKNOWN);
}
}
}