jbilcke-hf HF staff commited on
Commit
8ff0fc7
β€’
1 Parent(s): cda1d47

add exception interceptors

Browse files
src/index.mts CHANGED
@@ -39,6 +39,14 @@ const port = 7860
39
 
40
  let isRendering = false
41
 
 
 
 
 
 
 
 
 
42
  // fix this error: "PayloadTooLargeError: request entity too large"
43
  // there are multiple version because.. yeah well, it's Express!
44
  // app.use(bodyParser.json({limit: '50mb'}));
 
39
 
40
  let isRendering = false
41
 
42
+ process.on('unhandledRejection', (reason: string, p: Promise<any>) => {
43
+ console.error('Unhandled Rejection at:', p, 'reason:', reason);
44
+ })
45
+
46
+ process.on('uncaughtException', (error: Error) => {
47
+ console.error(`Caught exception: ${error}\n` + `Exception origin: ${error.stack}`);
48
+ })
49
+
50
  // fix this error: "PayloadTooLargeError: request entity too large"
51
  // there are multiple version because.. yeah well, it's Express!
52
  // app.use(bodyParser.json({limit: '50mb'}));
src/providers/image-generation/generateImageLCMGradio.mts CHANGED
@@ -3,6 +3,7 @@ import { client } from "@gradio/client"
3
 
4
  import { generateSeed } from "../../utils/misc/generateSeed.mts"
5
  import { getValidNumber } from "../../utils/validators/getValidNumber.mts"
 
6
 
7
  // TODO add a system to mark failed instances as "unavailable" for a couple of minutes
8
  // console.log("process.env:", process.env)
@@ -89,5 +90,9 @@ export async function generateImageLCMAsBase64(options: {
89
  if (!result?.length) {
90
  throw new Error(`the returned image was empty`)
91
  }
 
 
 
 
92
  return result
93
  }
 
3
 
4
  import { generateSeed } from "../../utils/misc/generateSeed.mts"
5
  import { getValidNumber } from "../../utils/validators/getValidNumber.mts"
6
+ import { convertToWebp } from "../../utils/image/convertToWebp.mts"
7
 
8
  // TODO add a system to mark failed instances as "unavailable" for a couple of minutes
9
  // console.log("process.env:", process.env)
 
90
  if (!result?.length) {
91
  throw new Error(`the returned image was empty`)
92
  }
93
+
94
+ // const finalImage = await convertToWebp(result)
95
+ // return finalImage
96
+
97
  return result
98
  }
src/utils/image/convertToWebp.mts ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sharp from "sharp"
2
+
3
+ export async function convertToWebp(imgBase64: string): Promise<string> {
4
+ // Convert base64 to buffer
5
+ const tmpBuffer = Buffer.from(imgBase64, 'base64')
6
+
7
+ // Resize the buffer to the target size
8
+ const newBuffer = await sharp(tmpBuffer)
9
+ .webp({
10
+ // for options please see https://sharp.pixelplumbing.com/api-output#webp
11
+
12
+ // preset: "photo",
13
+
14
+ // effort: 3,
15
+
16
+ // for a PNG-like quality
17
+ // lossless: true,
18
+
19
+ // by default it is quality 80
20
+ quality: 90,
21
+
22
+ // nearLossless: true,
23
+
24
+ // use high quality chroma subsampling
25
+ smartSubsample: true,
26
+ })
27
+ .toBuffer()
28
+
29
+ // Convert the buffer back to base64
30
+ const newImageBase64 = newBuffer.toString('base64')
31
+
32
+ return `data:image/webp;base64,${newImageBase64}`
33
+ }