Dall-e API on Firebase

Francesco Rollo
2 min readNov 12, 2022

--

Just getting the story started here on generating Dall-e images from Firebase. After plugging the API into a node.js function, I realized that the images expire after an hour, so if I want to let our players view their image generation history, I’m going to need to set up an image processing pipeline using a a Firebase function or two.

Cats with brains on outside of their heads

So, here’s the plan:

  1. Node express API (running as Firebase function) which generates a Dalle image on request and returns the Url.
  2. Store the Image Url in firebase for that player

Few days later: Massive detour on the way to the above. Turns out the Dalle images expire after an hour, so needed a server side method to get the image from Dalle and bring it over to Firebase Storage.

Here’s the function that generates the image from Dalle — and then the function that takes the Dalle URL (or any URL), stores it in Firebase Storage (Gcp) and then generates a public URL for it.

import { v4 as uuidv4 } from 'uuid';
import path from 'path';
import got from 'got';
import { Storage } from '@google-cloud/storage';

const storage = new Storage({
projectId: 'FIREBASEPROJECTID',
keyFilename: 'PATHTO_SERVICE_ACCTJSON'
});
const storageBucket = config.production.firebaseConfig.storageBucket;

//--------OPENAI-------------
import { Configuration, OpenAIApi } from "openai";
const openai_configuration = new Configuration({
apiKey: 'APIKEY',
});
const openai = new OpenAIApi(openai_configuration);

//----------------------THUMBS----------------------
/**
* GET THUMBS
* Gets thumbnails from Dalle, given a phrase
*
* @param {string} phrase user typed phrase
* @return {array} N thumbnail url that matching phrase
*/
dalleRouter.get("/thumbs", async (req, res, next) => {
console.log('->dalle thumbs, phrase is:', req.query.phrase)
const ret = await openai.createImage({
prompt: req.query.phrase,
n: 1,
size: "256x256",
});
let image_urls = ret.data.data;
console.log(image_urls)
res.status(200).json({'result':'success','data':image_urls})
});

//----------------------TRANSMOGRIFY----------------------
/**
* TRANSMOGRIFY
* Takes a Dalle URL and saves it to firebase storage, then returns the signed URL
* https://stackoverflow.com/questions/37686213/upload-image-from-url-to-firebase-storage
* @param {string} url url of dalle image
* @return {string} signed url of same image saved to server
*/
dalleRouter.get("/transmogrify", async (req, res) => {
let source_url = req.query.url;
console.log('->dalle transmogrify, url is:', source_url);

//create a new filename for this image
let uuid = uuidv4();
let fileName = `images/${uuid}.png`;

//get a reference to where we want to put this file in the bucket
const file = storage
.bucket(storageBucket)
.file(fileName);

//get the byte stream from teh source url and pipe it to the new location in bucket
got.stream(source_url)
.pipe(file.createWriteStream({
metadata: {
contentType: 'image/png'
}
}))
.on('error', (err) => {
console.error(err);
res.status(500).json({'result':'error','msg':err})
})
.on('finish', () => {
console.log(`Finished copying ${source_url} to ${fileName} `);
//now get a signed URL with expiry date far in the future
file.getSignedUrl({
action: 'read',
expires: '03-09-2491'
}).then(signedUrls => {
//console.log('URL',signedUrls[0]);
//return the signed URL to player
res.status(200).json({'result':'success','signed_url':signedUrls[0]})
});
});

--

--