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
168 changes: 168 additions & 0 deletions package-lock.json

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

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

const replicate = new Replicate({
auth: process.env.REPLICATE_API_TOKEN,
userAgent: `${packageData.name}/${packageData.version}`,
});

export default async function handler(req, res) {
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();
const prediction = await replicate.predictions.get(req.query.id);

if (prediction?.error) {
res.statusCode = 500;
res.end(JSON.stringify({ detail: error.detail }));
res.end(JSON.stringify({ detail: prediction.error }));
return;
}

const prediction = await response.json();
res.end(JSON.stringify(prediction));
}
30 changes: 10 additions & 20 deletions pages/api/predictions/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const REPLICATE_API_HOST = "https://api.replicate.com";

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

const replicate = new Replicate({
auth: process.env.REPLICATE_API_TOKEN,
userAgent: `${packageData.name}/${packageData.version}`,
});

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

const body = JSON.stringify({
// https://replicate.com/jagilley/controlnet-scribble/versions
// https://replicate.com/jagilley/controlnet-scribble/versions
const prediction = await replicate.predictions.create({
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 (response.status !== 201) {
let error = await response.json();
if (prediction?.error) {
res.statusCode = 500;
res.end(JSON.stringify({ detail: error.detail }));
res.end(JSON.stringify({ detail: prediction.error }));
return;
}

const prediction = await response.json();
res.statusCode = 201;
res.end(JSON.stringify(prediction));
}
Expand Down