fix: script
This commit is contained in:
parent
044202979b
commit
2fc7bd7698
|
|
@ -33,20 +33,7 @@ async function main() {
|
||||||
apiKey: process.env.GEMINI_API_KEY,
|
apiKey: process.env.GEMINI_API_KEY,
|
||||||
});
|
});
|
||||||
|
|
||||||
// First, let's list available models
|
// Note: Using gemini-2.0-flash-preview-image-generation with both IMAGE and TEXT response modalities
|
||||||
console.log('Listing available models...');
|
|
||||||
try {
|
|
||||||
const models = await ai.models.list();
|
|
||||||
console.log('Available models:');
|
|
||||||
for await (const model of models) {
|
|
||||||
console.log(`- ${model.name}`);
|
|
||||||
console.log(` Display name: ${model.displayName || 'N/A'}`);
|
|
||||||
console.log(` Description: ${model.description || 'N/A'}`);
|
|
||||||
console.log('---');
|
|
||||||
}
|
|
||||||
} catch (modelListError) {
|
|
||||||
console.error('Error listing models:', modelListError);
|
|
||||||
}
|
|
||||||
|
|
||||||
const config = {
|
const config = {
|
||||||
responseModalities: ['IMAGE', 'TEXT'],
|
responseModalities: ['IMAGE', 'TEXT'],
|
||||||
|
|
@ -63,43 +50,38 @@ async function main() {
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
console.log('Making API request...');
|
console.log('Making API request for image generation...');
|
||||||
try {
|
try {
|
||||||
// Try non-streaming first
|
|
||||||
const response = await ai.models.generateContent({
|
const response = await ai.models.generateContent({
|
||||||
model,
|
model,
|
||||||
config,
|
config,
|
||||||
contents,
|
contents,
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('Response received:', response);
|
console.log('Response received successfully');
|
||||||
|
|
||||||
if (response.candidates && response.candidates[0] && response.candidates[0].content) {
|
if (response.candidates && response.candidates[0] && response.candidates[0].content) {
|
||||||
const content = response.candidates[0].content;
|
const content = response.candidates[0].content;
|
||||||
console.log('Content parts:', content.parts?.length);
|
|
||||||
|
|
||||||
content.parts?.forEach((part, index) => {
|
content.parts?.forEach((part, index) => {
|
||||||
if (part.inlineData) {
|
if (part.inlineData) {
|
||||||
const fileName = `fantasy_warrior_${index}`;
|
const fileName = `fantasy_warrior_${index}`;
|
||||||
const inlineData = part.inlineData;
|
const inlineData = part.inlineData;
|
||||||
const fileExtension = mime.getExtension(inlineData.mimeType || '');
|
const fileExtension = mime.getExtension(inlineData.mimeType || '');
|
||||||
console.log(`Found image data with mime type: ${inlineData.mimeType}`);
|
console.log(`Saving image: ${fileName}.${fileExtension}`);
|
||||||
const buffer = Buffer.from(inlineData.data || '', 'base64');
|
const buffer = Buffer.from(inlineData.data || '', 'base64');
|
||||||
saveBinaryFile(`${fileName}.${fileExtension}`, buffer);
|
saveBinaryFile(`${fileName}.${fileExtension}`, buffer);
|
||||||
} else if (part.text) {
|
} else if (part.text) {
|
||||||
console.log('Text part:', part.text);
|
console.log('Description:', part.text);
|
||||||
} else {
|
|
||||||
console.log('Unknown part type:', part);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
console.log('Processing complete!');
|
console.log('Image generation complete!');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error during API call:', error);
|
console.error('Primary model failed:', error.message || error);
|
||||||
console.error('Error details:', JSON.stringify(error, null, 2));
|
|
||||||
|
|
||||||
// If this model fails, let's try a different one
|
// Try fallback model
|
||||||
console.log('Trying alternative model...');
|
console.log('Trying fallback model (Nano Banana)...');
|
||||||
try {
|
try {
|
||||||
const altResponse = await ai.models.generateContent({
|
const altResponse = await ai.models.generateContent({
|
||||||
model: 'gemini-2.5-flash-image-preview',
|
model: 'gemini-2.5-flash-image-preview',
|
||||||
|
|
@ -108,25 +90,28 @@ async function main() {
|
||||||
},
|
},
|
||||||
contents,
|
contents,
|
||||||
});
|
});
|
||||||
console.log('Alternative model response:', altResponse);
|
|
||||||
|
console.log('Fallback model succeeded!');
|
||||||
|
|
||||||
if (altResponse.candidates && altResponse.candidates[0] && altResponse.candidates[0].content) {
|
if (altResponse.candidates && altResponse.candidates[0] && altResponse.candidates[0].content) {
|
||||||
const content = altResponse.candidates[0].content;
|
const content = altResponse.candidates[0].content;
|
||||||
console.log('Alternative model content parts:', content.parts?.length);
|
|
||||||
|
|
||||||
content.parts?.forEach((part, index) => {
|
content.parts?.forEach((part, index) => {
|
||||||
if (part.inlineData) {
|
if (part.inlineData) {
|
||||||
const fileName = `fantasy_warrior_alt_${index}`;
|
const fileName = `fantasy_warrior_fallback_${index}`;
|
||||||
const inlineData = part.inlineData;
|
const inlineData = part.inlineData;
|
||||||
const fileExtension = mime.getExtension(inlineData.mimeType || '');
|
const fileExtension = mime.getExtension(inlineData.mimeType || '');
|
||||||
console.log(`Found image data with mime type: ${inlineData.mimeType}`);
|
console.log(`Saving fallback image: ${fileName}.${fileExtension}`);
|
||||||
const buffer = Buffer.from(inlineData.data || '', 'base64');
|
const buffer = Buffer.from(inlineData.data || '', 'base64');
|
||||||
saveBinaryFile(`${fileName}.${fileExtension}`, buffer);
|
saveBinaryFile(`${fileName}.${fileExtension}`, buffer);
|
||||||
|
} else if (part.text) {
|
||||||
|
console.log('Fallback description:', part.text);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (altError) {
|
} catch (altError) {
|
||||||
console.error('Alternative approach also failed:', altError);
|
console.error('Both models failed. Please try again later.');
|
||||||
|
console.error('Fallback error:', altError.message || altError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue