-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
182 lines (172 loc) Β· 5.45 KB
/
index.html
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
173
174
175
176
177
178
179
180
181
182
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
/>
<link rel="stylesheet" type="text/css" href="/styles.css" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/default.min.css"
/>
<script
defer
src="https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js"
></script>
<link href="/assets/github-dark.css" rel="stylesheet" />
<script
defer
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js"
></script>
<script
defer
src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"
></script>
<script
defer
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/purify.min.js"
></script>
<script defer src="/main.js"></script>
<link rel="icon" href="/assets/favicon.svg" type="image/svg+xml" />
<link rel="apple-touch-icon" href="/assets/apple-touch-icon.png" />
<link rel="manifest" href="/assets/manifest.json" />
<title>ChatAPI</title>
</head>
<body x-data="{ keyOAI: $store.chat.keyOAI, keyBFL: $store.chat.keyBFL }">
<header x-data="{ component: 'menu' }">
<div x-show="component === 'menu'" id="menu">
<h1>π ChatAPI</h1>
<button
@click="$store.chat.clear()"
id="clear"
title="Clear chat history"
>
π₯
</button>
<button @click="component = 'keys'" title="Update OAI API key">
π
</button>
</div>
<div x-show="component === 'keys'" id="keys">
<form id="keyOAI" class="key">
<input
type="password"
id="apiKeyOAI"
name="apiKeyOAI"
placeholder="OpenAI API Key"
x-model.lazy="keyOAI"
/>
<button
@click="component = 'menu'"
type="submit"
title="Save API key"
>
π
</button>
</form>
<form id="keyBFL" class="key">
<input
type="password"
id="apiKeyBFL"
name="apiKeyBFL"
placeholder="Black Forest Labs API Key"
x-model.lazy="keyBFL"
/>
<button
@click="component = 'menu'"
type="submit"
title="Save API key"
>
π
</button>
</form>
</div>
</header>
<main id="chat" x-data="{history: $store.chat.history}">
<div id="usage" x-show="history.length === 0">
<p>Prefix your prompt with '!img' to generate images.</p>
<p>
Find the project on
<a target="_blank" href="https://github.com/david-haerer/chatapi">
GitHub</a
>.
</p>
</div>
<template x-for="message in history">
<div class="message">
<template x-if="message.role === 'user'">
<div :class="message.role" x-text="message.content"></div>
</template>
<template x-if="message.role === 'assistant'">
<div :class="message.role" x-html="message.html"></div>
</template>
</div>
</template>
</main>
<footer>
<form id="submit" action="/" method="get">
<input
type="text"
id="prompt"
name="prompt"
placeholder="Promptβ¦"
autocomplete="off"
required
/>
<button type="submit" title="Submit prompt">π</button>
</form>
</footer>
<script>
document.addEventListener("alpine:init", () => {
Alpine.store("chat", {
history: [],
languages: [undefined],
clear() {
this.history.length = 0;
},
add(role, content) {
const N = this.history.length - 1;
if (this.history.length && this.history[N].role === role) {
this.history[N].content += content;
str = this.history[N].content;
this.history[N].html = DOMPurify.sanitize(
marked.parse(this.history[N].content),
);
} else {
this.history.push({
role: role,
content: content,
html: DOMPurify.sanitize(marked.parse(content)),
});
}
const parser = new DOMParser();
const html = parser.parseFromString(
this.history[this.history.length - 1].html,
"text/html",
);
const code = html.querySelectorAll("pre code");
if (!code.length) return;
code.forEach((el) => {
const language = el.className.split("language-")[1];
if (this.languages.includes(language)) return;
const script = document.createElement("script");
script.src = `https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/languages/${language}.min.js`;
document.head.appendChild(script);
this.languages.push(language);
});
},
messages() {
return this.history.map((message) => {
return {
role: message.role,
content: message.content,
};
});
},
});
});
</script>
</body>
</html>