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:
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.
Top comments (3)
what inside of file google.json bro ?
i have problem with auth
\edit:
i just download json file on APIs & Service > credentials, but got error
getIdToken is not defined
, what is that?i try use
idToken.access_token
but still failed, help T_Tfor anyone else that stumbles across this, here's how to fix the missing
getIdToken is not defined
error:Hey,
Can you share your repo or the basic idea.
I am new to backend and want to make use of it but don't have much knowledge as what goes into credentials.json and all