fix: use gemini-3.1-flash-image and generateContent API for image generation
Build and Deploy / build-and-push (push) Successful in 37s
Build and Deploy / build-and-push (push) Successful in 37s
This commit is contained in:
+25
-12
@@ -370,7 +370,7 @@ app.post('/api/sparkplug/extract', authenticate, upload.single('file'), async (r
|
||||
}
|
||||
});
|
||||
|
||||
// 2. Generate Images (Gemini Imagen 3)
|
||||
// 2. Generate Images (Gemini 3.1 Flash Image)
|
||||
app.post('/api/sparkplug/generate-images', authenticate, async (req, res) => {
|
||||
const { prompt } = req.body;
|
||||
if (!prompt) return res.status(400).json({ error: 'No prompt provided' });
|
||||
@@ -380,8 +380,8 @@ app.post('/api/sparkplug/generate-images', authenticate, async (req, res) => {
|
||||
const apiKey = keys.GEMINI_API_KEY || keys.NANO_BANANA_API_KEY;
|
||||
if (!apiKey) return res.status(400).json({ error: 'Gemini API key missing' });
|
||||
|
||||
// Use the standard Gemini Imagen 3 API
|
||||
const baseUrl = 'https://generativelanguage.googleapis.com/v1beta/models/imagen-3.0-generate-images:predict';
|
||||
// Use the Gemini 3.1 Flash Image API
|
||||
const baseUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-flash-image:generateContent';
|
||||
|
||||
const views = ['Front view', 'Back view', 'Left side view', 'Right side view'];
|
||||
|
||||
@@ -390,12 +390,15 @@ app.post('/api/sparkplug/generate-images', authenticate, async (req, res) => {
|
||||
const fullPrompt = `${prompt}. ${view}, isolated on a pure white background, studio lighting.`;
|
||||
|
||||
const payload = {
|
||||
instances: [
|
||||
{ prompt: fullPrompt }
|
||||
contents: [
|
||||
{
|
||||
parts: [
|
||||
{ text: fullPrompt }
|
||||
]
|
||||
}
|
||||
],
|
||||
parameters: {
|
||||
sampleCount: 1,
|
||||
aspectRatio: "1:1"
|
||||
generationConfig: {
|
||||
responseModalities: ["IMAGE"]
|
||||
}
|
||||
};
|
||||
|
||||
@@ -404,8 +407,18 @@ app.post('/api/sparkplug/generate-images', authenticate, async (req, res) => {
|
||||
});
|
||||
|
||||
let base64Image = null;
|
||||
if (response.data && response.data.predictions && response.data.predictions.length > 0) {
|
||||
base64Image = response.data.predictions[0].bytesBase64Encoded;
|
||||
let mimeType = 'image/jpeg';
|
||||
|
||||
// Extract from standard Gemini generateContent response
|
||||
const candidate = response.data?.candidates?.[0];
|
||||
if (candidate?.content?.parts) {
|
||||
for (const part of candidate.content.parts) {
|
||||
if (part.inlineData && part.inlineData.data) {
|
||||
base64Image = part.inlineData.data;
|
||||
mimeType = part.inlineData.mimeType || 'image/jpeg';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!base64Image) {
|
||||
@@ -413,8 +426,8 @@ app.post('/api/sparkplug/generate-images', authenticate, async (req, res) => {
|
||||
}
|
||||
|
||||
const buffer = Buffer.from(base64Image, 'base64');
|
||||
|
||||
const filename = `sparkplug-${req.user.id}-${Date.now()}-${index}.png`;
|
||||
const ext = mimeType === 'image/png' ? 'png' : 'jpg';
|
||||
const filename = `sparkplug-${req.user.id}-${Date.now()}-${index}.${ext}`;
|
||||
const filepath = path.join(uploadsDir, filename);
|
||||
fs.writeFileSync(filepath, buffer);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user