-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
77 lines (69 loc) · 2.03 KB
/
app.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
const express = require("express");
const fileUpload = require("express-fileupload");
const pdf = require("pdf-parse");
const inquirer = require('inquirer');
const openai = require("openai");
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
// Set OpenAI API Key
openai.apiKey = "YOUR_API_KEY";
// Initialize app
const app = express();
app.use(fileUpload());
// Set up routing
app.get("/", (req, res) => {
res.send(`
<h1>PDF Summary Generator</h1>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="pdf" accept=".pdf">
<button type="submit" disabled>Generate Summary</button>
</form>
<script>
// Handle file selection
document.querySelector("input[type=file]").addEventListener("change", (event) => {
// Enable the submit button if a file is selected
document.querySelector("button").disabled = !event.target.files.length;
});
</script>
`);
});
app.post("/upload", upload.single('pdf'), (req, res) => {
// Read the uploaded PDF file
pdfExtract.extract(req.file.path, {}, (err, data) => {
if (err) {
// Handle error from PDF parsing
res.send(`
<h1>Error</h1>
<p>${err.message}</p>
`);
return;
}
// Convert the extracted text to a string
const text = data.pages.map(page => page.content).join('\n');
// Use OpenAI's GPT-3 to generate summary
openai.createCompletion({
model: "text-davinci-002",
prompt: text,
temperature: 0.5,
max_tokens: text.length,
})
.then((response) => {
// Display summary
res.send(`
<h1>PDF Summary</h1>
<p>${response.data.choices[0].text}</p>
`);
})
.catch((error) => {
// Handle error from OpenAI API
res.send(`
<h1>Error</h1>
<p>${error.message}</p>
`);
});
});
});
// Start server
app.listen(3000, () => {
console.log("Server listening on port 3000");
});