This commit is contained in:
ai
2026-05-18 21:50:41 +08:00
commit 19347e0d80
8 changed files with 610 additions and 0 deletions
+265
View File
@@ -0,0 +1,265 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>AI Image Playground</title>
<style>
:root {
color-scheme: light dark;
--bg: #0b0d10;
--panel: #14171c;
--border: #262a31;
--text: #e6e6e6;
--muted: #9aa3af;
--accent: #6366f1;
--accent-hover: #4f46e5;
--danger: #ef4444;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family:
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
background: var(--bg);
color: var(--text);
min-height: 100vh;
}
.container {
max-width: 960px;
margin: 0 auto;
padding: 32px 20px 64px;
}
h1 {
margin: 0 0 8px;
font-size: 24px;
}
p.sub {
margin: 0 0 24px;
color: var(--muted);
font-size: 14px;
}
.panel {
background: var(--panel);
border: 1px solid var(--border);
border-radius: 12px;
padding: 20px;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
label {
display: block;
font-size: 12px;
color: var(--muted);
margin-bottom: 6px;
}
input,
textarea,
select {
width: 100%;
background: #0b0d10;
color: var(--text);
border: 1px solid var(--border);
border-radius: 8px;
padding: 10px 12px;
font-size: 14px;
font-family: inherit;
}
input:focus,
textarea:focus,
select:focus {
outline: 2px solid var(--accent);
outline-offset: -1px;
}
textarea {
min-height: 96px;
resize: vertical;
}
.row {
margin-bottom: 12px;
}
.actions {
display: flex;
gap: 12px;
align-items: center;
margin-top: 16px;
}
button {
background: var(--accent);
color: white;
border: 0;
padding: 10px 16px;
border-radius: 8px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
}
button:hover {
background: var(--accent-hover);
}
button:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.status {
font-size: 13px;
color: var(--muted);
}
.status.error {
color: var(--danger);
}
.result {
margin-top: 24px;
display: grid;
gap: 16px;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}
.result img {
width: 100%;
border-radius: 12px;
border: 1px solid var(--border);
display: block;
}
details {
margin-top: 12px;
font-size: 13px;
color: var(--muted);
}
summary {
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h1>AI Image Playground</h1>
<p class="sub">
Generate images via any OpenAI-compatible endpoint using the Vercel AI SDK.
</p>
<div class="panel">
<div class="grid">
<div class="row">
<label for="baseURL">Base URL</label>
<input
id="baseURL"
type="text"
placeholder="https://api.openai.com/v1"
/>
</div>
<div class="row">
<label for="apiKey">API Key</label>
<input id="apiKey" type="password" placeholder="sk-..." />
</div>
<div class="row">
<label for="model">Model</label>
<input id="model" type="text" placeholder="dall-e-3 / gpt-image-1 / flux-..." />
</div>
<div class="row">
<label for="size">Size</label>
<select id="size">
<option value="1024x1024">1024x1024</option>
<option value="512x512">512x512</option>
<option value="768x768">768x768</option>
<option value="1024x1792">1024x1792 (portrait)</option>
<option value="1792x1024">1792x1024 (landscape)</option>
</select>
</div>
</div>
<div class="row">
<label for="prompt">Prompt</label>
<textarea
id="prompt"
placeholder="A futuristic cityscape at sunset, cinematic lighting"
></textarea>
</div>
<div class="actions">
<button id="generate">Generate</button>
<span id="status" class="status"></span>
</div>
<details>
<summary>Settings are stored in your browser's localStorage</summary>
Base URL, API Key, model and size are saved locally. They are sent to
the local Bun server only when you click Generate.
</details>
</div>
<div id="result" class="result"></div>
</div>
<script>
const $ = (id) => document.getElementById(id);
const fields = ["baseURL", "apiKey", "model", "size", "prompt"];
for (const f of fields) {
const saved = localStorage.getItem("aip:" + f);
if (saved) $(f).value = saved;
$(f).addEventListener("input", () => {
localStorage.setItem("aip:" + f, $(f).value);
});
$(f).addEventListener("change", () => {
localStorage.setItem("aip:" + f, $(f).value);
});
}
$("generate").addEventListener("click", async () => {
const btn = $("generate");
const status = $("status");
const result = $("result");
const body = {
baseURL: $("baseURL").value.trim(),
apiKey: $("apiKey").value.trim(),
model: $("model").value.trim(),
size: $("size").value,
prompt: $("prompt").value.trim(),
};
if (!body.baseURL || !body.apiKey || !body.model || !body.prompt) {
status.textContent = "Please fill in Base URL, API Key, Model and Prompt.";
status.className = "status error";
return;
}
btn.disabled = true;
status.className = "status";
status.textContent = "Generating...";
result.innerHTML = "";
try {
const res = await fetch("/api/generate", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error || ("HTTP " + res.status));
if (!data.images?.length) {
status.textContent = "No images returned.";
status.className = "status error";
return;
}
for (const src of data.images) {
const img = document.createElement("img");
img.src = src;
img.alt = body.prompt;
result.appendChild(img);
}
status.textContent = "Done.";
} catch (err) {
status.textContent = "Error: " + (err.message || String(err));
status.className = "status error";
} finally {
btn.disabled = false;
}
});
</script>
</body>
</html>