-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
296 lines (264 loc) · 7.97 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
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
const Groq = require("groq-sdk");
if (process.env.NODE_ENV != "production") {
require("dotenv").config();
}
const CancerData = require("./models/upload.js");
const groq = new Groq({ apiKey: process.env.GROQ_API_KEY });
const https = require("https");
const API_KEY = process.env.PDF_API_KEY;
const predict = require("./AI/testModel.js");
const cloudinary = require("cloudinary").v2;
const express = require("express");
const app = express();
const mongoose = require("mongoose");
const path = require("path");
const methodOverride = require("method-override");
const ejsMate = require("ejs-mate");
const session = require("express-session");
const bodyParser = require("body-parser");
const MongoStore = require("connect-mongo");
const LocalStrategy = require("passport-local");
const passport = require("passport");
const flash = require("connect-flash");
const processImage = require("./run.js")
const multer = require("multer");
const { storage } = require("./cloudConfig.js");
const upload = multer({ storage });
const dbUrl = process.env.ATLASDB_URL;
const store = MongoStore.create({
mongoUrl: dbUrl,
crypto: {
secret: process.env.SECRET,
},
touchAfter: 24 * 60 * 60,
});
store.on("error", (error) => {
console.log("Error in MONGO SESSION STORE: ", error);
});
const sessionOptions = {
store,
secret: process.env.SECRET,
resave: false,
saveUninitialized: true,
cookie: {
expires: Date.now() + 7 * 24 * 60 * 60 * 1000,
maxAge: 7 * 24 * 60 * 60 * 1000,
httpOnly: true,
},
};
async function main() {
await mongoose.connect(dbUrl);
}
main()
.then(() => {
console.log("Connection Succeeded");
})
.catch((err) => console.log(err));
app.use(session(sessionOptions));
app.use(flash());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "/views"));
app.use(express.static(path.join(__dirname, "public")));
app.use("public/media/", express.static("./public/media"));
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride("_method"));
app.engine("ejs", ejsMate);
app.use(express.json());
app.use((req, res, next) => {
res.locals.messages = req.flash();
next();
});
const { GoogleGenerativeAI } = require("@google/generative-ai");
const fs = require("fs");
const dotenv = require("dotenv");
dotenv.config();
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
function fileToGenerativePart(path, mimeType) {
return {
inlineData: {
data: Buffer.from(fs.readFileSync(path)).toString("base64"),
mimeType,
},
};
}
let port = 3000;
app.listen(port, (req, res) => {
console.log("Listening to the Port: http://localhost:3000/scan");
});
app.get("/scan", (req, res) => {
res.render("index.ejs");
});
async function reportAnalysis(report) {
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
const prompt = `Predicts the type of cancer based on the provided data. The report includes ${report}; however, for an accurate diagnosis and further evaluation, consultation with a medical professional is essential. Summarize the report in three key bullet points.`;
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
return text;
}
app.post("/scan", upload.single("file"), async (req, res) => {
try {
const { userName, fileType, scanType, textInput } = req.body;
let filePath;
let cancerClass;
let resultPdf;
let resultImg;
let extractedTextImg;
if (fileType === "pdf") {
filePath = req.file.path;
try {
let imageUrls = await convertPDFToImageFromURL(filePath);
if (imageUrls && imageUrls.length > 0) {
// Now this should work correctly
let extractedText = await processImage(imageUrls[0]);
resultPdf = await reportAnalysis(extractedText);
} else {
throw new Error("No image URLs returned from PDF conversion");
}
} catch (e) {
console.error("Error processing PDF:", e);
throw new Error(`Failed to process PDF: ${e.message}`);
}
}else if (fileType === "image") {
filePath = req.file.path;
console.log(filePath);
try {
resultImg = await predict(filePath);
} catch (e) {
console.error("Error processing image:", e);
throw new Error(`Failed to process image: ${e.message}`);
}
} else if (fileType === "text") {
try {
let result = await reportAnalysis(textInput);
cancerClass = [result];
} catch (e) {
console.error("Error processing text input:", e);
throw new Error(`Failed to process text input: ${e.message}`);
}
} else {
throw new Error("Invalid file type");
}
if (fileType === "image") {
cancerClass = [resultImg];
} else if (fileType === "pdf") {
cancerClass = [resultPdf];
}
// Save the form data to the database
const formData = new CancerData({
userName,
fileType,
scanType,
filePath,
textInput,
cancerClass,
});
await formData.save();
req.flash("success", "Data Uploaded Successfully!");
res.status(200).json({
success: true,
cancerClass,
filePath: fileType !== "text" ? filePath : null,
});
} catch (error) {
console.error("Error in /scan route:", error);
req.flash(
"error",
error.message || "Failed to submit form or predict cancer class"
);
res.status(500).json({
success: false,
error: error.message || "Failed to submit form or predict cancer class",
});
}
});
function convertPDFToImageFromURL(
pdfUrl,
pages = "",
password = "",
imageType = "jpg"
) {
return new Promise((resolve, reject) => {
// Prepare URL for PDF to Image API call
const queryPath = `/v1/pdf/convert/to/${imageType}`;
// JSON payload for API request
const jsonPayload = JSON.stringify({
password: password,
pages: pages,
url: pdfUrl,
});
const reqOptions = {
host: "api.pdf.co",
method: "POST",
path: queryPath,
headers: {
"x-api-key": API_KEY,
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(jsonPayload, "utf8"),
},
};
// Send request
const postRequest = https.request(reqOptions, (response) => {
let chunks = [];
response.on("data", (chunk) => {
chunks.push(chunk);
});
response.on("end", () => {
const data = JSON.parse(Buffer.concat(chunks).toString());
if (data.error === false) {
// Resolve with the URLs of the generated image files
resolve(data.urls);
} else {
// Reject with the error message from the API
reject(`Error: ${data.message}`);
}
});
});
postRequest.on("error", (e) => {
// Handle request error
reject(`Request error: ${e.message}`);
});
// Write request data
postRequest.write(jsonPayload);
postRequest.end();
});
}
// async function imageToText(iurl) {
// try {
// const chatCompletion = await groq.chat.completions.create({
// messages: [
// {
// role: "user",
// content:
// "Extract the text only from the following image. Don't give any description about the image.",
// },
// {
// role: "user",
// content: [
// {
// type: "image_url",
// image_url: {
// url: iurl,
// },
// },
// ],
// },
// ],
// model: "llama-3.2-11b-vision-preview",
// temperature: 0.7,
// max_tokens: 1024,
// top_p: 1,
// stream: false,
// stop: null,
// });
// return chatCompletion.choices[0].message.content;
// } catch (error) {
// console.error("Error in imageToText:", error);
// throw error;
// }
// }
app.get("*", (req, res) => {
res.redirect("/scan");
});