Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions lib/db.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import prisma from "./prisma";

function enabled() {
return process.env.DATABASE_URL ? true : false;
}

export async function upsertPrediction(predictionData) {
console.log("🤔 upsert prediction? ", predictionData.id);

if (!enabled()) {
console.log("skiping upsert, database not enabled");
return;
}

if (predictionData?.status !== "succeeded") {
console.log("🙈 skiping incomplete or unsuccesful prediction");
return;
Expand Down
110 changes: 0 additions & 110 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"react-dom": "18.2.0",
"react-sketch-canvas": "^6.2.0",
"react-spinners": "^0.13.8",
"replicate": "github:replicate/replicate-js",
"upload-js-full": "^1.22.0"
},
"devDependencies": {
Expand All @@ -42,4 +41,4 @@
"tailwindcss": "^3.1.8",
"tailwindcss-animate": "^1.0.5"
}
}
}
16 changes: 11 additions & 5 deletions pages/api/predictions/[id].js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import replicate from "replicate";
const API_HOST = process.env.REPLICATE_API_HOST || "https://api.replicate.com";

export default async function handler(req, res) {
const prediction = await replicate.prediction(req.query.id).load();

if (prediction.error) {
const response = await fetch(`${API_HOST}/v1/predictions/${req.query.id}`, {
headers: {
Authorization: `Token ${process.env.REPLICATE_API_TOKEN}`,
"Content-Type": "application/json",
},
});
if (response.status !== 200) {
let error = await response.json();
res.statusCode = 500;
res.end(JSON.stringify({ detail: prediction.error }));
res.end(JSON.stringify({ detail: error.detail }));
return;
}

const prediction = await response.json();
res.end(JSON.stringify(prediction));
}
41 changes: 26 additions & 15 deletions pages/api/predictions/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import replicate from "replicate";
const REPLICATE_API_HOST = "https://api.replicate.com";

import packageData from "../../../package.json";

const WEBHOOK_HOST = process.env.VERCEL_URL
? `https://${process.env.VERCEL_URL}`
Expand All @@ -11,25 +13,34 @@ export default async function handler(req, res) {
);
}

const input = req.body;
const prediction = await replicate
.model(
"jagilley/controlnet-scribble:435061a1b5a4c1e26740464bf786efdfa9cb3a3ac488595a2de23e143fdb0117"
)
.createPrediction(
{ input },
{
webhook: `${WEBHOOK_HOST}/api/replicate-webhook`,
webhookEventsFilter: ["completed"],
}
);
const body = JSON.stringify({
// https://replicate.com/jagilley/controlnet-scribble/versions
version: "435061a1b5a4c1e26740464bf786efdfa9cb3a3ac488595a2de23e143fdb0117",
input: req.body,
webhook: `${WEBHOOK_HOST}/api/replicate-webhook`,
webhook_events_filter: ["start", "completed"],
});

const headers = {
Authorization: `Token ${process.env.REPLICATE_API_TOKEN}`,
"Content-Type": "application/json",
"User-Agent": `${packageData.name}/${packageData.version}`,
};

const response = await fetch(`${REPLICATE_API_HOST}/v1/predictions`, {
method: "POST",
headers,
body,
});

if (prediction.error) {
if (response.status !== 201) {
let error = await response.json();
res.statusCode = 500;
res.end(JSON.stringify({ detail: prediction.error }));
res.end(JSON.stringify({ detail: error.detail }));
return;
}

const prediction = await response.json();
res.statusCode = 201;
res.end(JSON.stringify(prediction));
}
Expand Down
2 changes: 1 addition & 1 deletion pages/api/replicate-webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { upsertPrediction } from "../../lib/db";

export default async function handler(req, res) {
console.log("🪝 received webhook for prediction: ", req.body.id);
console.log("received webhook for prediction: ", req.body.id);
await upsertPrediction(req.body);

res.end();
Expand Down