-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (39 loc) · 1.34 KB
/
Copy pathserver.js
File metadata and controls
50 lines (39 loc) · 1.34 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
import express from "express";
import cors from "cors";
import dotenv from "dotenv";
import { HfInference } from "@huggingface/inference";
dotenv.config();
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.static("public"));
const client = new HfInference(process.env.HF_TOKEN);
app.post("/api/generate", async (req, res) => {
const { prompt, model, width, height } = req.body;
if (!prompt) {
return res.status(400).json({ error: "Prompt is required" });
}
try {
const usedModel = model || "black-forest-labs/FLUX.1-schnell";
const w = width ? Math.floor(width / 16) * 16 : 512;
const h = height ? Math.floor(height / 16) * 16 : 512;
const imageBlob = await client.textToImage({
provider: "nebius",
model: usedModel,
inputs: prompt,
parameters: {
num_inference_steps: 25,
width: w,
height: h,
},
});
const buffer = Buffer.from(await imageBlob.arrayBuffer());
const base64Image = buffer.toString("base64");
res.json({ data: base64Image });
} catch (error) {
console.error("Server error:", error);
res.status(500).json({ error: "Server error while generating image", details: error.message });
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`✅ Server running on http://localhost:${PORT}`));