-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
300 lines (265 loc) · 7.87 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Recipe Generator</title>
<style>
html {
width: 100%;
height: 100%;
font-family: sans-serif;
font-size: large;
background-color: black;
color: white;
box-sizing: border-box;
padding: .5rem;
}
h1,
p {
margin: .25rem;
}
div {
margin: .25rem;
}
main {
display: flex;
flex-direction: column;
}
.container {
display: flex;
flex-direction: row;
align-items: center;
justify-content: right;
flex-wrap: wrap;
}
.newline {
display: block;
margin-bottom: .25rem;
}
.print-button,
.reset-button,
.submit-button {
cursor: pointer;
border-radius: 10px;
padding: .5rem;
font-size: large;
justify-content: center;
display: block;
color: white;
text-align: right;
background-color: blue;
width: 100%;
text-align: center;
}
input[type=button][disabled] {
background-color: #eee;
color: darkgray;
cursor: not-allowed;
}
.grow {
flex-grow: 1;
}
pre {
font-family: "Gill Sans", sans-serif;
}
.output {
width: 50%;
text-wrap: wrap;
}
@media (max-width: 767px) {
.output {
width: 100%;
}
}
textarea {
padding: .5rem;
display: block;
}
.category {
display: flex;
flex-direction: row;
align-items: center;
flex-wrap: wrap;
flex-grow: 1;
}
.image {
object-fit: cover;
height: 100%;
}
@media print {
.non-printableArea {
display: none;
}
}
a:link {
color: lightblue;
text-decoration: none;
}
a:visited {
color: lightblue;
;
}
a:hover {
color: blue;
background-color: white;
}
a:active {
color: lightblue;
;
}
</style>
</head>
<body>
<main>
<h1>Recipe Generator</h1>
<h5>Cloudflare Challenge April 2024 <a href="https://github.com/rebeccapeltz/recipe-generator"
target="_blank">Code</a></h5>
<p class="non-printableArea">
Choose a category and then describe the details of a recipe you want.
</p>
<section class="container non-printableArea">
<div class="grow">
<fieldset>
<legend>Select a recipe category:</legend>
<div class="category">
<div>
<input type="radio" value="cookies" name="recipes" />
<label for="cookies">Cookies</label>
</div>
<div>
<input type="radio" value="cake" name="recipes" />
<label for="cake">Cake</label>
</div>
<div>
<input type="radio" value="salad" name="recipes" />
<label for="cookies">Salad </label>
</div>
<div>
<input type="radio" value="soup" name="recipes" />
<label for="cookies">Soup</label>
</div>
<div>
<input type="radio" value="chicken" name="recipes" />
<label for="cookies">Chicken</label>
</div>
<div>
<input type="radio" value="tacos" name="recipes" />
<label for="cake">Tacos</label>
</div>
</div>
</fieldset>
</div>
<div class="grow">
<label class="newline" for="details">Recipe Detail: </label>
<textarea id="details" name="details" rows="5" cols="45"></textarea>
</div>
<div class="grow" style="display: flex; flex-direction: column">
<div class="grow">
<input id="submit-button" class="submit-button" type="button" value="Generate a recipe" />
</div>
<div class="grow">
<input id="print-button" class="noprint print-button" type="button" value="Print a recipe" />
</div>
<div class="grow">
<input id="reset-button" class="reset-button" type="button" value="Try another recipe" />
</div>
</div>
</section>
<section id="recipe-container" class="container">
<pre class="grow output" id="recipe-content"></pre>
<img id="gen-image" class="image grow output non-printableArea"
src="https://placehold.co/600x400?text=Generating+Image" alt="Generated Image" />
</section>
</main>
<script>
function hideOutput() {
document.querySelector("#recipe-container").style.display = "none";
}
function showOutput() {
document.querySelector("#recipe-container").style.display = "flex";
}
function disableButton(id) {
document.getElementById(id).disabled = true;
}
function enableButton(id) {
document.getElementById(id).disabled = false;
}
document.addEventListener("DOMContentLoaded", (e) => {
document
.querySelector("#reset-button")
.addEventListener("click", (e) => {
location.reload();
});
document
.querySelector("#print-button")
.addEventListener("click", (e) => {
window.print();
});
hideOutput();
enableButton("submit-button");
disableButton("print-button");
disableButton("reset-button");
const requestData = {};
document
.querySelector("#submit-button")
.addEventListener("click", (e) => {
const categorySelected = document.querySelector(
'input[name="recipes"]:checked'
);
const detailsInput = document.querySelector("#details").value;
if (!(categorySelected && detailsInput.length > 0)) {
alert("Choose a category and enter detail about the recipe");
} else {
requestData.category = categorySelected.value;
requestData.details = detailsInput;
console.log(requestData);
showOutput();
disableButton("submit-button");
enableButton("reset-button");
// format chat AI prompt
// recipe?prompt=Create+a+recipe+for+cake+described+as+chocolate`
// Chat to get Recipe
const chatPrompt = `${requestData.category} described as ${requestData.details}`;
console.log("chat prompt:", chatPrompt);
const recipeEventSource = new EventSource(
"/recipe?prompt=" + encodeURIComponent(chatPrompt)
);
console.log("recipeEventSource prompt:", recipeEventSource);
let recipeContent = "";
recipeEventSource.onmessage = function (event) {
if (event.data === "[DONE]") {
recipeEventSource.close();
enableButton("print-button");
return;
}
// Add placehoder image while the actual image is being generated
const data = JSON.parse(event.data);
if (data && data.response) {
document.querySelector("#recipe-content").textContent +=
data.response;
recipeContent += data.response;
}
};
// Generate Image
const imageGenPrompt = `${requestData.category} described as ${requestData.details}`;
fetch("/generate_image", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ imageGenPrompt }),
})
.then((response) => response.blob())
.then((blob) => {
const imageUrl = URL.createObjectURL(blob);
const imageElement = document.querySelector("#gen-image");
imageElement.src = imageUrl;
imageElement.style.display = "block";
});
}
});
});
</script>
</body>
</html>