-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathscript.js
125 lines (108 loc) · 4.41 KB
/
script.js
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
//script.js
document.addEventListener("DOMContentLoaded", function() {
const form = document.querySelector("form");
const downloadLink = document.createElement('a'); // Create the download link element
document.body.appendChild(downloadLink); // Append the download link element to the document body
downloadLink.style.display = 'none'; // Initially hide it
form.addEventListener("submit", function(event) {
const prompt = document.getElementById("prompt");
const numAsks = document.getElementById("numAsks");
const output = document.getElementById("output");
// Check if the prompt is empty
if (prompt.value.trim() === '') {
event.preventDefault();
alert('Please enter a prompt.');
return;
}
// Check if numAsks is a valid number
if (isNaN(numAsks.value) || numAsks.value <= 0) {
event.preventDefault();
alert('Please enter a valid number of asks.');
return;
}
event.preventDefault(); // Prevent the default form submission.
// Make the request
makeRequestToServer(prompt.value, numAsks.value)
.then(data => {
// append gptOutput and finalAnswer to output container
output.innerText += 'GPT Output: ' + data.gptOutput + '\n';
output.innerText += 'Final Answer: ' + data.finalAnswer + '\n';
// Update the download link
const downloadLink = document.querySelector(".output-link a");
downloadLink.href = '/download/' + data.fileName;
downloadLink.download = data.fileName;
})
.catch(err => {
// handle error
output.innerText += 'Error: ' + err.message + '\n';
});
});
});
function makeRequestToServer(prompt, numAsks) {
const data = { prompt, numAsks };
return fetch('/run', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
if (data.error) {
return Promise.reject(new Error(data.error));
}
// Return gptOutput, finalAnswer, and fileName
return { gptOutput: data.gptOutput, finalAnswer: data.finalAnswer, fileName: data.fileName };
});
}
// document.addEventListener("DOMContentLoaded", function() {
// const form = document.querySelector("form");
// form.addEventListener("submit", function(event) {
// const prompt = document.getElementById("prompt");
// const numAsks = document.getElementById("numAsks");
// const output = document.getElementById("output");
// // Check if the prompt is empty
// if (prompt.value.trim() === '') {
// event.preventDefault();
// alert('Please enter a prompt.');
// return;
// }
// // Check if numAsks is a valid number
// if (isNaN(numAsks.value) || numAsks.value <= 0) {
// event.preventDefault();
// alert('Please enter a valid number of asks.');
// return;
// }
// event.preventDefault(); // Add this line to prevent the default form submission.
// // Make the request
// makeRequestToServer(prompt.value, numAsks.value)
// .then(data => {
// // append gptOutput and finalAnswer to output container
// output.innerText += 'GPT Output: ' + data.gptOutput + '\n';
// output.innerText += 'Final Answer: ' + data.finalAnswer + '\n';
// })
// .catch(err => {
// // handle error
// output.innerText += 'Error: ' + err.message + '\n';
// });
// });
// });
// function makeRequestToServer(prompt, numAsks) {
// const data = { prompt, numAsks };
// return fetch('/run', {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json'
// },
// body: JSON.stringify(data)
// })
// .then(response => response.json())
// .then(data => {
// if (data.error) {
// return Promise.reject(new Error(data.error));
// }
// // Return both gptOutput and finalAnswer
// return { gptOutput: data.gptOutput, finalAnswer: data.finalAnswer };
// });
// }