fix: script
This commit is contained in:
parent
044202979b
commit
2fc7bd7698
|
|
@ -33,20 +33,7 @@ async function main() {
|
|||
apiKey: process.env.GEMINI_API_KEY,
|
||||
});
|
||||
|
||||
// First, let's list available models
|
||||
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);
|
||||
}
|
||||
// Note: Using gemini-2.0-flash-preview-image-generation with both IMAGE and TEXT response modalities
|
||||
|
||||
const config = {
|
||||
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 non-streaming first
|
||||
const response = await ai.models.generateContent({
|
||||
model,
|
||||
config,
|
||||
contents,
|
||||
});
|
||||
|
||||
console.log('Response received:', response);
|
||||
console.log('Response received successfully');
|
||||
|
||||
if (response.candidates && response.candidates[0] && response.candidates[0].content) {
|
||||
const content = response.candidates[0].content;
|
||||
console.log('Content parts:', content.parts?.length);
|
||||
|
||||
content.parts?.forEach((part, index) => {
|
||||
if (part.inlineData) {
|
||||
const fileName = `fantasy_warrior_${index}`;
|
||||
const inlineData = part.inlineData;
|
||||
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');
|
||||
saveBinaryFile(`${fileName}.${fileExtension}`, buffer);
|
||||
} else if (part.text) {
|
||||
console.log('Text part:', part.text);
|
||||
} else {
|
||||
console.log('Unknown part type:', part);
|
||||
console.log('Description:', part.text);
|
||||
}
|
||||
});
|
||||
}
|
||||
console.log('Processing complete!');
|
||||
console.log('Image generation complete!');
|
||||
} catch (error) {
|
||||
console.error('Error during API call:', error);
|
||||
console.error('Error details:', JSON.stringify(error, null, 2));
|
||||
console.error('Primary model failed:', error.message || error);
|
||||
|
||||
// If this model fails, let's try a different one
|
||||
console.log('Trying alternative model...');
|
||||
// Try fallback model
|
||||
console.log('Trying fallback model (Nano Banana)...');
|
||||
try {
|
||||
const altResponse = await ai.models.generateContent({
|
||||
model: 'gemini-2.5-flash-image-preview',
|
||||
|
|
@ -108,25 +90,28 @@ async function main() {
|
|||
},
|
||||
contents,
|
||||
});
|
||||
console.log('Alternative model response:', altResponse);
|
||||
|
||||
console.log('Fallback model succeeded!');
|
||||
|
||||
if (altResponse.candidates && altResponse.candidates[0] && altResponse.candidates[0].content) {
|
||||
const content = altResponse.candidates[0].content;
|
||||
console.log('Alternative model content parts:', content.parts?.length);
|
||||
|
||||
content.parts?.forEach((part, index) => {
|
||||
if (part.inlineData) {
|
||||
const fileName = `fantasy_warrior_alt_${index}`;
|
||||
const fileName = `fantasy_warrior_fallback_${index}`;
|
||||
const inlineData = part.inlineData;
|
||||
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');
|
||||
saveBinaryFile(`${fileName}.${fileExtension}`, buffer);
|
||||
} else if (part.text) {
|
||||
console.log('Fallback description:', part.text);
|
||||
}
|
||||
});
|
||||
}
|
||||
} 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