Skip to content

Instantly share code, notes, and snippets.

@fzn0x
Created October 9, 2024 04:26
Show Gist options
  • Save fzn0x/a2a17727e0368c9879c9147e837f81eb to your computer and use it in GitHub Desktop.
Save fzn0x/a2a17727e0368c9879c9147e837f81eb to your computer and use it in GitHub Desktop.
Example code for Generating images with hugging face stabilityai/stable-diffusion-2
import { HfInference } from "@huggingface/inference";
import fs from "fs";
const inference = new HfInference("HF_TOKEN");
async function saveBlobToFile(blob, filePath) {
// Convert the blob to a buffer
const arrayBuffer = await blob.arrayBuffer();
// Write the buffer to a file
fs.writeFile(filePath, Buffer.from(arrayBuffer), (err) => {
if (err) {
console.error("Error writing the file:", err);
} else {
console.log("File saved successfully at", filePath);
}
});
}
(async () => {
console.log("Generating..");
const res = await inference.textToImage({
model: "stabilityai/stable-diffusion-2",
inputs:
"award winning high resolution photo of a giant tortoise/((ladybird)) hybrid, [trending on artstation]",
parameters: {
negative_prompt: "blurry",
},
});
await saveBlobToFile(res, "output.png");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment