| 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; |
|
|
| |
| app.use(cors()); |
| app.use(express.json()); |
|
|
| |
| app.use(express.static(path.join(__dirname, 'public'))); |
|
|
| |
| app.get('/', (req, res) => { |
| res.sendFile(path.join(__dirname, 'public', 'index.html')); |
| }); |
|
|
| |
| app.post('/generate-video', async (req, res) => { |
| const { prompt } = req.body; |
|
|
| if (!prompt) { |
| return res.status(400).send("Prompt is required"); |
| } |
|
|
| try { |
| |
| 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."); |
| } |
| }); |
|
|
| |
| app.listen(PORT, () => { |
| console.log(`Server is running on port ${PORT}`); |
| }); |