File size: 3,128 Bytes
5f07a23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } from "@google/generative-ai";
import { NextResponse } from 'next/server';

// Configuration for the API route
export const config = {
  api: {
    bodyParser: {
      sizeLimit: '10mb', // Increase limit to 10MB (adjust if needed)
    },
  },
};

export default async function handler(req, res) {
  // Only allow POST requests
  if (req.method !== 'POST') {
    return res.status(405).json({ error: 'Method not allowed' });
  }

  try {
    const { imageData, customApiKey } = req.body;

    // Validate inputs
    if (!imageData) {
      return res.status(400).json({ error: 'Image data is required' });
    }

    // Set up the API key
    const apiKey = customApiKey || process.env.GEMINI_API_KEY;
    if (!apiKey) {
      return res.status(500).json({ error: 'API key is required' });
    }

    // Initialize the Gemini API
    const genAI = new GoogleGenerativeAI(apiKey);
    const model = genAI.getGenerativeModel({
      model: "gemini-2.0-flash-exp-image-generation",
      generationConfig: {
        temperature: 1,
        topP: 0.95,
        topK: 40,
        maxOutputTokens: 8192,
        responseModalities: ["image", "text"]
      }
    });

    // Create the prompt for doodle conversion
    const prompt = `Could you please convert this image into a black and white doodle.
Requirements:
- Use ONLY pure black lines on a pure white background
- No gray tones or shading
- Maintain the key shapes and outlines
- Follow the original content but simplify if needed
- IMPORTANT: If this image contains any text, logo, or wordmark:
  * Simply convert it to black and white, and pass it through
  * Preserve ALL text exactly as it appears in the original
  * Maintain the exact spelling, letterspacing, and arrangement of letters
  * Keep text legible and clear with high contrast
  * Do not simplify or omit any text elements
  * Text should remain readable in the final doodle, and true to the original :))`;

    // Prepare the generation content
    const generationContent = [
      {
        inlineData: {
          data: imageData,
          mimeType: "image/png"
        }
      },
      { text: prompt }
    ];

    // Generate content
    const result = await model.generateContent(generationContent);
    const response = await result.response;
    
    // Process the response to extract image data
    let convertedImageData = null;
    for (const part of response.candidates[0].content.parts) {
      if (part.inlineData) {
        convertedImageData = part.inlineData.data;
        break;
      }
    }

    if (!convertedImageData) {
      throw new Error('No image data received from the API');
    }

    // Return the converted image data
    return res.status(200).json({
      success: true,
      imageData: convertedImageData
    });
  } catch (error) {
    console.error("Error during doodle conversion:", error);
    const errorMessage = error.message || "An unknown error occurred during conversion.";
    // Ensure JSON response even on error
    return res.status(500).json({ success: false, error: errorMessage });
  }
}