TDN-M commited on
Commit
e34b6c6
·
verified ·
1 Parent(s): 00a11a2

Update pages/api/generate.js

Browse files
Files changed (1) hide show
  1. pages/api/generate.js +33 -29
pages/api/generate.js CHANGED
@@ -2,33 +2,33 @@ import { GoogleGenerativeAI } from "@google/generative-ai";
2
 
3
  export default async function handler(req, res) {
4
  // Only allow POST requests
5
- if (req.method !== 'POST') {
6
- return res.status(405).json({ error: 'Method not allowed' });
7
  }
8
 
9
  // Get prompt, drawing, and custom API key from request body
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,
17
  drawingDataSample: drawingData ? `${drawingData.substring(0, 50)}... (truncated)` : null,
18
- hasCustomApiKey: !!customApiKey
19
  });
20
-
21
- if (!prompt) {
22
- return res.status(400).json({ error: 'Prompt is required' });
23
  }
24
 
25
  // Use custom API key if provided, otherwise use the one from environment variables
26
  const apiKey = customApiKey || process.env.GEMINI_API_KEY;
27
-
28
  if (!apiKey) {
29
- return res.status(400).json({
30
- success: false,
31
- error: 'No API key available. Please provide a valid Gemini API key.'
32
  });
33
  }
34
 
@@ -38,46 +38,50 @@ export default async function handler(req, res) {
38
  const model = genAI.getGenerativeModel({
39
  model: "gemini-2.0-flash-exp-image-generation",
40
  generationConfig: {
41
- responseModalities: ['Text', 'Image']
42
  },
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
- { text: ${prompt}. From the provided 2D plan layout as the starting point. Convert it into a detailed 3D sketch, including walls, doors, windows, and basic furniture placement. || "Enhance the design with a realistic perspective and improve it to create a stunning 3D representation. Maintain the original style of the layout while adding depth and visual appeal" }
 
 
 
 
62
  ];
63
  console.log("Using multipart content with drawing data and prompt");
64
  } else {
65
  // Use text-only prompt if no drawing is provided
66
- generationContent = prompt;
67
  console.log("Using text-only prompt");
68
  }
69
-
70
  console.log("Calling Gemini API...");
71
  const response = await model.generateContent(generationContent);
72
  console.log("Gemini API response received");
73
-
74
  // Initialize response data
75
  const result = {
76
  success: true,
77
- message: '',
78
- imageData: null
79
  };
80
-
81
  // Process response parts
82
  for (const part of response.response.candidates[0].content.parts) {
83
  // Based on the part type, either get the text or image data
@@ -87,19 +91,19 @@ export default async function handler(req, res) {
87
  } else if (part.inlineData) {
88
  const imageData = part.inlineData.data;
89
  console.log("Received image data, length:", imageData.length);
90
-
91
  // Include the base64 data in the response
92
  result.imageData = imageData;
93
  }
94
  }
95
-
96
  console.log("Sending successful response");
97
  return res.status(200).json(result);
98
  } catch (error) {
99
  console.error("Error generating content:", error);
100
- return res.status(500).json({
101
- success: false,
102
- error: error.message || 'Failed to generate image'
103
  });
104
  }
105
- }
 
2
 
3
  export default async function handler(req, res) {
4
  // Only allow POST requests
5
+ if (req.method !== "POST") {
6
+ return res.status(405).json({ error: "Method not allowed" });
7
  }
8
 
9
  // Get prompt, drawing, and custom API key from request body
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,
17
  drawingDataSample: drawingData ? `${drawingData.substring(0, 50)}... (truncated)` : null,
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
26
  const apiKey = customApiKey || process.env.GEMINI_API_KEY;
27
+
28
  if (!apiKey) {
29
+ return res.status(400).json({
30
+ success: false,
31
+ error: "No API key available. Please provide a valid Gemini API key.",
32
  });
33
  }
34
 
 
38
  const model = genAI.getGenerativeModel({
39
  model: "gemini-2.0-flash-exp-image-generation",
40
  generationConfig: {
41
+ responseModalities: ["Text", "Image"],
42
  },
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
+ }From the provided 2D plan layout as the starting point, convert it into a detailed 3D sketch, including walls, doors, windows, and basic furniture placement. Enhance the design with a realistic perspective and improve it to create a stunning 3D representation. Maintain the original style of the layout 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 2D plan layout into a detailed 3D sketch, including walls, doors, windows, and basic furniture placement. Enhance the design with a realistic perspective and improve it to create a stunning 3D representation.";
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 = {
80
  success: true,
81
+ message: "",
82
+ imageData: null,
83
  };
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
 
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
+ }