如何在JS中获取这种数据?

huangapple go评论105阅读模式
英文:

How can I get this kind of data in JS?

问题

我正在尝试在我的小项目中使用NovelAI的图像生成功能。然而,当我尝试使用API时出现了以下情况。
首先,这个截图 是在Firefox中打开的NovelAI官方网站的F12菜单。当我解码这个Base64字符串时,它给我一个包含生成的图像_0.png的ZIP文件。
但是当我尝试使用异步fetch()接收它时,我只得到这样的数据,而且无法使用。
显然这看起来像我之前看到的Base64字符串的解码版本,但当我保存它,将其重命名为~~~.zip,并尝试解压它时,它无法工作,它说文件已损坏。
这是我的源代码,可能的问题是什么?

    await fetch("https://api.novelai.net/ai/generate-image",{method:"POST",headers: {'Content-Type':'application/json','Authorization': `Bearer ${accessToken}`,'Accept': "*/*"},body:JSON.stringify({
        "input": `masterpiece, best quality, ${queryparse.data.input}`,
        "model": model,
        "action": "generate",
        "parameters": {
            "width": 768,
            "height": 512,
            "scale": 11,
            "sampler": "k_dpmpp_2m",
            "steps": 28,
            "seed": getRandomInt(10000000000),
            "n_samples": 1,
            "ucPreset": 0,
            "qualityToggle": true,
            "sm": false,
            "sm_dyn": false,
            "dynamic_thresholding": false,
            "controlnet_strength": 1,
            "legacy": false,
            "add_original_image": false,
            "negative_prompt": "lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry"}})}
    ).then(async (response) => {
        ctx.status = response.status
        // ctx.response = response.data // i am a genius        ok apparently i wasn't
        ctx.body = (await response.text())
    })

我还尝试了axios库,但它也没有工作,只是给了我类似于那样的东西。

英文:

so I am trying to use NovelAI's image generation in my small project. However, when I tried to use the API this happened.
First, this screenshot is the F12 menu of NovelAI's official site opened in Firefox. When i decode this base64 string it gives me a nice zip file which contains the generated image_0.png.
But when I try receiving it with async fetch() I only get this kind of data, and it is unusable.
apparently this looks like the decoded version of the Base64 string I saw earlier,but when I save it, rename it to ~~~.zip, and try unzip it it doesn't work, it says the file is corrupted.
This is my source code, what could be the problem?

    await fetch("https://api.novelai.net/ai/generate-image",{method:"POST",headers: {'Content-Type':'application/json','Authorization': `Bearer ${accessToken}`,'Accept': "*/*"},body:JSON.stringify({
        "input": `masterpiece, best quality, ${queryparse.data.input}`,
        "model": model,
        "action": "generate",
        "parameters": {
            "width": 768,
            "height": 512,
            "scale": 11,
            "sampler": "k_dpmpp_2m",
            "steps": 28,
            "seed": getRandomInt(10000000000),
            "n_samples": 1,
            "ucPreset": 0,
            "qualityToggle": true,
            "sm": false,
            "sm_dyn": false,
            "dynamic_thresholding": false,
            "controlnet_strength": 1,
            "legacy": false,
            "add_original_image": false,
            "negative_prompt": "lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry"}})}
    ).then(async (response) => {
        ctx.status = response.status
        // ctx.response = response.data // i am a genius        ok apparently i wasn't
        ctx.body = (await response.text())
    })

I also tried with the axios library but it also didn't work, just gave me something similar to that.

答案1

得分: 1

已解决,response.arrayBuffer() 用于原始数据。具体来说,我使用它来返回数据的Base64版本:

function arrayBufferToBase64(buffer: ArrayBuffer) {
    let binary = '';
    const bytes = new Uint8Array(buffer);
    const len = bytes.byteLength;
    for (let i = 0; i < len; i += 1) {
        binary += String.fromCharCode(bytes[i]);
    }
    return btoa(binary);
}

// ~~~~~ 一些代码 ~~~~~
    ).then(async (response) => {
        ctx.status = response.status
        ctx.body = arrayBufferToBase64(await response.arrayBuffer())
    })
    await next()
})

如果有人需要使用NovelAI的API来生成图像,这是获取返回的ZIP文件数据的Base64数据的方法。此外,它的名称为 images.zip,包含一个名为 image_0.png 的单个图像文件。

英文:

Solved, response.arrayBuffer() worked for raw data. To be specific, I used this to return the Base64 version of the data:

function arrayBufferToBase64(buffer: ArrayBuffer) {
    let binary = &#39;&#39;;
    const bytes = new Uint8Array( buffer );
    const len = bytes.byteLength;
    for (let i = 0; i &lt; len; i+=1) {
        binary += String.fromCharCode( bytes[ i ] );
    }
    return btoa( binary );
}

// ~~~~~ some code ~~~~~
    ).then(async (response) =&gt; {
        ctx.status = response.status
        ctx.body = arrayBufferToBase64(await response.arrayBuffer())
    })
    await next()
})

if anyone needs to use NovelAI's API for image generation here is the way to get the Base64 data of the returned ZIP file. Also, it is named images.zip and contains a single image file called image_0.png.

huangapple
  • 本文由 发表于 2023年6月15日 21:33:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定