jbilcke-hf HF staff commited on
Commit
92ae21d
·
1 Parent(s): 9002fdc

fixed upscaling

Browse files
Files changed (2) hide show
  1. src/config.mts +1 -1
  2. src/index.mts +124 -0
src/config.mts CHANGED
@@ -11,7 +11,7 @@ export const filesDirPath = path.join(storagePath, "files")
11
  export const pendingFilesDirFilePath = path.join(filesDirPath, "pending")
12
  export const completedFilesDirFilePath = path.join(filesDirPath, "completed")
13
 
14
- // this is a semi-persistent storage (we want to renew it from time to time)
15
  export const cacheDirPath = path.join(storagePath, "cache")
16
  export const renderedDirFilePath = path.join(cacheDirPath, "rendered")
17
 
 
11
  export const pendingFilesDirFilePath = path.join(filesDirPath, "pending")
12
  export const completedFilesDirFilePath = path.join(filesDirPath, "completed")
13
 
14
+ // this is a semi-persistent storage (we will want to renew it from time to time)
15
  export const cacheDirPath = path.join(storagePath, "cache")
16
  export const renderedDirFilePath = path.join(cacheDirPath, "rendered")
17
 
src/index.mts CHANGED
@@ -23,6 +23,7 @@ import { getRenderedScene, renderScene } from "./production/renderScene.mts"
23
  import { parseRenderRequest } from "./utils/parseRenderRequest.mts"
24
  import { loadRenderedSceneFromCache } from "./utils/loadRenderedSceneFromCache.mts"
25
  import { analyzeImage } from "./analysis/analyzeImageWithIDEFICSAndNastyHack.mts"
 
26
  // import { speechToText } from "./speechToText/speechToTextWithWhisperLib.mts"
27
 
28
  initFolders()
@@ -150,6 +151,14 @@ app.post("/listen", async (req, res) => {
150
  // a "fast track" pipeline
151
  app.post("/render", async (req, res) => {
152
 
 
 
 
 
 
 
 
 
153
  console.log(req.body)
154
 
155
  const request = parseRenderRequest(req.body as RenderRequest)
@@ -218,6 +227,121 @@ app.post("/render", async (req, res) => {
218
  }
219
  })
220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  // a "fast track" pipeline
222
  app.get("/render/:renderId", async (req, res) => {
223
 
 
23
  import { parseRenderRequest } from "./utils/parseRenderRequest.mts"
24
  import { loadRenderedSceneFromCache } from "./utils/loadRenderedSceneFromCache.mts"
25
  import { analyzeImage } from "./analysis/analyzeImageWithIDEFICSAndNastyHack.mts"
26
+ import { upscaleImage } from "./utils/upscaleImage.mts"
27
  // import { speechToText } from "./speechToText/speechToTextWithWhisperLib.mts"
28
 
29
  initFolders()
 
151
  // a "fast track" pipeline
152
  app.post("/render", async (req, res) => {
153
 
154
+ if (!hasValidAuthorization(req.headers)) {
155
+ console.log("Invalid authorization")
156
+ res.status(401)
157
+ res.write(JSON.stringify({ error: "invalid token" }))
158
+ res.end()
159
+ return
160
+ }
161
+
162
  console.log(req.body)
163
 
164
  const request = parseRenderRequest(req.body as RenderRequest)
 
227
  }
228
  })
229
 
230
+ // upscale an arbitrary image
231
+ app.post("/upscale", async (req, res) => {
232
+
233
+ if (!hasValidAuthorization(req.headers)) {
234
+ console.log("Invalid authorization")
235
+ res.status(401)
236
+ res.write(JSON.stringify({ error: "invalid token" }))
237
+ res.end()
238
+ return
239
+ }
240
+
241
+ const image = `${req.body.image}`
242
+
243
+ if (!image) {
244
+ console.error("invalid input image")
245
+ res.status(400)
246
+ res.write(JSON.stringify({ error: `invalid input image` }))
247
+ res.end()
248
+ return
249
+ }
250
+
251
+ let response = {
252
+ assetUrl: "",
253
+ error: "",
254
+ }
255
+
256
+ let assetUrl = ""
257
+ try {
258
+ try {
259
+ assetUrl = await upscaleImage(image, 2)
260
+ } catch (err) {
261
+ // hmm.. let's try again?
262
+ try {
263
+ assetUrl = await upscaleImage(image, 2)
264
+ } catch (err) {
265
+ throw new Error(`second attempt to upscale the image failed (${err})`)
266
+ }
267
+ }
268
+ if (!assetUrl) {
269
+ throw new Error(`no image to return`)
270
+ }
271
+
272
+ response.assetUrl = assetUrl
273
+ // console.log(`request ${renderId} is already in cache, so we return that`)
274
+ res.status(200)
275
+ res.write(JSON.stringify(response))
276
+ res.end()
277
+ return
278
+ } catch (err) {
279
+ response.error = `${err}`
280
+ console.error(`${err}`)
281
+ res.status(500)
282
+ res.write(JSON.stringify(response))
283
+ res.end()
284
+ }
285
+ })
286
+
287
+
288
+ // used to upscale an existing image
289
+ // the image has to be completed for this to work
290
+ // ie it has to be in cache
291
+ // it's a bit of a hack endpoint I've added to reduce the laod
292
+ // on the AI Comic Factory, but we don't really need it for other applications
293
+ app.post("/upscale/:renderId", async (req, res) => {
294
+
295
+ const renderId = `${req.params.renderId}`
296
+
297
+ if (!uuidValidate(renderId)) {
298
+ console.error("invalid render id")
299
+ res.status(400)
300
+ res.write(JSON.stringify({ error: `invalid render id` }))
301
+ res.end()
302
+ return
303
+ }
304
+
305
+ let response = {
306
+ assetUrl: "",
307
+ error: "",
308
+ }
309
+
310
+ let assetUrl = ""
311
+ try {
312
+ // we still try to search for it in the cache
313
+ const cached = await loadRenderedSceneFromCache(undefined, renderId)
314
+ try {
315
+ assetUrl = await upscaleImage(cached.assetUrl, 2)
316
+ } catch (err) {
317
+ // hmm.. let's try again?
318
+ try {
319
+ assetUrl = await upscaleImage(cached.assetUrl, 2)
320
+ } catch (err) {
321
+ throw new Error(`second attempt to upscale the image failed (${err})`)
322
+ }
323
+ }
324
+ if (!assetUrl) {
325
+ throw new Error(`no image to return`)
326
+ }
327
+
328
+ response.assetUrl = assetUrl
329
+ // console.log(`request ${renderId} is already in cache, so we return that`)
330
+ res.status(200)
331
+ res.write(response)
332
+ res.end()
333
+ return
334
+ } catch (err) {
335
+ // console.log("renderId not found in cache: "+ err)
336
+ // move along
337
+ response.error = `${err}`
338
+ console.error(`${err}`)
339
+ res.status(500)
340
+ res.write(JSON.stringify(response))
341
+ res.end()
342
+ }
343
+ })
344
+
345
  // a "fast track" pipeline
346
  app.get("/render/:renderId", async (req, res) => {
347