forked from Ahmsanz/node-pdfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
279 lines (235 loc) · 12.4 KB
/
server.mjs
File metadata and controls
279 lines (235 loc) · 12.4 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
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
/**
* problem: CommonJS require() vs ESM import/export standard
* https://stackoverflow.com/questions/70691479/is-commonjs-require-still-used-or-deprecated
*
* Solution: Changed file extention to .mjs and all modules to ESM imports (not require())
* https://stackoverflow.com/a/62749284/14154848
*/
import helmet from 'helmet';
import bodyParser from 'body-parser';
import path from 'path';
import { ChatGPTAPI } from 'chatgpt';
import express from 'express';
import dotenv from 'dotenv-safe';
import { oraPromise } from 'ora';
/* configuring server to parse multipart/form-data uploads */
import multer from 'multer';
import fs from 'fs';
/* importing utils */
import { getTextFromPDF, readQuestionsFromFile, saveQuestionsToFile, structureRegex, structureResponse } from './utils.mjs';
/**
* Solution to "__dirname does not exist in ESM": https://stackoverflow.com/a/62892482/14154848
*/
import { fileURLToPath } from 'url';
import { dirname } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const app = express();
app.set('view engine', 'ejs');
/* changing the views default path: https://stackoverflow.com/a/45903536/14154848 */
app.set('views', path.join(__dirname, '/web/'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(helmet()); // additional HTTP headers to protect against XSS, clickjacking, etc.
app.use ('/static', express.static('web'));
app.use('/build', express.static('build'));
app.listen(4000, () => console.log('Server running. Please navigate to http://localhost:4000 on your browser.'));
dotenv.config()
/* variables */
var questionFolder = "web/questions/";
var questionFilePath = "";
var questionArray;
var pdfFolder = "pdfs/"
var fileName = "";
var filePath = ""; // default is empty
// Multer middleware configuration
const storage = multer.diskStorage({
destination: function (req, file, cb) {
let path = 'web/' + pdfFolder;
cb(null, path);
},
filename: function (req, file, cb) {
const filename = file.originalname;
let filepath = 'web/' + pdfFolder + filename;
/* Check if file exists on server before upload */
if (fs.existsSync(filepath)) {
// File already exists, delete it and accept the upload
fs.unlink(filepath, (err) => {
if (err) {
cb(new Error('Failed to delete existing file'));
} else {
console.log("Deleted existing file, accepting upload.");
cb(null, filename);
}
});
} else {
// File does not exist, accept the upload
console.log("New file, accepting upload.");
cb(null, filename);
}
}
});
const upload = multer({ storage: storage });
/**
* Gets learning questions (and answers) from ChatGPT based on an array of page/paragraph text.
* @param {array} textArray - array of PDF text, each elem is a page/paragraph of text
* @param {string} questionType - type of the questions to be generated. Either 'Comprehension' or 'Analysis'.
* @param {number} numQuestions - number of learning questions to generate per page.
* @returns {array} resultArray - array of learning questions, each elem is an array of questions for a page/paragraph. Each question is followed by an answer.
*/
async function gptQuestionFunc(textArray, questionType = 'comprehension', numQuestions = 4) {
let prompt = '';
let resultArray = [];
console.log("Question Type:", questionType);
// maximum request is 4097 tokens, shorten text to a maximum of 2900 characters to stay below
switch (questionType) {
case 'comprehension':
prompt = `Write ${numQuestions} comprehension questions followed by answers to the questions on a new line about the following research article:
"${textArray[0].trimEnd().slice(0,2900)}".
Number these questions with a C (like C1, C2, etc) and output each question to a new line. Output an answer preceded with 'Answer:' to a new line after each question.`
break;
case 'analysis':
prompt = `Analysis questions are questions that force the reader to reflect and expand beyond the scope of the paper. These types of questions require less regurgitation and more sustained thought, forcing the reader to identify reasons or motives, identify relations across texts, and reach a conclusion. For example, analysis questions include “What are the limitations of this paper?”, “What are the weaknesses in this writer’s argument?”, and “How does the program in this paper compare to existing programs?”.
Based on this definition, write ${numQuestions} analysis questions followed by answers to the questions on a new line about the following research article:
"${textArray[0].trimEnd().slice(0,2900)}".
Number these questions with an A (like A1, A2, etc) and output each question to a new line. Output an answer preceded with 'Answer:' to a new line after each question.`
break;
case 'both':
prompt = `Write ${Math.ceil(numQuestions/2)} comprehension questions followed by answers to the questions on a new line about the following research article:
"${textArray[0].trimEnd().slice(0,2900)}".
Number these questions with a C (like C1, C2, etc) and output each question to a new line. Output an answer preceded with 'Answer:' to a new line after each question.
After, generate ${Math.floor(numQuestions/2)} analysis questions about the same text. Write these analysis questions based on this definition:
'Analysis questions are questions that force the reader to reflect and expand beyond the scope of the paper. These types of questions require less regurgitation and more sustained thought, forcing the reader to identify reasons or motives, identify relations across texts, and reach a conclusion. For example, analysis questions include “What are the limitations of this paper?”, “What are the weaknesses in this writer’s argument?”, and “How does the program in this paper compare to existing programs?”.'
Number these questions with an A (like A1, A2, etc) and output each question to a new line. Output an answer preceded with 'Answer:' to a new line after each question.`
break;
default:
prompt = `Write ${numQuestions} comprehension questions followed by answers to the questions on a new line about the following research article:
"${textArray[0].trimEnd().slice(0,2900)}".
Number these questions with a C (like C1, C2, etc) and output each question to a new line. Output an answer preceded with 'Answer:' to a new line after each question.`
break;
}
const api = new ChatGPTAPI({
apiKey: process.env.OPENAI_API_KEY
})
// First page
let res = await oraPromise(api.sendMessage(prompt), {
text: 'Loading questions for Page 1 of ' + textArray.length
});
resultArray[0] = structureResponse(res.text);
// Loop through remaining pages and generate learning questions (set loop to index < textArray.length to go through all pages)
for (let index = 1; index < textArray.length; index++) {
switch (questionType) {
case 'comprehension':
prompt = `Write ${numQuestions} comprehension questions followed by answers to the questions on a new line about the following research article:
"${textArray[index].trimEnd().slice(0,2900)}".
Number these questions with a C (like C1, C2, etc) and output each question to a new line. Output an answer preceded with 'Answer:' to a new line after each question.`
break;
case 'analysis':
prompt = `Based on the previously provided definition of analysis questions, write ${numQuestions} analysis questions followed by answers to the questions on a new line about the following research article:
"${textArray[index].trimEnd().slice(0,2900)}".
Number these questions with an A (like A1, A2, etc) and output each question to a new line. Output an answer preceded with 'Answer:' to a new line after each question.`
break;
case 'both':
prompt = `Write ${Math.ceil(numQuestions/2)} comprehension questions followed by answers to the questions on a new line about the following research article:
"${textArray[index].trimEnd().slice(0,2900)}".
Number these questions with a C (like C1, C2, etc) and output each question to a new line. Output an answer preceded with 'Answer:' to a new line after each question.
After, generate ${Math.floor(numQuestions/2)} analysis questions about the same text. Write these analysis questions based on the previously provided definition for analysis questions.
Number these questions with an A (like A1, A2, etc) and output each question to a new line. Output an answer preceded with 'Answer:' to a new line after each question.`
break;
default:
prompt = `Write ${numQuestions} comprehension questions followed by answers to the questions on a new line about the following research article:
"${textArray[index].trimEnd().slice(0,2900)}".
Number these questions with a C (like C1, C2, etc) and output each question to a new line. Output an answer preceded with 'Answer:' to a new line after each question.`
break;
}
let pageNum = index + 1
res = await oraPromise(
api.sendMessage(prompt, {
conversationId: res.conversationId,
parentMessageId: res.messageId
}),
{
text: 'Loading questions for Page ' + pageNum + ' of ' + textArray.length
}
)
resultArray[index] = structureResponse(res.text);
}
return resultArray;
}
app.get('/', function(req, res) {
// res.sendFile(path.join(__dirname + '/public/visor.html'));
// Straight to viewer, no need for visor.html
res.redirect('/static/viewer.html' + '?file=' + filePath);
});
app.get('/static/viewer.html', function(req, res) {
// res.sendFile(path.join(__dirname + '/static/viewer.ejs'));
res.render("viewer", {questionArray: questionArray});
});
/* POST request with Multer file upload middleware function before route handler function */
app.post('/upload', upload.single('pdfFile'), async (req, res) => {
console.log("Uploaded file:", req.file);
const pdfFile = req.file;
if (!pdfFile) {
return res.status(400).send('No file uploaded.');
}
/* update pdfFile and questionFile paths */
fileName = pdfFile.filename;
filePath = pdfFolder + pdfFile.filename;
questionFilePath = questionFolder + fileName.slice(0, -4) + '.txt'; // slice() to replace .pdf with .txt. (Ex: web/questions/sam.txt)
/* check if questionsFile exists, if so, open it */
fs.access(questionFilePath, fs.constants.F_OK, (err) => {
if (err) {
// File doesn't exist
console.error("No question file found.");
questionArray = []; // empty the questionArray
} else {
// File exists, read questions from file
questionArray = readQuestionsFromFile(questionFilePath);
console.log("Question file found and loaded.");
console.log("QuestionArray length:", questionArray.length);
}
});
/* Redirect client to newly opened PDF */
res.redirect('/static/viewer.html' + '?file=' + filePath);
});
/* POST request triggered by "Generate Questions" button */
app.post('/static/viewer.html', function(req, res) {
console.log("Body:", req.body);
let questionType = '';
let numQuestions = req.body.num_questions;
let comprehension = req.body.comprehension;
let analysis = req.body.analysis;
// No file loaded, do nothing
if(filePath === '') {
console.log("No file loaded!");
res.redirect('/static/viewer.html' + '?file=' + filePath);
return;
}
/* pre-process questionType */
if(typeof comprehension !== "undefined" && typeof analysis !== "undefined") {
questionType = 'both';
}
else if(typeof comprehension !== "undefined") {
questionType = 'comprehension';
}
else if(typeof analysis !== "undefined") {
questionType = 'analysis';
} else {
// no checkbox selected, do nothing
console.log("No checkboxes selected.");
res.redirect('/static/viewer.html' + '?file=' + filePath);
return;
}
getTextFromPDF("web/" + filePath).then(pageTexts => {
// console.log(pageTexts);
gptQuestionFunc(pageTexts, questionType, numQuestions).then(lqs => {
// console.log(lqs);
console.log("Done!");
questionArray = lqs;
// Write result to question file
saveQuestionsToFile(questionArray, questionFilePath);
console.log("Saved questions to file.");
res.redirect('/static/viewer.html' + '?file=' + filePath);
});
});
})