images formats

#1
by victor HF staff - opened
Files changed (1) hide show
  1. routes/generate/index.js +39 -35
routes/generate/index.js CHANGED
@@ -1,40 +1,40 @@
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
- height: 512,
16
- width: 512
17
  },
18
  "portrait-3_4": {
19
- height: 512,
20
- width: 384
21
  },
22
  "portrait-9_16": {
23
- height: 512,
24
- width: 288
25
  },
26
  "landscape-4_3": {
27
- height: 384,
28
- width: 512
29
  },
30
  "landscape-16_9": {
31
- height: 288,
32
- width: 512
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,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.readFile(process.env.PUBLIC_FILE_UPLOAD_DIR + "/" + slug + ".png")?.catch(() => null)
 
 
47
  if (file) {
48
- return reply
49
- .header('Content-Type', 'image/jpeg')
50
- .send(file);
51
  }
52
 
53
- const { height, width } = IMAGE_SIZES[format ?? "square"] ?? IMAGE_SIZES["square"];
 
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.opendir(process.env.PUBLIC_FILE_UPLOAD_DIR).catch(() => null)
68
- if (!dir) await fs.mkdir(process.env.PUBLIC_FILE_UPLOAD_DIR)
69
- await fs.writeFile(process.env.PUBLIC_FILE_UPLOAD_DIR + "/" + slug + ".png", array)
 
 
 
 
 
70
 
71
- return reply
72
- .header('Content-Type', 'image/jpeg')
73
- .send(array);
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
+ };