Spaces:
Running
Running
images formats
#1
by
victor
HF staff
- opened
- routes/generate/index.js +39 -35
routes/generate/index.js
CHANGED
@@ -1,40 +1,40 @@
|
|
1 |
-
|
2 |
|
3 |
-
const dotenv = require(
|
4 |
-
const fs = require(
|
5 |
|
6 |
-
const HfInference = require(
|
7 |
|
8 |
dotenv.config();
|
9 |
|
10 |
const inference = new HfInference(process.env.HF_TOKEN);
|
11 |
|
12 |
-
const REPO_NAME = "black-forest-labs/FLUX.1-schnell"
|
13 |
const IMAGE_SIZES = {
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
},
|
18 |
"portrait-3_4": {
|
19 |
-
|
20 |
-
|
21 |
},
|
22 |
"portrait-9_16": {
|
23 |
-
|
24 |
-
|
25 |
},
|
26 |
"landscape-4_3": {
|
27 |
-
|
28 |
-
|
29 |
},
|
30 |
"landscape-16_9": {
|
31 |
-
|
32 |
-
|
33 |
-
}
|
34 |
-
}
|
35 |
|
36 |
module.exports = async function (fastify, opts) {
|
37 |
-
fastify.get(
|
38 |
let { inputs } = request.params;
|
39 |
const { format } = request.query;
|
40 |
if (format) {
|
@@ -43,33 +43,37 @@ module.exports = async function (fastify, opts) {
|
|
43 |
|
44 |
const slug = inputs.replace(/[^a-zA-Z0-9-_ ]/g, "").replace(/ /g, "-");
|
45 |
|
46 |
-
const file = await fs
|
|
|
|
|
47 |
if (file) {
|
48 |
-
return reply
|
49 |
-
.header('Content-Type', 'image/jpeg')
|
50 |
-
.send(file);
|
51 |
}
|
52 |
|
53 |
-
const { height, width } =
|
|
|
54 |
|
55 |
const hfRequest = await inference.textToImage({
|
56 |
inputs,
|
57 |
model: REPO_NAME,
|
58 |
parameters: {
|
59 |
height,
|
60 |
-
width
|
61 |
-
}
|
62 |
-
})
|
63 |
|
64 |
const buffer = await hfRequest.arrayBuffer();
|
65 |
const array = new Uint8Array(buffer);
|
66 |
|
67 |
-
const dir = await fs
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
-
return reply
|
72 |
-
|
73 |
-
|
74 |
-
})
|
75 |
-
}
|
|
|
1 |
+
"use strict";
|
2 |
|
3 |
+
const dotenv = require("dotenv");
|
4 |
+
const fs = require("fs").promises;
|
5 |
|
6 |
+
const HfInference = require("@huggingface/inference").HfInference;
|
7 |
|
8 |
dotenv.config();
|
9 |
|
10 |
const inference = new HfInference(process.env.HF_TOKEN);
|
11 |
|
12 |
+
const REPO_NAME = "black-forest-labs/FLUX.1-schnell";
|
13 |
const IMAGE_SIZES = {
|
14 |
+
square: {
|
15 |
+
width: 1024,
|
16 |
+
height: 1024,
|
17 |
},
|
18 |
"portrait-3_4": {
|
19 |
+
width: 768,
|
20 |
+
height: 1024,
|
21 |
},
|
22 |
"portrait-9_16": {
|
23 |
+
width: 576,
|
24 |
+
height: 1024,
|
25 |
},
|
26 |
"landscape-4_3": {
|
27 |
+
width: 1024,
|
28 |
+
height: 768,
|
29 |
},
|
30 |
"landscape-16_9": {
|
31 |
+
width: 1024,
|
32 |
+
height: 576,
|
33 |
+
},
|
34 |
+
};
|
35 |
|
36 |
module.exports = async function (fastify, opts) {
|
37 |
+
fastify.get("/:inputs", async function (request, reply) {
|
38 |
let { inputs } = request.params;
|
39 |
const { format } = request.query;
|
40 |
if (format) {
|
|
|
43 |
|
44 |
const slug = inputs.replace(/[^a-zA-Z0-9-_ ]/g, "").replace(/ /g, "-");
|
45 |
|
46 |
+
const file = await fs
|
47 |
+
.readFile(process.env.PUBLIC_FILE_UPLOAD_DIR + "/" + slug + ".png")
|
48 |
+
?.catch(() => null);
|
49 |
if (file) {
|
50 |
+
return reply.header("Content-Type", "image/jpeg").send(file);
|
|
|
|
|
51 |
}
|
52 |
|
53 |
+
const { height, width } =
|
54 |
+
IMAGE_SIZES[format ?? "square"] ?? IMAGE_SIZES["square"];
|
55 |
|
56 |
const hfRequest = await inference.textToImage({
|
57 |
inputs,
|
58 |
model: REPO_NAME,
|
59 |
parameters: {
|
60 |
height,
|
61 |
+
width,
|
62 |
+
},
|
63 |
+
});
|
64 |
|
65 |
const buffer = await hfRequest.arrayBuffer();
|
66 |
const array = new Uint8Array(buffer);
|
67 |
|
68 |
+
const dir = await fs
|
69 |
+
.opendir(process.env.PUBLIC_FILE_UPLOAD_DIR)
|
70 |
+
.catch(() => null);
|
71 |
+
if (!dir) await fs.mkdir(process.env.PUBLIC_FILE_UPLOAD_DIR);
|
72 |
+
await fs.writeFile(
|
73 |
+
process.env.PUBLIC_FILE_UPLOAD_DIR + "/" + slug + ".png",
|
74 |
+
array
|
75 |
+
);
|
76 |
|
77 |
+
return reply.header("Content-Type", "image/jpeg").send(array);
|
78 |
+
});
|
79 |
+
};
|
|
|
|