-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
181 lines (150 loc) · 5 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
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
const express = require('express');
const bodyParser = require('body-parser');
const natural = require('natural');
const path = require('path');
const mongoose = require('mongoose');
const multer = require('multer');
const fs = require('fs');
const app = express();
const port = process.env.PORT || 3000;
const tokenizer = new natural.SentenceTokenizer();
const wordTokenizer = new natural.WordTokenizer();
const stopWords = new Set(natural.stopwords.words);
function summarizeText(inputText, numSentences = 5) {
// Tokenize the input text into sentences
const sentences = tokenizer.tokenize(inputText);
// Tokenize each sentence into words
const words = sentences.flatMap(sentence =>
wordTokenizer.tokenize(sentence.toLowerCase())
);
// Remove stopwords (common words that don't carry much meaning)
const filteredWords = words.filter(word => word.match(/[a-zA-Z0-9]/) && !stopWords.has(word));
// Calculate word frequency
const wordFreq = {};
filteredWords.forEach(word => {
wordFreq[word] = (wordFreq[word] || 0) + 1;
});
// Assign importance scores to sentences based on word frequency
const sentenceScores = {};
sentences.forEach((sentence, i) => {
const sentenceWords = wordTokenizer.tokenize(sentence);
sentenceWords.forEach(word => {
if (wordFreq[word]) {
sentenceScores[i] = (sentenceScores[i] || 0) + wordFreq[word];
}
});
});
// Select the top 'numSentences' sentences with the highest scores
const selectedSentences = Object.keys(sentenceScores)
.sort((a, b) => sentenceScores[b] - sentenceScores[a])
.slice(0, numSentences);
// Generate the summarized text
const summarizedText = selectedSentences
.map(index => sentences[index])
.join(' ');
return summarizedText;
}
// Connect to MongoDB
mongoose.connect('mongodb://mymongodbac:AFExw5Veb5znSY8InRmXN5nhe1kQKWubbf1bVrBWYPa6F50kH8EROgdABn7O44fP3wosnXFDo9ylACDbdagGbA==@mymongodbac.mongo.cosmos.azure.com:10255/?ssl=true&retrywrites=false&replicaSet=globaldb&maxIdleTimeMS=120000&appName=@mymongodbac@', {
});
// Define Mongoose schema for image and PDF
const imageSchema = new mongoose.Schema({
path: String,
text: String,
summary: String,
});
const pdfSchema = new mongoose.Schema({
text: String,
summary: String,
});
const Image = mongoose.model('Image', imageSchema);
const Pdf = mongoose.model('Pdf', pdfSchema);
// tis is for store an image on upload folder
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads');
},
filename: (req, file, cb) => {
cb(null, Date.now() + path.extname(file.originalname));
},
});
const upload = multer({ storage });
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('public'));
// Serve the HTML page
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// Handle image upload and summarization
app.post('/summarizeImage', upload.single('fileInput'), async (req, res) => {
try {
const imgText = req.body.textimg;
const imgSummary = req.body.summaryResultimg;
const imagePath = req.file.path;
// Save image data to MongoDB
const image = new Image({
path: imagePath,
text: imgText,
summary: imgSummary, // Save the binary image data
});
await image.save();
res.send({ message: 'Image data stored successfully' });
} catch (error) {
console.error('Error:', error);
res.status(500).send({ error: 'Internal Server Error' });
}
});
// Handle PDF upload and summarization
app.post('/summarizePdf', upload.single('pdfInput'), async (req, res) => {
try {
const pdfText = req.body.text;
const pdfSummary = req.body.summaryResult;
// Save PDF data to MongoDB
const pdf = new Pdf({
text: pdfText,
summary: pdfSummary,
});
await pdf.save();
res.send({ message: 'PDF data stored successfully' });
} catch (error) {
console.error('Error:', error);
res.status(500).send({ error: 'Internal Server Error' });
}
});
app.post('/summarize', (req, res) => {
try {
const inputText = req.body.text;
const summary = summarizeText(inputText);
res.send({ summary });
} catch (error) {
console.error('Error:', error);
res.status(500).send({ error: 'Internal Server Error' });
}
});
app.get('/history', (req, res) => {
res.sendFile(path.join(__dirname, 'history.html'));
});
// Fetch image data
app.get('/fetchImageData', async (req, res) => {
try {
const imageData = await Image.find();
res.json(imageData);
} catch (error) {
console.error('Error:', error);
res.status(500).send({ error: 'Internal Server Error' });
}
});
// Fetch PDF data
app.get('/fetchPdfData', async (req, res) => {
try {
const pdfData = await Pdf.find();
res.json(pdfData);
} catch (error) {
console.error('Error:', error);
res.status(500).send({ error: 'Internal Server Error' });
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});