const functions = require("firebase-functions");
const axios = require("axios");
const admin = require("firebase-admin");
admin.initializeApp();
const storage = admin.storage();
// Specify the region for your functions
exports.generateImage = functions.region('asia-east1').https.onRequest(async (req, res) => {
try {
const { prompt } = req.body;
if (!prompt) {
return res.status(400).send({ error: "Prompt is required" });
}
// Hugging Face API Configuration
const apiUrl = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1";
const apiToken = "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"; // Replace with your Hugging Face API token
// Call Hugging Face API
const response = await axios.post(
apiUrl,
{ inputs: prompt },
{
headers: {
Authorization: `Bearer ${apiToken}`,
},
responseType: "arraybuffer", // To get binary image data
}
);
// Upload Image to Firebase Storage
const bucket = storage.bucket();
const fileName = `images/${Date.now()}.png`;
const file = bucket.file(fileName);
await file.save(response.data, {
metadata: {
contentType: "image/png",
},
});
const fileUrl = `https://storage.googleapis.com/${bucket.name}/${fileName}`;
// Return the Image URL
return res.status(200).send({ imageUrl: fileUrl });
} catch (error) {
console.error(error);
return res.status(500).send({ error: "Failed to generate image" });
}
});
My project ID is correct. My package.json is also correct. All dependencies are installed. I am not sure why it is not showing up on firebase… I tried deploying so many times. And it always deploys successfully. I tried re-initilaizing firebase. My region is also correct.