File size: 1,086 Bytes
955ce73
cb7d06b
955ce73
cb7d06b
 
 
 
 
 
 
 
 
56f841d
cb7d06b
 
 
 
56f841d
cb7d06b
 
 
 
 
 
 
 
 
 
 
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

import { RenderedScene, RenderRequest } from "../types.mts"
import { analyzeImage } from "../providers/image-caption/analyzeImageWithIDEFICSAndNastyHack.mts"

export async function renderImageAnalysis(
  request: RenderRequest,
  response: RenderedScene,
): Promise<RenderedScene> {
  response.alt = request.prompt
  
  try {
    // note: this converts a base64 PNG to a base64 JPG (which is good, actually!)
    response.alt = await analyzeImage(response.assetUrl, request.prompt)
    console.log(`analysis worked on the first try!`)
  } catch (err) {
    console.error(`analysis failed the first time.. let's try again..`)
    try {
      response.alt = await analyzeImage(response.assetUrl, request.prompt)
      console.log(`analysis worked on the second try!`)
    } catch (err) {
      console.error(`analysis failed on the second attempt.. let's keep the prompt as a fallback, then :|`)
      // no need to log a catastrophic failure here, since we still have the original (low-res image)
      // to work with
      response.alt = request.prompt
    }
  }

  return response
}