AiVidGen / server.js
Boombaaa's picture
Update server.js
79fd5e0 verified
const express = require('express');
const axios = require('axios');
const cors = require('cors');
const path = require('path');
require('dotenv').config();
const app = express();
const PORT = 7860; // Required port for Hugging Face Spaces
// 1. Middleware
app.use(cors());
app.use(express.json());
// 2. Tell Express where your static files (CSS/JS/Images) are
app.use(express.static(path.join(__dirname, 'public')));
// 3. FORCE the root URL to show your index.html (Fixes "Cannot GET /")
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// 4. The Video Generation Route
app.post('/generate-video', async (req, res) => {
const { prompt } = req.body;
if (!prompt) {
return res.status(400).send("Prompt is required");
}
try {
// Calling the Hugging Face AI Model
const response = await axios({
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"
},
data: JSON.stringify({ inputs: prompt }),
responseType: 'arraybuffer',
});
res.set('Content-Type', 'video/mp4');
res.send(response.data);
} catch (error) {
console.error("AI Error:", error.message);
res.status(500).send("The AI model is currently busy or loading. Please try again in a moment.");
}
});
// 5. Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});