forked from replicate/scribble-diffusion
-
Notifications
You must be signed in to change notification settings - Fork 2
/
og.js
63 lines (56 loc) · 1.77 KB
/
og.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { ImageResponse } from "@vercel/og";
import { NextRequest } from "next/server";
import Image from "next/image";
export const config = {
runtime: "edge",
};
// This endpoint take a query parameter `id` which is the ID of a prediction
// and returns an Open Graph image for that prediction
export default async function handler(req) {
const { searchParams } = req.nextUrl;
const predictionId = searchParams.get("id");
let inputImageURL, outputImageURL;
// extract protocol and host from the request url, so we can call the local API
const url = new URL(req.url);
const protocol = url.protocol;
const host = url.host;
if (predictionId) {
const response = await fetch(
`${protocol}//${host}/api/predictions/${predictionId}`
);
const prediction = await response.json();
if (response.status === 200) {
inputImageURL = prediction.input.image;
outputImageURL = prediction.output?.[prediction.output.length - 1];
}
}
// Fallback to a default image
if (!inputImageURL) {
inputImageURL =
"https://upcdn.io/FW25b4F/raw/uploads/scribble-diffusion/1.0.0/2023-02-17/scribble_input_2JrhzKQH.png";
outputImageURL =
"https://replicate.delivery/pbxt/fX5WyhTJ2LQ7ZC4Pq1TLZaHfLdEciFyfmKxU7FN8WLZhhaeBB/output_1.png";
}
return new ImageResponse(
(
<div
style={{
display: "flex",
background: "#f6f6f6",
width: "100%",
height: "100%",
flexDirection: "row",
justifyContent: "center",
alignItems: "center",
}}
>
<img alt="img" width="630" height="630" src={inputImageURL} />
<img alt="img" width="630" height="630" src={outputImageURL} />
</div>
),
{
width: 1200,
height: 630,
}
);
}