-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathgeoimage.mts
More file actions
45 lines (37 loc) · 1.32 KB
/
geoimage.mts
File metadata and controls
45 lines (37 loc) · 1.32 KB
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
import { Config } from "@netlify/functions";
import OpenAI from "openai";
export default async (req: Request) => {
const url = new URL(req.url);
const prompt = url.searchParams.get("url");
if (!prompt) {
return new Response("Missing 'url' query parameter", { status: 400 });
}
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
try {
const response = await openai.images.generate({
model: "dall-e-3",
prompt: `Create a single image from a GeoCities website. Not an image of the website, but an image that would be used on a GeoCities website. Guess what the image should be from the URL, ${prompt}`,
n: 1,
size: "1024x1024",
response_format: "b64_json",
});
if (!response.data || !response.data[0] || !response.data[0].b64_json) {
return new Response("Error generating image", { status: 500 });
}
const image = Buffer.from(response.data[0].b64_json, "base64");
return new Response(image, {
headers: {
"Content-Type": "image/png",
"Netlify-CDN-Cache-Control": "public, max-age=31536000, durable",
},
});
} catch (error) {
console.error("Error generating image:", error);
return new Response("Error generating image", { status: 500 });
}
};
export const config: Config = {
path: "/geoimages",
};