-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (49 loc) · 1.95 KB
/
Copy pathserver.js
File metadata and controls
62 lines (49 loc) · 1.95 KB
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
56
57
58
59
60
61
62
import express from "express";
import cors from "cors";
const app = express();
const PORT = process.env.PORT || 3000;
app.use(cors());
app.use(express.json({ limit: "2mb" }));
app.use(express.static("public"));
app.get("/health", (req, res) => res.json({ ok: true }));
app.post("/api/generate", async (req, res) => {
try {
const { prompt, duration = 5 } = req.body || {};
if (!prompt) return res.status(400).json({ error: "Missing prompt." });
const HF_TOKEN = process.env.HF_TOKEN;
const HF_ENDPOINT = process.env.HF_ENDPOINT || "https://api-inference.huggingface.co/models/damo-vilab/text-to-video-ms-1.7b";
if (!HF_TOKEN) {
return res.status(500).json({ error: "Missing HF_TOKEN environment variable on server." });
}
const hfResponse = await fetch(HF_ENDPOINT, {
method: "POST",
headers: {
"Authorization": `Bearer ${HF_TOKEN}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
inputs: prompt,
parameters: {
num_frames: Math.min(64, Math.max(16, Number(duration) * 8 || 32))
}
})
});
const contentType = hfResponse.headers.get("content-type") || "";
if (!hfResponse.ok) {
const text = await hfResponse.text();
return res.status(hfResponse.status).json({ error: "Hugging Face failed.", details: text });
}
if (contentType.includes("application/json")) {
const data = await hfResponse.json();
return res.json({ status: "json_response", data });
}
const arrayBuffer = await hfResponse.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
res.setHeader("Content-Type", contentType || "video/mp4");
res.setHeader("Content-Disposition", "inline; filename=command-cinema-output.mp4");
res.send(buffer);
} catch (err) {
res.status(500).json({ error: "Server error.", details: err.message });
}
});
app.listen(PORT, () => console.log(`Command Cinema Runtime running on port ${PORT}`));