Awesome
StabilityClient.Net
gRPC client for StabilitySDK written with .Net 6.
Features
- Connect to StabilitySDK using:
- environment variable STABILITY_KEY (default)
- host (optional)
- apiKey (optional)
RequestBuilder
: fluent builder to easily build requestsSaveImagesToAsync
: extension method to AsyncServerStreamingCall to easily save images
Usage
1. Add Nuget package from here.
2. Get your Stability API Key.
See here for instruction about how to get Stability API Key.
3. Create your client.
Recommended:
// This will use environment variable STABILITY_KEY as your API key input and https://grpc.stability.ai:443 as your host input.
var stability = new StabilityClient();
Optional:
var stability = new StabilityClient("myStabilityApiKey", "myHost");
4. Create request.
Using RequestBuilder
:
var request = new RequestBuilder()
.SetTextPrompt("Chihuahua in sombrero")
.SetImageHeight(512) // optional
.SetImageWidth(512) // optional
.SetImageSteps(30) // optional
.SetEngineId("stable-diffusion-512-v2-1") // optional
.Build();
Raw:
var request = new Request {
Prompt = {
new Prompt {
Text = "Chihuahua in sombrero",
Parameters = new PromptParameters {
Init = true
},
},
},
EngineId = "stable-diffusion-512-v2-1",
Image = new ImageParameters {
Height = 512,
Width = 512,
Steps = 30
}
};
5. Send request and save image from response to file.
Using SaveImagesToAsync
extension method:
// Send request
var response = stabilityClient.Generation.Generate(request);
// Save to file
await response.SaveImagesToAsync("./Images");
Raw:
// Send request
var response = stabilityClient.Generation.Generate(request).ResponseStream;
// Save to file
var source = new CancellationTokenSource();
while (await response.MoveNext(source.Token)) {
var answer = response.Current;
foreach (var artifact in answer.Artifacts) {
if (artifact.Type == ArtifactType.ArtifactImage) {
var content = artifact.Binary.ToByteArray();
await File.WriteAllBytesAsync("C:/Projects/Images/Image.png", content, source.Token);
}
}
}
6. Enjoy your image :)
Development
I am happy to accept suggestions for further development. Please feel free to add Issues :)
Authors
License
This project is licensed under the MIT License - see the LICENSE file for details.