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) => {
|
app.post('/api/sparkplug/generate-images', authenticate, async (req, res) => {
|
||||||
const { prompt } = req.body;
|
const { prompt } = req.body;
|
||||||
if (!prompt) return res.status(400).json({ error: 'No prompt provided' });
|
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;
|
const apiKey = keys.GEMINI_API_KEY || keys.NANO_BANANA_API_KEY;
|
||||||
if (!apiKey) return res.status(400).json({ error: 'Gemini API key missing' });
|
if (!apiKey) return res.status(400).json({ error: 'Gemini API key missing' });
|
||||||
|
|
||||||
// Use the standard Gemini Imagen 3 API
|
// Use the Gemini 3.1 Flash Image API
|
||||||
const baseUrl = 'https://generativelanguage.googleapis.com/v1beta/models/imagen-3.0-generate-images:predict';
|
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'];
|
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 fullPrompt = `${prompt}. ${view}, isolated on a pure white background, studio lighting.`;
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
instances: [
|
contents: [
|
||||||
{ prompt: fullPrompt }
|
{
|
||||||
|
parts: [
|
||||||
|
{ text: fullPrompt }
|
||||||
|
]
|
||||||
|
}
|
||||||
],
|
],
|
||||||
parameters: {
|
generationConfig: {
|
||||||
sampleCount: 1,
|
responseModalities: ["IMAGE"]
|
||||||
aspectRatio: "1:1"
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -404,8 +407,18 @@ app.post('/api/sparkplug/generate-images', authenticate, async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let base64Image = null;
|
let base64Image = null;
|
||||||
if (response.data && response.data.predictions && response.data.predictions.length > 0) {
|
let mimeType = 'image/jpeg';
|
||||||
base64Image = response.data.predictions[0].bytesBase64Encoded;
|
|
||||||
|
// 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) {
|
if (!base64Image) {
|
||||||
@@ -413,8 +426,8 @@ app.post('/api/sparkplug/generate-images', authenticate, async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const buffer = Buffer.from(base64Image, 'base64');
|
const buffer = Buffer.from(base64Image, 'base64');
|
||||||
|
const ext = mimeType === 'image/png' ? 'png' : 'jpg';
|
||||||
const filename = `sparkplug-${req.user.id}-${Date.now()}-${index}.png`;
|
const filename = `sparkplug-${req.user.id}-${Date.now()}-${index}.${ext}`;
|
||||||
const filepath = path.join(uploadsDir, filename);
|
const filepath = path.join(uploadsDir, filename);
|
||||||
fs.writeFileSync(filepath, buffer);
|
fs.writeFileSync(filepath, buffer);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user