vite-plugin-ssam-replicate
This is a plugin for Ssam.js creative coding helper. It uses Replicate Node.js API to generate Stable Diffusion image from your HTML5 Canvas drawing. With this plugin, you don't have to write boilerplate for the backend and focus on your Canvas drawing code.
Notes
- The plugin only works in the development environment.
- A Replicate online account and an API key are required. You get free inference for first tries, but you will eventually have to pay for the API usage.
Installation
Create a new sketch with npm create ssam@latest and choose the StableDiffusion template. You only need to provide your API key.
If you are not using Ssam, install with the following command:
npm i -D vite-plugin-ssam-replicate
Setup
- Create an API key on Replicate.com.
- Create a
.env.DEV.local file on the root of the Ssam sketch folder.
- Add the key as
REPLICATE_API_KEY=abcd1234 as environment variable. Do not share this key with anyone! You will be charged for images generated with the key.
- In
vite.config.ts, import and add the plugin.
import { defineConfig, loadEnv } from "vite";
import { ssamReplicate } from "vite-plugin-ssam-replicate";
const envVars = loadEnv("DEV", process.cwd(), "REPLICATE_");
export default defineConfig({
plugins: [
ssamExport(),
ssamGit(),
ssamFfmpeg(),
ssamReplicate({
apiKey: string;
testOutput?: string[];
saveOutput?: boolean;
log?: boolean;
outDir?: string;
}),
],
});
In your sketch code
For the full example using Ssam, see the examples/. You can adapt this code to use without Ssam package if you want.
if (!(ev.metaKey || ev.ctrlKey || ev.shiftKey) && ev.key === "g") {
const payload = {
dryRun: true,
version: "479633443fc6588e1e8ae764b79cdb3702d0c196e0cb2de6db39ce577383be77",
input: {
prompt: "An astronaut in spacesuit on Mars",
negative_prompt: "",
image: canvas.toDataURL(),
width,
height,
num_outputs: 1,
num_inference_steps: 6,
guidance_scale: 2,
prompt_strength: 0.8,
seed: (Math.random() * 100000000) | 0,
},
};
import.meta.hot && import.meta.hot.send("ssam:replicate-predict", payload);
}
if (import.meta.hot) {
import.meta.hot.on("ssam:replicate-prediction", async (prediction) => {
output = await Promise.all(
prediction.output.map(async (url: string) => await loadImage(url)),
);
render();
});
}
License
MIT