Update pages/api/generate.js
Browse files- pages/api/generate.js +36 -40
pages/api/generate.js
CHANGED
@@ -10,7 +10,7 @@ export default async function handler(req, res) {
|
|
10 |
const { prompt, drawingData, customApiKey } = req.body;
|
11 |
|
12 |
// Log request details (truncating drawingData for brevity)
|
13 |
-
console.log("
|
14 |
prompt,
|
15 |
hasDrawingData: !!drawingData,
|
16 |
drawingDataLength: drawingData ? drawingData.length : 0,
|
@@ -18,8 +18,8 @@ export default async function handler(req, res) {
|
|
18 |
hasCustomApiKey: !!customApiKey,
|
19 |
});
|
20 |
|
21 |
-
if (!
|
22 |
-
return res.status(400).json({ error: "
|
23 |
}
|
24 |
|
25 |
// Use custom API key if provided, otherwise use the one from environment variables
|
@@ -34,7 +34,7 @@ export default async function handler(req, res) {
|
|
34 |
|
35 |
const genAI = new GoogleGenerativeAI(apiKey);
|
36 |
|
37 |
-
// Set responseModalities to include "Image"
|
38 |
const model = genAI.getGenerativeModel({
|
39 |
model: "gemini-2.0-flash-exp-image-generation",
|
40 |
generationConfig: {
|
@@ -43,37 +43,30 @@ export default async function handler(req, res) {
|
|
43 |
});
|
44 |
|
45 |
try {
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
];
|
67 |
-
console.log("Using multipart content with drawing data and prompt");
|
68 |
-
} else {
|
69 |
-
// Use text-only prompt if no drawing is provided
|
70 |
-
generationContent = prompt || "Convert a basic line into a stunning artwork.";
|
71 |
-
console.log("Using text-only prompt");
|
72 |
-
}
|
73 |
|
74 |
-
console.log("
|
75 |
const response = await model.generateContent(generationContent);
|
76 |
-
console.log("
|
77 |
|
78 |
// Initialize response data
|
79 |
const result = {
|
@@ -84,26 +77,29 @@ export default async function handler(req, res) {
|
|
84 |
|
85 |
// Process response parts
|
86 |
for (const part of response.response.candidates[0].content.parts) {
|
87 |
-
// Based on the part type, either get the text or image data
|
88 |
if (part.text) {
|
89 |
result.message = part.text;
|
90 |
console.log("Received text response:", part.text);
|
91 |
} else if (part.inlineData) {
|
92 |
const imageData = part.inlineData.data;
|
93 |
-
console.log("Received image data, length:", imageData.length);
|
94 |
-
|
95 |
-
// Include the base64 data in the response
|
96 |
result.imageData = imageData;
|
97 |
}
|
98 |
}
|
99 |
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
101 |
return res.status(200).json(result);
|
102 |
} catch (error) {
|
103 |
-
console.error("Error
|
104 |
return res.status(500).json({
|
105 |
success: false,
|
106 |
-
error: error.message || "Failed to
|
|
|
107 |
});
|
108 |
}
|
109 |
}
|
|
|
10 |
const { prompt, drawingData, customApiKey } = req.body;
|
11 |
|
12 |
// Log request details (truncating drawingData for brevity)
|
13 |
+
console.log("Fashion Design Conversion Request:", {
|
14 |
prompt,
|
15 |
hasDrawingData: !!drawingData,
|
16 |
drawingDataLength: drawingData ? drawingData.length : 0,
|
|
|
18 |
hasCustomApiKey: !!customApiKey,
|
19 |
});
|
20 |
|
21 |
+
if (!drawingData) {
|
22 |
+
return res.status(400).json({ error: "Fashion design drawing is required" });
|
23 |
}
|
24 |
|
25 |
// Use custom API key if provided, otherwise use the one from environment variables
|
|
|
34 |
|
35 |
const genAI = new GoogleGenerativeAI(apiKey);
|
36 |
|
37 |
+
// Set responseModalities to include "Image" for image generation
|
38 |
const model = genAI.getGenerativeModel({
|
39 |
model: "gemini-2.0-flash-exp-image-generation",
|
40 |
generationConfig: {
|
|
|
43 |
});
|
44 |
|
45 |
try {
|
46 |
+
// Create a content part with the base64-encoded fashion design
|
47 |
+
const imagePart = {
|
48 |
+
inlineData: {
|
49 |
+
data: drawingData,
|
50 |
+
mimeType: "image/png",
|
51 |
+
},
|
52 |
+
};
|
53 |
+
|
54 |
+
// Fashion-specific instructions to preserve design integrity
|
55 |
+
const fashionPrompt = `Convert this fashion design sketch into a high-quality product image with the following requirements:
|
56 |
+
1. Maintain EXACT proportions, silhouette, and design details from the original sketch
|
57 |
+
2. Use realistic fabrics and textures appropriate for the design
|
58 |
+
3. Add professional lighting and shadows to enhance dimensionality
|
59 |
+
4. Keep the background neutral (white or light gray) unless specified
|
60 |
+
${prompt ? "Additional instructions: " + prompt : ""}`;
|
61 |
+
|
62 |
+
const generationContent = [
|
63 |
+
imagePart,
|
64 |
+
{ text: fashionPrompt },
|
65 |
+
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
console.log("Processing fashion design conversion...");
|
68 |
const response = await model.generateContent(generationContent);
|
69 |
+
console.log("Fashion conversion completed");
|
70 |
|
71 |
// Initialize response data
|
72 |
const result = {
|
|
|
77 |
|
78 |
// Process response parts
|
79 |
for (const part of response.response.candidates[0].content.parts) {
|
|
|
80 |
if (part.text) {
|
81 |
result.message = part.text;
|
82 |
console.log("Received text response:", part.text);
|
83 |
} else if (part.inlineData) {
|
84 |
const imageData = part.inlineData.data;
|
85 |
+
console.log("Received product image data, length:", imageData.length);
|
|
|
|
|
86 |
result.imageData = imageData;
|
87 |
}
|
88 |
}
|
89 |
|
90 |
+
// Verify the output maintains design integrity
|
91 |
+
if (!result.imageData) {
|
92 |
+
throw new Error("The generated image did not preserve the original design adequately");
|
93 |
+
}
|
94 |
+
|
95 |
+
console.log("Sending successful fashion conversion response");
|
96 |
return res.status(200).json(result);
|
97 |
} catch (error) {
|
98 |
+
console.error("Error in fashion design conversion:", error);
|
99 |
return res.status(500).json({
|
100 |
success: false,
|
101 |
+
error: error.message || "Failed to convert fashion design to product image",
|
102 |
+
suggestion: "Please ensure your design sketch has clear lines and distinct elements",
|
103 |
});
|
104 |
}
|
105 |
}
|