app.post('/generate-video', async (req, res) => { const { prompt } = req.body; try { const response = await axios({ // Switching to a more modern, efficient motion model url: "https://api-inference.huggingface.co/models/guoyww/animatediff-motion-adapter-v1-5-2", method: "POST", headers: { Authorization: `Bearer ${process.env.HF_TOKEN}`, "Content-Type": "application/json", // CRITICAL: This tells HF to wait for the model to load instead of 503 erroring "X-Wait-For-Model": "true" }, data: JSON.stringify({ inputs: prompt }), responseType: 'arraybuffer', timeout: 120000 // Increase local timeout to 2 minutes }); res.set('Content-Type', 'video/mp4'); res.send(response.data); } catch (error) { if (error.response && error.response.status === 503) { res.status(503).json({ error: "The AI is still waking up. Please wait 1 minute and try one more time." }); } else { console.error("AI Error:", error.message); res.status(500).json({ error: "Generation timed out. The free API is heavily loaded right now." }); } } });