-
Notifications
You must be signed in to change notification settings - Fork 2
/
scripts.js
448 lines (382 loc) · 14.2 KB
/
scripts.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
$(document).ready(function () {
loadUserData();
$('#save-api-key-btn').click(function (e) {
e.preventDefault();
testOpenAIKey().then(result => console.log(result));
saveUserData();
});
$('#style-changer-tab').click(function (e) {
e.preventDefault();
$('#style-changer').show();
$('#book-writer').hide();
$('#book-condenser').hide();
$('#api-key').hide();
$(this).addClass('active');
$('#book-writer-tab').removeClass('active');
$('#book-condenser-tab').removeClass('active');
});
$('#book-writer-tab').click(function (e) {
e.preventDefault();
$('#style-changer').hide();
$('#book-writer').show();
$('#book-condenser').hide();
$('#api-key').hide();
$(this).addClass('active');
$('#style-changer-tab').removeClass('active');
$('#book-condenser-tab').removeClass('active');
});
$('#book-condenser-tab').click(function (e) {
e.preventDefault();
$('#style-changer').hide();
$('#book-writer').hide();
$('#book-condenser').show();
$('#api-key').hide();
$(this).addClass('active');
$('#book-writer-tab').removeClass('active');
$('#style-changer-tab').removeClass('active');
});
$('#api-key-btn').click(function (e) {
e.preventDefault();
$('#style-changer').hide();
$('#book-writer').hide();
$('#book-condenser').hide();
$('#api-key').show();
$('#book-writer-tab').removeClass('active');
$('#style-changer-tab').removeClass('active');
$('#book-condenser-tab').removeClass('active');
});
$('#style-changer').hide();
$('#book-writer').hide();
$('#book-condenser').hide();
$('#api-key').show();
$('#addInputBtn').click(function () {
addInputField();
updateLevels();
});
$('#style-changer-form').on('submit', function (e) {
e.preventDefault();
// Clear previous error messages
$('.error').text('');
// Validation
let isValid = true;
if ($('#fileUpload').val() === '') {
$('#fileError').text('Please upload a file.');
isValid = false;
}
if ($('#title').val().trim() === '') {
$('#titleError').text('Please enter a title.');
isValid = false;
}
if ($('#author').val().trim() === '') {
$('#authorError').text('Please enter an author.');
isValid = false;
}
if ($('#prompt').val().trim() === '') {
$('#promptError').text('Please enter a prompt.');
isValid = false;
}
if (!isValid) {
return; // Stop the function if validation fails
}
prefix = 'style-changer'
// Show the loading bar
$('#' + prefix + '-loading-bar-container').show();
$('#' + prefix + '-loading-bar').css('width', '0%');
$('#' + prefix + '-loading-percent').text('0%'); // Reset the text
var title = $('#title').val().trim();
var formData = new FormData(this);
var fileInput = document.getElementById('fileUpload');
formData.append("file", fileInput.files[0]);
formData.append("api_key", $("#api-key-input").val());
$.ajax({
type: 'POST',
url: '/style-changer', // Your Flask endpoint
data: formData,
contentType: false,
processData: false,
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
// Update loading bar width
$('#' + prefix + '-loading-bar').css('width', percentComplete * 100 + '%');
}
}, false);
return xhr;
},
success: function (response) {
console.log("Data submitted successfully:", response);
},
error: function (xhr, status, error) {
// Handle any errors here
console.error("Error: " + error);
$('#style-changer-response').html("An error occurred: " + error);
},
complete: function () {
// Hide the loading bar when the request is complete
updateLoadingBar(title, prefix);
}
});
});
$('#book-writer-form').on('submit', function (e) {
e.preventDefault();
// Clear previous error messages
$('.error').text('');
// Validation
let isValid = true;
if ($('#book-writer-title').val().trim() === '') {
$('#book-writer-title-Error').text('Please enter a title.');
isValid = false;
}
if (!isValid) {
return; // Stop the function if validation fails
}
prefix = 'book-writer'
// Show the loading bar
$('#' + prefix + '-loading-bar-container').show();
$('#' + prefix + '-loading-bar').css('width', '0%');
$('#' + prefix + '-loading-percent').text('0%'); // Reset the text
let formData = gatherFormData();
formData['title'] = $('#book-writer-title').val().trim();
formData["api_key"] = $("#api-key-input").val();
$.ajax({
type: "POST",
url: "/book-writer",
contentType: "application/json",
data: JSON.stringify(formData),
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
// Update loading bar width
$('#' + prefix + '-loading-bar').css('width', percentComplete * 100 + '%');
}
}, false);
return xhr;
},
success: function (response) {
console.log("Data submitted successfully:", response);
},
error: function (xhr, status, error) {
console.error("Error in data submission:", xhr.responseText);
},
complete: function () {
// Hide the loading bar when the request is complete
updateLoadingBar(formData.title, prefix);
}
});
});
// Event listener for when the selection changes
$('#book-writer-prompt-type').change(function() {
// Get the selected value
var selectedOption = $(this).val();
// Hide both tabs initially
$('#book-writer-outline-tab').hide();
$('#book-writer-summary-tab').hide();
// Show the relevant tab based on the selected option
if (selectedOption === 'Outline') {
$('#book-writer-outline-tab').show();
} else if (selectedOption === 'Summary') {
$('#book-writer-summary-tab').show();
}
});
// Trigger the change event on page load to ensure the correct tab is shown
$('#book-writer-prompt-type').trigger('change');
});
function addInputField() {
let newInput = createDepthControlInput();
$('#inputContainer').append(newInput);
}
function createDepthControlInput() {
let inputGroup = $('<div>', {
'class': 'input-group mb-2',
'data-depth': '1',
css: { 'padding-left': '20px' }
});
let levelIndicator = $('<div>', {
'class': 'level-indicator',
css: { 'margin-right': '10px' }
});
let indentBtn = $('<button>', {
type: 'button',
'class': 'btn btn-outline-secondary',
text: '>',
click: function () {
adjustDepth(inputGroup, true);
}
});
let outdentBtn = $('<button>', {
type: 'button',
'class': 'btn btn-outline-secondary',
text: '<',
click: function () {
adjustDepth(inputGroup, false);
}
});
let deleteBtn = $('<button>', {
type: 'button',
'class': 'btn btn-outline-danger',
text: 'X',
click: function () { deleteInputField(inputGroup); } // Corrected event handler
});
let input = $('<input>', {
type: 'text',
'class': 'form-control',
placeholder: 'Enter detail'
});
inputGroup.append(levelIndicator, outdentBtn, indentBtn, input, deleteBtn);
return inputGroup;
}
function adjustDepth(element, isIndent) {
let currentDepth = parseInt(element.attr('data-depth'));
currentDepth += isIndent ? 1 : -1;
currentDepth = Math.max(1, currentDepth); // Ensure depth is not negative
element.attr('data-depth', currentDepth.toString());
// Adjust padding based on depth
let padding = (20 * currentDepth);
element.css('padding-left', `${padding}px`);
updateLevels();
}
function updateLevels() {
let levelNumbers = [0]; // Initialize level numbers
$('#inputContainer').children('.input-group').each(function () {
let depth = parseInt($(this).attr('data-depth'));
while (levelNumbers.length - 1 > depth) {
levelNumbers.pop(); // Remove deeper levels
}
if (levelNumbers.length - 1 < depth) {
levelNumbers.push(1); // Start a new sub-level
} else {
levelNumbers[depth]++; // Increment the current level
}
let levelString = levelNumbers.slice(1).join('.');
$(this).find('.level-indicator').text(levelString);
});
}
function deleteInputField(element) {
element.remove();
updateLevels();
}
function gatherFormData() {
let selectedPromptType = $('#book-writer-prompt-type').val();
let formData = {};
if (selectedPromptType === 'Outline') {
let inputData = [];
$('#inputContainer').find('.input-group').each(function () {
let level = $(this).find('.level-indicator').text();
let value = $(this).find('input[type="text"]').val();
inputData.push({ value: value, level: level });
});
formData['outline'] = inputData;
} else if (selectedPromptType === 'Summary') {
formData['summary'] = $('#summaryTextarea').val().trim();
}
return formData;
}
function setLocalStorageItem(value) {
localStorage.setItem('key54-32579032', value);
}
function getLocalStorageItem() {
return localStorage.getItem('key54-32579032');
}
function saveUserData() {
var userInput = $("#api-key-input").val();
setLocalStorageItem(userInput);
}
function loadUserData() {
var userData = getLocalStorageItem();
if (userData) {
$("#api-key-input").val(userData);
}
}
async function testOpenAIKey() {
var apiKey = $("#api-key-input").val();
const url = 'https://api.openai.com/v1/chat/completions'; // Example endpoint
const headers = {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
};
const body = JSON.stringify({
model: "gpt-4o-mini",
messages: [
{
role: "system",
content: "You are a helpful assistant."
},
{
role: "user",
content: "Hello!"
}
]
});
try {
const response = await fetch(url, { method: 'POST', headers: headers, body: body });
const data = await response.json();
if (response.ok) {
return { valid: true, message: 'API key is valid.', data: data };
} else {
return { valid: false, message: 'API key is not valid.', error: data };
}
} catch (error) {
return { valid: false, message: 'Failed to test API key.', error: error };
}
}
// Function to periodically fetch progress and update the loading bar
function updateLoadingBar(title, prefix) {
$.get('/progress', function (data) {
if (data.current && data.total) {
var progress = (data.current / data.total) * 100;
$('#' + prefix + '-loading-bar').css('width', progress + '%');
$('#' + prefix + '-loading-percent').text(Math.round(progress) + '%'); // Update the text
}
if (data.complete) {
deliverPDF(data.text, title);
// Hide the loading bar when processing is complete
$('#' + prefix + '-loading-bar-container').hide();
}
else {
setTimeout(() => updateLoadingBar(title, prefix), 1000); // Update every second
}
});
}
// JavaScript Part
function deliverPDF(text, title) {
// Prepare the data to be sent in the request
const data = JSON.stringify({text: text, title: title});
// Use the fetch API to send the POST request
fetch('/create-pdf', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: data
})
.then(response => {
// Check if the response is ok (status in the range 200-299)
if (!response.ok) {
throw new Error('Network response was not ok');
}
// Retrieve the filename from the Content-Disposition header
// This is optional, but useful if you want to use the original filename
const filename = response.headers.get('Content-Disposition').split('filename=')[1].replaceAll('"', '');
return response.blob().then(blob => ({blob, filename}));
})
.then(({ blob, filename }) => {
// Create a URL for the blob object
const url = window.URL.createObjectURL(blob);
// Create an anchor (<a>) element with the URL as the href
const a = document.createElement('a');
a.href = url;
a.download = filename || 'download.pdf'; // Use the filename from the header or a default
document.body.appendChild(a); // Append the anchor to the body
a.click(); // Simulate a click on the anchor to trigger the download
window.URL.revokeObjectURL(url); // Clean up by revoking the blob URL
a.remove(); // Remove the anchor from the body
})
.catch(error => {
console.error('Error creating PDF:', error);
// Handle any errors that occurred during the fetch
});
}