Connect to the Stable Diffusion.cpp integration

このコンテンツはまだ日本語訳がありません。

⭐ Community Toolkit

This page explains the endpoint information that Aspire provides when an application references a Stable Diffusion.cpp resource. The Community Toolkit package hosts the server but does not include a client library. Use an HTTP client with the server URI and follow the stable-diffusion.cpp API documentation for supported routes and request formats.

For AppHost setup and configuration, see Stable Diffusion.cpp hosting integration.

The server resource exposes the following connection property:

PropertyDescription
UriThe HTTP URI for the server’s http endpoint.

The server connection string is:

Endpoint=http://localhost:<assigned-port>

The host port is assigned dynamically unless the AppHost configures a fixed port. If you reference a resource named stable-diffusion, Aspire exposes its Uri connection property to the consuming application as STABLE_DIFFUSION_URI.

The model child resource also has a connection string and properties:

Endpoint=http://localhost:<assigned-port>;Model=stabilityai/stable-diffusion-xl-base-1.0/sd_xl_base_1.0.safetensors;Revision=main
PropertyDescription
UriThe parent server’s HTTP URI.
ModelThe Hugging Face repository and file path, joined with /.
RevisionThe selected Hugging Face revision, defaulting to main.

The model resource name is generated from the server resource name and the model file name unless you provide an explicit name. The connection property prefix is based on the referenced resource name.

Reference the server resource from the consuming project:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const stableDiffusion = await builder.addStableDiffusionCpp('stable-diffusion');
await stableDiffusion.addHuggingFaceModel(
'stabilityai/stable-diffusion-xl-base-1.0',
'sd_xl_base_1.0.safetensors'
);
await builder
.addNodeApp('image-api', './image-api', 'index.js')
.withReference(stableDiffusion);
// After adding all resources, run the app...

Read STABLE_DIFFUSION_URI in the application and use it as the base address for HTTP requests:

Program.cs
var stableDiffusionUri = builder.Configuration["STABLE_DIFFUSION_URI"]
?? throw new InvalidOperationException("The Stable Diffusion.cpp URI is not configured.");
builder.Services.AddHttpClient("stable-diffusion", client =>
{
client.BaseAddress = new Uri(stableDiffusionUri);
});

The client name and configuration key follow the AppHost resource name. If the resource name changes, update the consuming app’s key to match.