I am trying to create a backend for my application using firebase functions. I want to make a call to an API from my backend. When I make the call from Postman to the API, the API works fine but the call takes 10-20 seconds. I am looking for a way to make the call from my firebase functions backend.
exports.generate = functions.https.onRequest( (req, res) => {
corsHandler(req, res, () => {
res.set("Access-Control-Allow-Origin", "*");
const prompt = req.query.text;
const apiKeySD =
"key";
const endpointSD = "https://stablediffusionapi.com/api/v3/text2img";
const data = {
"key": apiKeySD,
"prompt": prompt,
"negative_prompt": "",
"width": "256",
"height": "256",
"samples": "1",
"num_inference_steps": "20",
"seed": null,
"guidance_scale": 7.5,
"webhook": null,
"track_id": null,
};
const generationOptions = {
method: "POST",
timeout: 20000,
headers: {
"Content-Length": Buffer.byteLength(JSON.stringify(data)),
"Connection": "keep-alive",
"content-type": "application/json",
},
body: data,
};
https.request(endpointSD, generationOptions, (sdRes:any) => {
let dataSD = "";
sdRes.on("data", (chunk:any) => {
dataSD += chunk;
});
sdRes.on("end", () => {
const response = JSON.parse(dataSD);
const images = response.output;
res.send(images);
});
}).on("error", (err:any) => {
res.send("Error occured during generation" + err);
});
});
});
The log from firebase:
ERROR 2022-12-25T14:31:46.524426Z [resource.labels.functionName: generate] [labels.executionId: apbs87dr3l5f] Error: socket hang up at connResetException (node:internal/errors:705:14) at TLSSocket.socketOnEnd (node:_http_client:518:23) at TLSSocket.emit (node:events:525:35) at TLSSocket.emit (node:domain:552:15) at endReadableNT (node:internal/streams/readable:1358:12) at processTicksAndRejections (node:internal/process/task_queues:83:21)
{
"textPayload": "Error: socket hang up\n at connResetException (node:internal/errors:705:14)\n at TLSSocket.socketOnEnd (node:_http_client:518:23)\n at TLSSocket.emit (node:events:525:35)\n at TLSSocket.emit (node:domain:552:15)\n at endReadableNT (node:internal/streams/readable:1358:12)\n at processTicksAndRejections (node:internal/process/task_queues:83:21)",
"insertId": "63a85ed20008008affab5af6",
"resource": {
"type": "cloud_function",
"labels": {
"project_id": "hufusion",
"region": "us-central1",
"function_name": "generate"
}
},
"timestamp": "2022-12-25T14:31:46.524426Z",
"severity": "ERROR",
"labels": {
"instance_id": "00c61b117cda64fea6286b0f26e0be07b4eadab5bb72817b9a3ee93932a7180c204fcbd8cf5aae7bd6140ec301a231eac74aa0311c81559c4b74eb",
"execution_id": "apbs87dr3l5f"
},
"logName": "projects/hufusion/logs/cloudfunctions.googleapis.com%2Fcloud-functions",
"trace": "projects/hufusion/traces/e972f60ed325b739363d5972d1bd64c1",
"receiveTimestamp": "2022-12-25T14:31:46.795046411Z"
}
I tested the SD API using Postman, it works as expected, I also tested it using python. I checked the firebase logs but I did not find anything that helped.