xxxxxxxxxx
52
// jshint esversion:8
// Based on an example by Gottfried Haider
// https://shanghai.nyu.edu/academics/faculty/directory/gottfried-haider
const replicate_proxy = "https://splashy-rambunctious-leader.glitch.me/";
// https://replicate.com/meta/llama-2-70b-chat
const version =
"02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3";
let statusP;
function setup() {
noCanvas();
let textInput = createInput("Write a poem about creative coding.");
statusP = createP("");
createButton("submit").mousePressed(async function () {
const results = await replicate(textInput.value());
let txt = results.output.join("");
txt = txt.replace("\n", "<br>");
createP(txt);
});
}
async function replicate(prompt) {
const response = await fetch(replicate_proxy + "/v1/predictions", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ version, input: { prompt } }),
});
const prediction = await response.json();
statusP.html(prediction.status);
let final;
// noprotect
do {
await sleep(1000);
const response = await fetch(
replicate_proxy + `/v1/predictions/${prediction.id}`
);
json = await response.json();
statusP.html(json.status);
console.log(json.status);
} while (json.status !== "succeeded" && json.status !== "failed");
return json;
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}