require('dotenv').config(); const express = require('express'); const multer = require('multer'); const cors = require('cors'); const path = require('path'); const fs = require('fs'); const pdfParse = require('pdf-parse'); const pptxParser = require('pptx-parser'); const { GoogleGenerativeAI } = require('@google/generative-ai'); const app = express(); const port = process.env.PORT || 3000; // Middleware app.use(cors()); app.use(express.json()); app.use(express.static('public')); // Configure multer for file upload const storage = multer.diskStorage({ destination: function (req, file, cb) { const uploadDir = 'uploads'; if (!fs.existsSync(uploadDir)) { fs.mkdirSync(uploadDir); } cb(null, uploadDir); }, filename: function (req, file, cb) { cb(null, Date.now() + path.extname(file.originalname)); } }); const upload = multer({ storage: storage, fileFilter: function (req, file, cb) { const allowedTypes = ['.pdf', '.pptx']; const ext = path.extname(file.originalname).toLowerCase(); if (allowedTypes.includes(ext)) { cb(null, true); } else { cb(new Error('Only PDF and PPTX files are allowed')); } } }); // Initialize Gemini AI const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY); // Extract text from PDF async function extractTextFromPDF(filePath) { const dataBuffer = fs.readFileSync(filePath); const data = await pdfParse(dataBuffer); return data.text; } // Extract text from PPTX async function extractTextFromPPTX(filePath) { const result = await pptxParser.parseFile(filePath); let text = ''; result.slides.forEach(slide => { text += slide.text + '\n'; }); return text; } // Generate notes using Gemini AI async function generateNotes(text) { const model = genAI.getGenerativeModel({ model: 'gemini-pro' }); const prompt = `Please create well-structured, comprehensive notes from the following content. Include main points, key concepts, and important details:\n\n${text}`; const result = await model.generateContent(prompt); const response = await result.response; return response.text(); } // File upload and processing endpoint app.post('/upload', upload.single('file'), async (req, res) => { try { if (!req.file) { return res.status(400).json({ error: 'No file uploaded' }); } const filePath = req.file.path; const fileExt = path.extname(req.file.originalname).toLowerCase(); let extractedText = ''; if (fileExt === '.pdf') { extractedText = await extractTextFromPDF(filePath); } else if (fileExt === '.pptx') { extractedText = await extractTextFromPPTX(filePath); } const notes = await generateNotes(extractedText); // Clean up uploaded file fs.unlinkSync(filePath); res.json({ notes }); } catch (error) { console.error('Error processing file:', error); res.status(500).json({ error: 'Error processing file' }); } }); app.listen(port, () => { console.log(`Server is running on port ${port}`); });