Spaces:
Running
Running
File size: 4,367 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 101 102 103 104 105 106 107 108 |
import { GoogleGenerativeAI } from '@google/generative-ai';
export default async function handler(req, res) {
if (req.method !== 'POST') {
return res.status(405).json({ error: 'Method not allowed' });
}
try {
// Extract material description from request body
const { materialDescription } = req.body;
if (!materialDescription) {
return res.status(200).json({
name: 'Unknown Material',
details: 'Add standard material properties with accurate surface texturing.'
});
}
// Initialize the Google Generative AI with API key
const apiKey = process.env.GEMINI_API_KEY;
if (!apiKey) {
console.error("Missing GEMINI_API_KEY");
// Return a 200 with fallback data instead of error
return res.status(200).json({
name: materialDescription,
details: `Emphasize the characteristic properties of ${materialDescription.toLowerCase()} with accurate surface texturing.`
});
}
try {
const genAI = new GoogleGenerativeAI(apiKey);
const model = genAI.getGenerativeModel({
model: "gemini-pro",
generationConfig: {
temperature: 0.7,
topP: 0.8,
topK: 40
}
});
// Create prompt for material enhancement
const prompt = `Given the material description "${materialDescription}", provide:
1. A concise material name (2-3 words maximum, keep original name if simple enough)
2. Please provide contextual, specific material properties to enhance the existing prompt:
"Transform this sketch into a [material] material. Render it in a high-end 3D visualization style with professional studio lighting against a pure black background. Make it look like an elegant Cinema 4D and Octane rendering with detailed material properties and characteristics. The final result should be a premium product visualization with perfect studio lighting, crisp shadows, and high-end material definition."
Format response STRICTLY as JSON:
{
"name": "Material Name",
"details": "Only additional material properties,"
}
Requirements:
- Keep name simple if input is already concise (e.g., "rusted iron" stays as "Rusted Iron")
- Simplify complex descriptions (e.g., "glass beads made of fire" becomes "Molten Glass")
- Details should focus on physical properties, visual characteristics, and rendering techniques. Feel free to be creative! :)
- Do not repeat what's already in the base prompt (black background, lighting, etc)
- Keep details concise and technical`;
// Generate content with the model
const result = await model.generateContent(prompt);
const response = result.response;
const responseText = response.text();
try {
// Try to parse the response as JSON
const jsonResponse = JSON.parse(responseText);
// Validate the response format
if (!jsonResponse.name || !jsonResponse.details) {
throw new Error('Invalid response format from AI');
}
return res.status(200).json(jsonResponse);
} catch (error) {
console.error('Error parsing AI response or invalid format:', error.message);
// Fallback: Use the original description, capitalized, and provide empty details.
// This keeps the JSON structure consistent for the frontend.
const capitalizedName = materialDescription
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(' ');
return res.status(200).json({
name: capitalizedName,
details: ""
});
}
} catch (aiError) {
console.error('Error calling Generative AI Model:', aiError);
// Fallback if the AI call itself fails
const capitalizedName = materialDescription
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join(' ');
return res.status(500).json({
name: capitalizedName,
details: ""
});
}
} catch (error) {
// Catch errors before AI initialization (e.g., API key issue)
console.error('API handler setup error:', error);
return res.status(500).json({ error: 'Internal server error' });
}
} |