Skip to content

Instantly share code, notes, and snippets.

@tikikun
Created July 3, 2023 09:04
Show Gist options
  • Save tikikun/71a4ca6e18fe00b4aaefe7feb4fb05ec to your computer and use it in GitHub Desktop.
Save tikikun/71a4ca6e18fe00b4aaefe7feb4fb05ec to your computer and use it in GitHub Desktop.
import * as zlib from 'zlib';
import fetch from 'node-fetch';
// model input params
let prompt = "mdjrny-v4 style 1girl watch at you";
let negative_prompt = "NONE"; // replace NONE with actual negative prompt if any
let samples = 1; // no.of images to generate
let scheduler = "DPMSolverMultistepScheduler";
let steps = 20;
let guidance_scale = 7.5;
let seed = 123123123;
let url = "https://random_alan.jan.ai/v2/models/stable_diffusion/versions/1/infer";
let message = {
"inputs": [
{
"name": "PROMPT",
"shape": [1],
"datatype": "BYTES",
"data": [prompt],
},
{
"name": "NEGATIVE_PROMPT",
"shape": [1],
"datatype": "BYTES",
"data": [negative_prompt],
},
{
"name": "SAMPLES",
"shape": [1],
"datatype": "INT32",
"data": [samples],
},
{
"name": "SCHEDULER",
"shape": [1],
"datatype": "BYTES",
"data": [scheduler],
},
{
"name": "STEPS",
"shape": [1],
"datatype": "INT32",
"data": [steps],
},
{
"name": "GUIDANCE_SCALE",
"shape": [1],
"datatype": "FP32",
"data": [guidance_scale],
},
{
"name": "SEED",
"shape": [1],
"datatype": "INT64",
"data": [seed],
},
],
"outputs": [
{
"name": "IMAGES",
"parameters": {"binary_data": False},
}
],
};
let stringMessage = JSON.stringify(message);
let bytesMessage = Buffer.from(stringMessage, 'utf-8');
let requestBody = zlib.gzipSync(bytesMessage);
fetch(url, {
method: 'POST',
body: requestBody,
headers: {
'Content-Encoding': 'gzip',
'Accept-Encoding': 'gzip',
'Inference-Header-Content-Length': String(bytesMessage.length),
},
})
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error:' + err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment