Skip to content

Instantly share code, notes, and snippets.

@chiliec
Created April 19, 2024 08:19
Show Gist options
  • Save chiliec/e2986507061ad23a61ccf545fb9147f2 to your computer and use it in GitHub Desktop.
Save chiliec/e2986507061ad23a61ccf545fb9147f2 to your computer and use it in GitHub Desktop.
Stable Diffusion 3.0 (SD3) image-to-image code example
import FormData from "form-data";
import fs from "node:fs";
import axios from "axios";
export async function generate(
originalImagePath: string,
resultImageName: string,
prompt: string,
negativePrompt: string,
strength: number,
): Promise<string> {
const apiHost = "https://api.stability.ai";
const apiKey = "sk-**********************";
if (!apiKey) throw new Error("Missing Stability API key.");
const image = fs.createReadStream(originalImagePath);
const formData = new FormData();
formData.append("mode", "image-to-image");
formData.append("model", "sd3"); // or sd3-turbo
formData.append("image", image);
formData.append("prompt", prompt);
formData.append("strength", String(strength));
formData.append("negative_prompt", negativePrompt); // NOT work with sd3-turbo
formData.append("seed", "0");
formData.append("output_format", "png");
const response = await axios.post(
`${apiHost}/v2beta/stable-image/generate/sd3`,
formData,
{
headers: {
Authorization: `Bearer ${apiKey}`,
...formData.getHeaders(),
},
responseType: "json",
},
);
const json = response.data;
if (response.status === 200 && "finish_reason" in json) {
if (json.finish_reason === "SUCCESS") {
const imageData = json.image;
if (!imageData) {
throw new Error("Image data not found in the response.");
}
const fp = `./data/${resultImageName}.png`;
fs.writeFileSync(fp, Buffer.from(imageData, "base64"));
return fp;
}
throw new Error(json.finish_reason);
}
throw new Error(`Unknown generation error: ${JSON.stringify(json)}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment