Initial implementation of Stable Diffusion
- Needs to add attachment to discord - Needs to delete image afterward - Needs to calculate token usage
This commit is contained in:
60
controllers/stabilityController.js
Normal file
60
controllers/stabilityController.js
Normal file
@@ -0,0 +1,60 @@
|
||||
// Debug
|
||||
const { DebugBuilder } = require("../utilities/debugBuilder");
|
||||
const log = new DebugBuilder("server", "stabilityController");
|
||||
|
||||
// Modules
|
||||
const Generation = require("./stabilityAi/generation_pb");
|
||||
const { GenerationServiceClient } = require("./stabilityAi/generation_pb_service");
|
||||
|
||||
const { grpc } = require("@improbable-eng/grpc-web");
|
||||
const GRPCWeb = grpc;
|
||||
|
||||
const { NodeHttpTransport } = require("@improbable-eng/grpc-web-node-http-transport");
|
||||
const fs = require("fs");
|
||||
const {
|
||||
buildGenerationRequest,
|
||||
executeGenerationRequest,
|
||||
onGenerationComplete,
|
||||
} = require("../utilities/stabilityHelpers");
|
||||
|
||||
// Set Global Color for this controller
|
||||
exports.STABILITY_COLOR = 0xeb34b7;
|
||||
|
||||
// This is a NodeJS-specific requirement - browsers implementations should omit this line.
|
||||
GRPCWeb.setDefaultTransport(NodeHttpTransport());
|
||||
|
||||
// Authenticate using your API key, don't commit your key to a public repository!
|
||||
const metadata = new GRPCWeb.Metadata();
|
||||
metadata.set("Authorization", "Bearer " + process.env.STABILITY_API_KEY);
|
||||
|
||||
// Create a generation client to use with all future requests
|
||||
const stabilityClient = new GenerationServiceClient("https://grpc.stability.ai", {});
|
||||
|
||||
exports.submitImageGenerationTransaction = async () => {
|
||||
const request = buildGenerationRequest("stable-diffusion-512-v2-1", {
|
||||
type: "text-to-image",
|
||||
prompts: [
|
||||
{
|
||||
text: "A dream of a distant galaxy, by Caspar David Friedrich, matte painting trending on artstation HQ",
|
||||
},
|
||||
],
|
||||
width: 512,
|
||||
height: 512,
|
||||
samples: 1,
|
||||
cfgScale: 13,
|
||||
steps: 10,
|
||||
sampler: Generation.DiffusionSampler.SAMPLER_K_DPMPP_2M,
|
||||
});
|
||||
log.DEBUG("Stability request: ", request, metadata, stabilityClient);
|
||||
|
||||
executeGenerationRequest(stabilityClient, request, metadata)
|
||||
.then((response) => {
|
||||
log.DEBUG("Stability Generation response: ". response)
|
||||
onGenerationComplete(response);
|
||||
return;
|
||||
})
|
||||
.catch((error) => {
|
||||
log.ERROR("Failed to make text-to-image request:", error);
|
||||
}
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user