| import { GoogleGenerativeAI } from "@google/generative-ai"; |
|
|
| export default async function handler(req, res) { |
| |
| if (req.method !== "POST") { |
| return res.status(405).json({ error: "Method not allowed" }); |
| } |
|
|
| |
| const { prompt, drawingData, customApiKey } = req.body; |
|
|
| |
| 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" }); |
| } |
|
|
| |
| 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); |
|
|
| |
| const model = genAI.getGenerativeModel({ |
| model: "gemini-2.0-flash-exp-image-generation", |
| generationConfig: { |
| responseModalities: ["Text", "Image"], |
| }, |
| }); |
|
|
| try { |
| |
| const imagePart = { |
| inlineData: { |
| data: drawingData, |
| mimeType: "image/png", |
| }, |
| }; |
|
|
| |
| 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"); |
|
|
| |
| const result = { |
| success: true, |
| message: "", |
| imageData: null, |
| }; |
|
|
| |
| 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; |
| } |
| } |
|
|
| |
| 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", |
| }); |
| } |
| } |