-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
61 lines (57 loc) · 1.97 KB
/
index.html
File metadata and controls
61 lines (57 loc) · 1.97 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Haiku Server</title>
</head>
<body>
<h1>Test Server</h1>
<button></button>
</body>
<script>
(function () {
const btn = document.querySelector("button");
btn.textContent = "Generate Haiku";
const out = document.createElement("pre");
out.style.whiteSpace = "pre-wrap";
out.style.fontFamily =
'ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial';
out.style.marginTop = "1rem";
document.body.insertBefore(out, btn.nextSibling);
async function fetchHaiku() {
out.textContent = "Loading…";
try {
const res = await fetch("http://127.0.0.1:8000/generate-haiku", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ prompt: "Write a haiku about nature" }),
});
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
const ct = res.headers.get("content-type") || "";
let text;
if (ct.includes("application/json")) {
const data = await res.json();
if (Array.isArray(data)) {
text = data.join("\n");
} else if (Array.isArray(data.haiku)) {
text = data.haiku.join("\n");
} else if (Array.isArray(data.lines)) {
text = data.lines.join("\n");
} else if (typeof data.haiku === "string") {
text = data.haiku;
} else {
text = JSON.stringify(data, null, 2);
}
} else {
text = await res.text();
}
out.textContent = text;
} catch (err) {
out.textContent = "Error: " + (err.message || err);
}
}
btn.addEventListener("click", fetchHaiku);
})();
</script>
</html>