gRPC client for StabilitySDK written with .Net 6.
- 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
1. Add Nuget package from here.
See here for instruction about how to get Stability API Key.
Recommended:
// This will use environment variable STABILITY_KEY as your API key input and https://grpc.stability.ai:443 as your host input.varstability=newStabilityClient();Optional:
varstability=newStabilityClient("myStabilityApiKey","myHost");Using RequestBuilder:
varrequest=newRequestBuilder().SetTextPrompt("Chihuahua in sombrero").SetImageHeight(512)// optional.SetImageWidth(512)// optional.SetImageSteps(30)// optional.SetEngineId("stable-diffusion-512-v2-1")// optional.Build();Raw:
varrequest=newRequest{Prompt={newPrompt{Text="Chihuahua in sombrero",Parameters=newPromptParameters{Init=true},},},EngineId="stable-diffusion-512-v2-1",Image=newImageParameters{Height=512,Width=512,Steps=30}};Using SaveImagesToAsync extension method:
// Send requestvarresponse=stabilityClient.Generation.Generate(request);// Save to fileawaitresponse.SaveImagesToAsync("./Images");Raw:
// Send requestvarresponse=stabilityClient.Generation.Generate(request).ResponseStream;// Save to filevarsource=newCancellationTokenSource();while(awaitresponse.MoveNext(source.Token)){varanswer=response.Current;foreach(varartifactinanswer.Artifacts){if(artifact.Type==ArtifactType.ArtifactImage){varcontent=artifact.Binary.ToByteArray();awaitFile.WriteAllBytesAsync("C:/Projects/Images/Image.png",content,source.Token);}}}I am happy to accept suggestions for further development. Please feel free to add Issues :)
This project is licensed under the MIT License - see the LICENSE file for details.
