File size: 1,660 Bytes
229b934
 
 
 
 
 
 
585a355
229b934
585a355
229b934
 
 
585a355
 
 
 
 
 
 
 
 
229b934
585a355
 
 
 
 
 
229b934
585a355
229b934
79fd5e0
229b934
585a355
 
 
 
 
 
229b934
585a355
229b934
 
585a355
 
 
 
229b934
 
 
585a355
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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}`);
});