0

I'm querying a dreambooth model from Hugging Face using the inference API and am getting a huge data response string back which starts with: ����çx00çx10JFIFçx00çx01çx01çx00çx00çx01çx0...

Content-type is: image/jpeg

How do I decode this and display it as an image in javascript?

cormacncheese
  • 1,251
  • 1
  • 13
  • 27

2 Answers2

0

Not 100% sure but I suppose something similar to that should do it.

for (var e = atob("����çx00çx10JFIFçx00çx01çx01çx00çx00çx01çx0..."), t = new Array(e.length), r = 0; r < e.length; r++) t[r] = e.charCodeAt(r);
var n = new Uint8Array(t),
    a = new Blob([n], {
        type: "image/jpeg"
    }),
    x = (window.URL || window.webkitURL).createObjectURL(a);

let img = document.createElement("img")

img.src = x;
Nuro007
  • 157
  • 12
0

got it working by including a responseType param in the axios request.

Node.js code:

const inputData = {
    inputs: prompt,
    options: {
        wait_for_model: true,
    },
}

const response = await axios({
    url: `https://api-inference.huggingface.co/models/${model}`,
    method: 'POST',
    headers: {
        Authorization: `Bearer ${process.env.HUGGING_FACE_TOKEN}`,
        Accept: 'application/json',
        'Content-Type': 'application/json',
    },
    data: JSON.stringify(inputData),
    responseType: 'arraybuffer',
})

const mimeType = response.headers['content-type']

const result = response.data

const base64data = Buffer.from(result).toString('base64')

const img = `data:${mimeType};base64,` + base64data

return img

React code:

<img src={img} />
cormacncheese
  • 1,251
  • 1
  • 13
  • 27
  • The image's response type is application/json which is making it unusable in my project. Is there a way to convert this into an image since it does not seem like the API is returning an image? – Brian Mason Nov 26 '22 at 04:47
  • Hey cormac! I'm having some issues setting up HuggingFace Inference API, any chance you could assist? – Roi Mulia Jan 02 '23 at 15:48
  • @RoiMulia yea sure! Are you trying to call a public repo? – cormacncheese Jan 02 '23 at 19:47
  • Hey @cormacncheese, thank you for the fast response! Yes, I am. Maybe you can do some paid-consultant to my company regarding the subject? I can send you all the info via the e-mail. – Roi Mulia Jan 03 '23 at 12:00