Open ABDERRAHMANE-OUALI opened 1 year ago
the full code : ` import as functions from "firebase-functions"; import as GenerationService from "./generation/generation_pb_service"; import as Generation from "./generation/generation_pb"; import as grpc from "grpc";
export const generateImage = functions.https.onRequest((request, response) => { const {text} = JSON.parse(request.body)
// Set up image parameters const imageParams = new Generation.ImageParameters(); imageParams.setWidth(512); imageParams.setHeight(512); imageParams.addSeed(1234); imageParams.setSamples(1); imageParams.setSteps(50);
// Use the k-dpmpp-2
sampler
const transformType = new Generation.TransformType();
transformType.setDiffusion(Generation.DiffusionSampler.SAMPLER_K_DPMPP_2M);
imageParams.setTransform(transformType);
// Use Stable Diffusion 2.0 const generationRequest = new Generation.Request(); generationRequest.setEngineId("stable-diffusion-512-v2-1"); generationRequest.setRequestedType(Generation.ArtifactType.ARTIFACT_IMAGE); generationRequest.setClassifier(new Generation.ClassifierParameters());
// Use a CFG scale of 13
const samplerParams = new Generation.SamplerParameters();
samplerParams.setCfgScale(13);
const stepParams = new Generation.StepParameter(); const scheduleParameters = new Generation.ScheduleParameters();
// Set the schedule to 0
, this changes when doing an initial image generation
stepParams.setScaledStep(0);
stepParams.setSampler(samplerParams);
stepParams.setSchedule(scheduleParameters);
imageParams.addParameters(stepParams); generationRequest.setImage(imageParams);
// Set our text prompt const promptText = new Generation.Prompt(); promptText.setText( text, // "A dream of a distant galaxy, by Caspar David Friedrich, matte painting trending on artstation HQ" );
generationRequest.addPrompt(promptText);
// Authenticate using your API key, don't commit your key to a public repository!
const metadata = new grpc.Metadata(); metadata.set("Authorization", "Bearer " + process.env.API_KEY);
// Create a generation client const generationClient = new GenerationService.GenerationServiceClient( 'https://grpc.stability.ai/', {} );
// Send the request using the metadata
with our key from earlier
const generation = generationClient.generate(generationRequest, metadata);
// Set up a callback to handle data being returned generation.on("data", (data: any) => { data.getArtifactsList().forEach((artifact: any) => { // Oh no! We were filtered by the NSFW classifier! if ( artifact.getType() === Generation.ArtifactType.ARTIFACT_TEXT && artifact.getFinishReason() === Generation.FinishReason.FILTER ) { return console.error("Your image was filtered by the NSFW classifier."); }
// Make sure we have an image
if (artifact.getType() !== Generation.ArtifactType.ARTIFACT_IMAGE) return;
// You can convert the raw binary into a base64 string
const base64Image = btoa(
new Uint8Array(artifact.getBinary()).reduce(
(data, byte) => data + String.fromCodePoint(byte),
""
)
);
// Here's how you get the seed back if you set it to `0` (random)
// const seed = artifact.getSeed();
// We're done!
someFunctionToCallWhenFinished(base64Image);
}); });
// Anything other than status.code === 0
is an error
generation.on("status", (status: any) => {
if (status.code === 0) return;
console.error(
"Your image could not be generated. You might not have enough credits."
);
});
functions.logger.info("Hello logs!", {structuredData: true});
response.send("Hello from Firebase!");
});
function someFunctionToCallWhenFinished( base64Image: string) {
const image = document.createElement("img");
image.src = data:image/png;base64,${base64Image}
;
document.body.appendChild(image);
}
`
You need to change the grpc you import in the header to
import { grpc } from "@improbable-eng/grpc-web";
I'm using the typescript client guide and then this error appear