TDN-M commited on
Commit
330b45b
·
verified ·
1 Parent(s): 3c15937

Update pages/api/generate.js

Browse files
Files changed (1) hide show
  1. 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("API Request:", {
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 (!prompt && !drawingData) {
22
- return res.status(400).json({ error: "Either prompt or drawingData is required" });
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" so the model can generate an 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
- let generationContent;
47
-
48
- // If drawingData is provided, include it as an image in the request
49
- if (drawingData) {
50
- // Create a content part with the base64-encoded image
51
- const imagePart = {
52
- inlineData: {
53
- data: drawingData,
54
- mimeType: "image/png",
55
- },
56
- };
57
-
58
- // Combine drawing with text prompt
59
- generationContent = [
60
- imagePart,
61
- {
62
- text: `${
63
- prompt ? prompt + ". " : ""
64
- }Use the provided hand-drawing as the starting line-art, build it up to a stunning photo. Remeber to keep the original line of while adding depth and visual appeal.`,
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("Calling Gemini API...");
75
  const response = await model.generateContent(generationContent);
76
- console.log("Gemini API response received");
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
- console.log("Sending successful response");
 
 
 
 
 
101
  return res.status(200).json(result);
102
  } catch (error) {
103
- console.error("Error generating content:", error);
104
  return res.status(500).json({
105
  success: false,
106
- error: error.message || "Failed to generate image",
 
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
  }