generated from fahricansecer/boilerplate-be
@@ -264,39 +264,58 @@ IMPORTANT: Only output valid JSON, no markdown code blocks or other text.`;
|
||||
try {
|
||||
this.logger.debug(`🎨 Görsel üretiliyor: "${prompt.substring(0, 80)}..." [${aspectRatio}]`);
|
||||
|
||||
const enhancedPrompt = `Generate a high-quality image for this description: ${prompt}. Style: photorealistic, cinematic lighting, detailed. Aspect ratio: ${aspectRatio}.`;
|
||||
|
||||
// 1) First try the stable Imagen-4 Fast API
|
||||
try {
|
||||
const response = await this.client!.models.generateImages({
|
||||
model: 'imagen-4.0-fast-generate-001',
|
||||
prompt: enhancedPrompt,
|
||||
config: {
|
||||
numberOfImages: 1,
|
||||
aspectRatio: aspectRatio,
|
||||
outputMimeType: 'image/jpeg',
|
||||
personGeneration: 'ALLOW_ALL' as any
|
||||
}
|
||||
});
|
||||
|
||||
if (response.generatedImages?.[0]?.image?.imageBytes) {
|
||||
const buffer = Buffer.from(response.generatedImages[0].image.imageBytes, 'base64');
|
||||
const mimeType = 'image/jpeg';
|
||||
this.logger.log(`✅ Görsel üretildi (Imagen): ${(buffer.length / 1024).toFixed(1)} KB [${mimeType}]`);
|
||||
return { buffer, mimeType };
|
||||
}
|
||||
} catch (imagenError: any) {
|
||||
this.logger.warn(`Imagen API error, falling back to generateContent... ${imagenError.message}`);
|
||||
}
|
||||
|
||||
// 2) Fallback to Gemini Flash image modalities (experimental feature)
|
||||
const response = await this.client!.models.generateContent({
|
||||
model: imageModel,
|
||||
contents: [
|
||||
{
|
||||
role: 'user',
|
||||
parts: [
|
||||
{
|
||||
text: `Generate a high-quality image for this description: ${prompt}. Aspect ratio: ${aspectRatio}. Style: photorealistic, cinematic lighting, detailed.`,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
contents: enhancedPrompt,
|
||||
config: {
|
||||
responseModalities: ['TEXT', 'IMAGE'] as any,
|
||||
responseModalities: ['IMAGE', 'TEXT'],
|
||||
},
|
||||
});
|
||||
|
||||
// Gemini image response'dan image part'ı çıkar
|
||||
const parts = (response as any).candidates?.[0]?.content?.parts || [];
|
||||
for (const part of parts) {
|
||||
if (part.inlineData?.data) {
|
||||
const buffer = Buffer.from(part.inlineData.data, 'base64');
|
||||
const mimeType = part.inlineData.mimeType || 'image/png';
|
||||
// Gemini image generation modeli, inlineData olarak görsel döner
|
||||
const candidate = response.candidates?.[0];
|
||||
const imagePart = candidate?.content?.parts?.find(
|
||||
(p: any) => p.inlineData?.mimeType?.startsWith('image/'),
|
||||
);
|
||||
|
||||
this.logger.log(`✅ Görsel üretildi: ${(buffer.length / 1024).toFixed(1)} KB`);
|
||||
return { buffer, mimeType };
|
||||
}
|
||||
if (imagePart?.inlineData?.data) {
|
||||
const buffer = Buffer.from(imagePart.inlineData.data, 'base64');
|
||||
const mimeType = imagePart.inlineData.mimeType || 'image/png';
|
||||
|
||||
this.logger.log(`✅ Görsel üretildi (Flash): ${(buffer.length / 1024).toFixed(1)} KB [${mimeType}]`);
|
||||
return { buffer, mimeType };
|
||||
}
|
||||
|
||||
this.logger.warn('Gemini görsel üretemedi — boş yanıt');
|
||||
this.logger.warn('Gemini görsel üretemedi — response içinde image part bulunamadı');
|
||||
return null;
|
||||
} catch (error) {
|
||||
this.logger.error(`Gemini görsel üretim hatası: ${error}`);
|
||||
this.logger.error(`Gemini görsel üretim hatası: ${error instanceof Error ? error.message : error}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user