How to integrate Imagen2 AI in NodeJS

how-to-integrate-imagen2-ai-in-nodejs

Imagen is Google’s new state-of-the-art image generation AI model. In this post, we’ll look at how to integrate Imagen into a Node.js application to generate images from text prompts.

What’s great about Imagen2 is that anyone can access it without needing to apply for trusted tester access. This means it’s effectively generally available to the public already.

So you don’t need to go through an application process or be approved to generate images with Imagen2. As long as you have a Google Cloud account and authentication set up, you can start building imaginative applications powered by this new state-of-the-art AI today.

We’ll be using the Google Cloud AI Platform to access Imagen. So first we need to set up authentication and get an ID token:

import { JWT } from "google-auth-library";
import dotenv from "dotenv";

dotenv.config({override: true});

const client = new JWT({
  keyFile: "./google.json",
  scopes: [
    "https://www.googleapis.com/auth/cloud-platform",
  ],  
});

const idToken = await client.authorize();

This uses a service account key file to get OAuth credentials and request an ID token we can use to call the Imagen API.

Next we set up the API endpoint and headers:

const API_ENDPOINT = "us-central1-aiplatform.googleapis.com";
const IMAGEN_URL = `https://${API_ENDPOINT}/v1/projects/${process.env.GOOGLE_KEY}/locations/us-central1/publishers/google/models/imagegeneration:predict`;


const headers = {
  Authorization: `Bearer ${await getIdToken()}`,
  "Content-Type": "application/json",
};

To generate an image, we need to send a JSON payload with the prompt text:

const data = {
  instances: [
    { 
      prompt: "a cute baby sea otter",
    },
  ],
  parameters: {
    sampleCount: 1,
  },
};

We can then call the API and get the image data in Base64 encoding:

const response = await fetch(IMAGEN_URL, {
  method: "POST",
  headers,
  body: JSON.stringify(data),  
});

const result = await response.json();

return Buffer.from(result?.predictions?[0]?.bytesBase64Encoded, "base64");

And that’s it! With just a few lines of code we can now generate images from Imagen right inside our Node.js applications.

Some ideas for using this:

  • Generative art
  • Automatically creating images for blog posts
  • Making profile pictures
  • And more!

Examples:

Image description

Image description

Image description

Full story generated by my service (Manga TV) here: https://mangatv.shop/story/dune-battle-fremens-vs-sarduckars

Let me know in the comments if you end up building something cool with the Imagen2 API.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
how-often-should-you-(or-your-company)-blog?-[new-data]

How Often Should You (or Your Company) Blog? [New Data]

Next Post
pushing-cloudflare-worker-logs-to-grafana-loki

Pushing Cloudflare Worker logs to Grafana Loki

Related Posts