File size: 3,644 Bytes
eceac5b
 
 
 
e34b6c6
 
eceac5b
 
e1a68dd
 
e34b6c6
eceac5b
330b45b
eceac5b
 
 
e1a68dd
e34b6c6
eceac5b
e34b6c6
330b45b
 
eceac5b
 
e1a68dd
 
e34b6c6
e1a68dd
e34b6c6
 
 
e1a68dd
 
 
 
eceac5b
330b45b
eceac5b
 
 
e34b6c6
eceac5b
 
 
 
330b45b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e34b6c6
330b45b
eceac5b
330b45b
e34b6c6
eceac5b
 
 
e34b6c6
 
eceac5b
e34b6c6
eceac5b
 
 
 
 
 
 
330b45b
eceac5b
 
 
e34b6c6
330b45b
 
 
 
 
 
eceac5b
 
330b45b
e34b6c6
 
330b45b
 
eceac5b
 
e34b6c6
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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",
    });
  }
}