Docs
AI Replicate
AI Replicate
Let's take a look at how to use ai Replicate.
Deploying to Replicate is easy, but a bit more involved than Runpod.
Check out the AI Services tutorial to learn how to deploy your custom AI models on Replicate.
After deploying your model get your model version, its input type and modify api/ai/replicate/route.ts to make requests to your model inside your BuouAI project.
Since Replicate API is async, meaning the response is not instantaneous, we need to wait and retry if necessary. Don't worry,We handle all the waiting/retrying logic for you.
let data = JSON.stringify({
// MODELS INPUT AS A JSON OBJECT.
version: "#YOUR_DEPLOYED_MODEL_VERSION", // YOUR DEPLOYED MODEL VERSION,
input: {
image: base64,// Modify the input type to match the request body of your API, exact types depend on your model
prompt: requestBody.prompt,
seed: requestBody.seed,
style: requestBody.style,
},
});
let config = {
method: "post",
maxBodyLength: Infinity,
url: "https://api.replicate.com/v1/predictions",
headers: {
Authorization: `Bearer ${process.env.REPLICATE_API_TOKEN}`, // place in your API key here or use environment variables
"Content-Type": "application/json",
},
data: data,
};
// ...
// Since replicate api is async, we need to wait and retry if necessary.
try {
axiosRetry(axios, {
retryDelay: (retryCount) => {
return retryCount * 1;
},
retries: 15,
});
let result = await axios.request(config);
if (result.status === 200) {
if (result.data.status === "failed") {
return NextResponse.json(result.data, { status: 500 });
}
let waitCount = 0;
let predId = result.data.id;
while (
result.data.status == "starting" ||
result.data.status == "processing"
) {
if (waitCount > 20) {
// at least 20 seconds
return NextResponse.json(result.data, { status: 500 }); // max wait, timeout
}
await new Promise((resolve) => setTimeout(resolve, 1000)); // wait 1 second
result = await axios.get(`https://api.replicate.com/v1/predictions/${predId}`, {
{
headers: {
Authorization: `Bearer ${process.env.REPLICATE_API_TOKEN}`,
"Content-Type": "application/json",
},
}
);
waitCount++;
if (result.data.status === "failed") {
return NextResponse.json(result.data, { status: 500 });
}
if (result.data.status === "succeeded") {
return NextResponse.json(result.data, { status: 200 });
}
}
} else {
return NextResponse.json(result.data, { status: result.status });
}
} catch (error) {
return NextResponse.json(error.response.data, {
status: error.response.status,
});
}
}