-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
172 lines (147 loc) · 4.52 KB
/
Copy pathserver.js
File metadata and controls
172 lines (147 loc) · 4.52 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import "dotenv/config";
import express from "express";
import OpenAI from "openai";
import { z } from "zod";
import { zodTextFormat } from "openai/helpers/zod";
import path from "path";
import { fileURLToPath } from "url";
const app = express();
const port = process.env.PORT || 3000;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const publicDir = path.join(__dirname, "public");
const MemeResponseSchema = z.object({
summaryTitle: z.string(),
summaryPills: z.array(z.string()).length(3),
variants: z.array(
z.object({
tone: z.string(),
format: z.string(),
imageLabel: z.string(),
imageHeadline: z.string(),
visualScene: z.string(),
line: z.string(),
caption: z.string(),
imagePrompt: z.string()
})
).length(3)
});
const systemPrompt = [
"You turn everyday words and expressions into short, witty meme writeups.",
"Write for internet-native audiences and keep each output highly readable and shareable.",
"Make the humor sharp, but do not use slurs, hate, or abusive stereotypes.",
"Also create an image prompt for each meme variant.",
"The image prompt should describe a bold, expressive, poster-like meme image with no embedded text."
].join(" ");
let client;
function getClient() {
if (!process.env.OPENAI_API_KEY) {
return null;
}
if (!client) {
client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
}
return client;
}
app.use(express.json({ limit: "2mb" }));
app.use(express.static(publicDir));
app.get("/", (_req, res) => {
res.sendFile(path.join(publicDir, "index.html"));
});
app.post("/api/generate", async (req, res) => {
const openai = getClient();
const expression = String(req.body?.expression || "").trim();
const mood = String(req.body?.mood || "auto").trim();
const audience = String(req.body?.audience || "everyone").trim();
if (!openai) {
return res.status(500).json({
error: "Missing OPENAI_API_KEY. Set it in your shell before starting Memeforge."
});
}
if (!expression) {
return res.status(400).json({ error: "Expression is required." });
}
try {
const memeResponse = await openai.responses.parse({
model: "gpt-5",
input: [
{
role: "system",
content: [{ type: "input_text", text: systemPrompt }]
},
{
role: "user",
content: [
{
type: "input_text",
text: [
`Expression: ${expression}`,
`Mood: ${mood}`,
`Audience: ${audience}`,
"Generate exactly 3 variants.",
"Make the image prompts visually distinct from one another."
].join("\n")
}
]
}
],
text: {
format: zodTextFormat(MemeResponseSchema, "meme_response")
}
});
const parsed = memeResponse.output_parsed;
if (!parsed) {
return res.status(502).json({ error: "The model returned no structured meme output." });
}
return res.json({
summaryTitle: parsed.summaryTitle,
summaryPills: parsed.summaryPills,
variants: parsed.variants
});
} catch (error) {
const message =
error instanceof Error ? error.message : "Something went wrong while generating the meme.";
return res.status(500).json({ error: message });
}
});
app.post("/api/generate-image", async (req, res) => {
const openai = getClient();
const imagePrompt = String(req.body?.imagePrompt || "").trim();
if (!openai) {
return res.status(500).json({
error: "Missing OPENAI_API_KEY. Set it in your shell before starting Memeforge."
});
}
if (!imagePrompt) {
return res.status(400).json({ error: "Image prompt is required." });
}
try {
const imageResponse = await openai.responses.create({
model: "gpt-5",
input: imagePrompt,
tools: [
{
type: "image_generation",
quality: "low",
size: "1024x1024"
}
]
});
const imageCall = imageResponse.output.find(
(item) => item.type === "image_generation_call"
);
return res.json({
imageBase64: imageCall?.result || null
});
} catch (error) {
const message =
error instanceof Error ? error.message : "Something went wrong while generating the image.";
return res.status(500).json({ error: message });
}
});
if (process.env.VERCEL !== "1") {
app.listen(port, () => {
console.log(`Memeforge server running at http://localhost:${port}`);
});
}
export default app;