Skip to content

Instantly share code, notes, and snippets.

@brianfoody
Created August 16, 2023 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianfoody/e724a65ae1d3773e778805b85a8306ee to your computer and use it in GitHub Desktop.
Save brianfoody/e724a65ae1d3773e778805b85a8306ee to your computer and use it in GitHub Desktop.
Replicate Blip
type Prediction = {
status: "starting" | "processing" | "succeeded" | "failed" | "canceled";
id: string;
output: string;
};
const captionAnImage = async () => {
const body = JSON.stringify({
version: "2e1dddc8621f72155f24cf2e0adbde548458d3cab9f00c0139eea840d0ac4746",
input: {
// i.e. `data:image/${image.extension};base64,${base64}`
image: req.body.image,
task: "image_captioning",
},
// You can use webhooks or you can poll
webhook: `${WEBHOOK_HOST}/api/replicate-webhook`,
webhook_events_filter: ["completed"],
});
const headers = {
Authorization: `Token ${process.env.REPLICATE_API_TOKEN}`,
"Content-Type": "application/json",
"User-Agent": `BedtimeStory`,
};
console.log("making prediuction at ", new Date().toISOString());
const response = await fetch(`https://api.replicate.com/v1/predictions`, {
method: "POST",
headers,
body,
});
if (!response.ok) {
let error = await response.json();
throw new Error(error.detail);
}
const prediction = await response.json();
return prediction;
};
// id from the prediction
const pollForSuccess = async (id: string) => {
let prediction: Prediction | undefined;
do {
const response = await fetch(
`https://api.replicate.com/v1/predictions/${req.query.id}`,
{
headers: {
Authorization: `Token ${process.env.REPLICATE_API_TOKEN}`,
"Content-Type": "application/json",
"User-Agent": `BedtimeStory`,
},
}
);
if (!response.ok) {
let error = await response.json();
throw new Error(error.detail);
}
prediction = await response.json();
} while (
prediction?.status === "starting" ||
prediction?.status === "processing"
);
if (prediction?.status !== "succeeded") {
throw new Error("Prediction failed");
}
let caption = prediction.output;
return {
status: prediction.status,
id: prediction.id,
caption: caption,
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment