import express from 'express'; import fetch from 'node-fetch'; import bodyParser from 'body-parser'; import dotenv from 'dotenv'; dotenv.config(); const app = express(); const port = 3000; const apiKey = process.env.KEY; app.use(bodyParser.json()); app.post('/generate-image', async (req, res) => { const { prompt } = req.body; if (!prompt) { return res.status(400).send('Missing "prompt" in request body'); } try { const response = await fetch('https://api-inference.huggingface.co/models/your-model-name', { method: 'POST', headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ inputs: prompt }) }); if (!response.ok) { throw new Error(`Error from Hugging Face API: ${response.statusText}`); } const imageBuffer = await response.buffer(); const base64Image = imageBuffer.toString('base64'); res.json({ image: base64Image }); } catch (error) { console.error('Error generating image:', error); res.status(500).send('Error generating image'); } }); app.listen(port, () => { console.log(`Server is running on http://localhost:${port}`); });