-
Notifications
You must be signed in to change notification settings - Fork 118
/
route.js
46 lines (36 loc) · 1.44 KB
/
route.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { NextResponse } from 'next/server';
import Replicate from 'replicate';
const replicate = new Replicate({
auth: process.env.REPLICATE_API_TOKEN,
});
// Prevent Next.js / Vercel from caching responses
// See https://github.com/replicate/replicate-javascript/issues/136#issuecomment-1728053102
replicate.fetch = (url, options) => {
return fetch(url, { ...options, cache: "no-store" });
};
// In production and preview deployments (on Vercel), the VERCEL_URL environment variable is set.
// In development (on your local machine), the NGROK_HOST environment variable is set.
const WEBHOOK_HOST = process.env.VERCEL_URL
? `https://${process.env.VERCEL_URL}`
: process.env.NGROK_HOST;
export async function POST(request) {
if (!process.env.REPLICATE_API_TOKEN) {
throw new Error(
'The REPLICATE_API_TOKEN environment variable is not set. See README.md for instructions on how to set it.'
);
}
const { prompt } = await request.json();
const options = {
version: '8beff3369e81422112d93b89ca01426147de542cd4684c244b673b105188fe5f',
input: { prompt }
}
if (WEBHOOK_HOST) {
options.webhook = `${WEBHOOK_HOST}/api/webhooks`
options.webhook_events_filter = ["start", "completed"]
}
const prediction = await replicate.predictions.create(options);
if (prediction?.error) {
return NextResponse.json({ detail: prediction.error }, { status: 500 });
}
return NextResponse.json(prediction, { status: 201 });
}