import { GoogleGenerativeAI } from "@google/generative-ai"; export default async function handler(req, res) { // Only allow POST requests if (req.method !== "POST") { return res.status(405).json({ error: "Method not allowed" }); } // Get prompt, drawing, and custom API key from request body const { prompt, drawingData, customApiKey } = req.body; // Log request details (truncating drawingData for brevity) console.log("Fashion Design Conversion Request:", { prompt, hasDrawingData: !!drawingData, drawingDataLength: drawingData ? drawingData.length : 0, drawingDataSample: drawingData ? `${drawingData.substring(0, 50)}... (truncated)` : null, hasCustomApiKey: !!customApiKey, }); if (!drawingData) { return res.status(400).json({ error: "Fashion design drawing is required" }); } // Use custom API key if provided, otherwise use the one from environment variables const apiKey = customApiKey || process.env.GEMINI_API_KEY; if (!apiKey) { return res.status(400).json({ success: false, error: "No API key available. Please provide a valid Gemini API key.", }); } const genAI = new GoogleGenerativeAI(apiKey); // Set responseModalities to include "Image" for image generation const model = genAI.getGenerativeModel({ model: "gemini-2.0-flash-exp-image-generation", generationConfig: { responseModalities: ["Text", "Image"], }, }); try { // Create a content part with the base64-encoded fashion design const imagePart = { inlineData: { data: drawingData, mimeType: "image/png", }, }; // Fashion-specific instructions to preserve design integrity const fashionPrompt = `Convert this fashion design sketch into a high-quality product image with the following requirements: 1. Maintain EXACT proportions, silhouette, and design details from the original sketch 2. Use realistic fabrics and textures appropriate for the design 3. Add professional lighting and shadows to enhance dimensionality 4. Keep the background neutral (white or light gray) unless specified ${prompt ? "Additional instructions: " + prompt : ""}`; const generationContent = [ imagePart, { text: fashionPrompt }, ]; console.log("Processing fashion design conversion..."); const response = await model.generateContent(generationContent); console.log("Fashion conversion completed"); // Initialize response data const result = { success: true, message: "", imageData: null, }; // Process response parts for (const part of response.response.candidates[0].content.parts) { if (part.text) { result.message = part.text; console.log("Received text response:", part.text); } else if (part.inlineData) { const imageData = part.inlineData.data; console.log("Received product image data, length:", imageData.length); result.imageData = imageData; } } // Verify the output maintains design integrity if (!result.imageData) { throw new Error("The generated image did not preserve the original design adequately"); } console.log("Sending successful fashion conversion response"); return res.status(200).json(result); } catch (error) { console.error("Error in fashion design conversion:", error); return res.status(500).json({ success: false, error: error.message || "Failed to convert fashion design to product image", suggestion: "Please ensure your design sketch has clear lines and distinct elements", }); } }