Spaces:
Running
Running
// Default configuration | |
const DEFAULT_CONFIG = { | |
model: 'deepseek-ai/DeepSeek-V2.5', | |
temperature: 0.7, | |
max_tokens: 2048, | |
top_p: 0.95 | |
}; | |
const { HfInference } = require('@huggingface/inference'); | |
// Initialize HuggingFace client | |
const hf = new HfInference(process.env.HF_API_TOKEN); | |
const generateHtml = async(req, res) => { | |
try { | |
const userPrompt = req.query.prompt; | |
if (!userPrompt) { | |
return res.status(400).json({ | |
error: 'Missing required "prompt" query parameter' | |
}); | |
} | |
// Build configuration by merging defaults with any provided query parameters | |
const config = { | |
...DEFAULT_CONFIG, | |
...(req.query.model && { model: req.query.model }), | |
...(req.query.temperature && { temperature: parseFloat(req.query.temperature) }), | |
...(req.query.max_tokens && { max_tokens: parseInt(req.query.max_tokens) }), | |
...(req.query.top_p && { top_p: parseFloat(req.query.top_p) }) | |
}; | |
// Create the system and user messages | |
const messages = [ | |
{ | |
role: "system", | |
content: `You are a specialized AI model for generating webpage content. | |
Your responses must always be valid HTML that can be directly rendered in a browser. | |
You can include JavaScript and CSS within appropriate tags if the request calls for interactive elements. | |
Ensure all HTML is properly structured and follows the specified framework requirements. | |
Generate only the HTML content - do not include any explanations or markdown. | |
Do not wrap the response in code blocks or quotes - respond with pure HTML.` | |
}, | |
{ | |
role: "user", | |
content: userPrompt | |
} | |
]; | |
// Make chat completion call to HuggingFace with merged configuration | |
const result = await hf.chatCompletion({ | |
...config, | |
messages: messages | |
}); | |
// Extract generated HTML from the response | |
const generatedHtml = result.choices[0].message.content | |
// Validate that the response contains HTML | |
/* Not a valid check... | |
if (!generatedHtml.includes('<')) { | |
throw new Error('Generated content does not appear to be valid HTML'); | |
} */ | |
// Set content type to HTML and send response | |
res.setHeader('Content-Type', 'text/html'); | |
res.send(generatedHtml); | |
} catch (error) { | |
console.error('HTML generation error:', error); | |
res.status(500).json({ | |
error: error.message, | |
details: error.message | |
}); | |
} | |
}; | |
module.exports = {generateHtml} |