Spaces:
Running
Running
File size: 2,660 Bytes
5f07a23 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import { GoogleGenerativeAI } from "@google/generative-ai";
// Configure API route options
export const config = {
api: {
bodyParser: {
sizeLimit: '10mb' // Increase the body size limit to 10MB
}
}
};
export default async function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
try {
const { prompt, imageData, customApiKey } = req.body;
// Use custom API key if provided
const apiKey = customApiKey || process.env.GEMINI_API_KEY;
const genAI = new GoogleGenerativeAI(apiKey);
// Configure the model with the correct settings
const model = genAI.getGenerativeModel({
model: "gemini-2.0-flash-exp-image-generation",
generationConfig: {
temperature: 1,
topP: 0.95,
topK: 40,
maxOutputTokens: 8192,
responseModalities: ["image", "text"]
}
});
// Prepare a more specific prompt for the refinement
const refinementPrompt = `Please refine this image according to the following instruction: "${prompt}".
Maintain the artistic quality and style while applying the requested changes.
Ensure the output is a high-quality image that preserves the original intent while incorporating the refinement.`;
// Prepare the generation content with both image and prompt
const generationContent = [
{
inlineData: {
data: imageData,
mimeType: "image/png"
}
},
{ text: refinementPrompt }
];
// Generate content with the image and prompt
const result = await model.generateContent(generationContent);
const response = await result.response;
// Process the response to extract image data
let refinedImageData = null;
for (const part of response.candidates[0].content.parts) {
if (part.inlineData) {
refinedImageData = part.inlineData.data;
break;
}
}
if (!refinedImageData) {
throw new Error('No image data received from the API');
}
// Return the refined image data
return res.status(200).json({
success: true,
imageData: refinedImageData
});
} catch (error) {
console.error('Error in /api/refine:', error);
// Check for specific error types
if (error.message?.includes('quota') || error.message?.includes('Resource has been exhausted')) {
return res.status(429).json({
error: 'API quota exceeded. Please try again later or use your own API key.'
});
}
return res.status(500).json({
error: 'An error occurred during image refinement.'
});
}
} |