-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (65 loc) · 2.33 KB
/
index.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
const express = require("express");
const multer = require("multer");
const { GoogleGenerativeAI } = require("@google/generative-ai");
require("dotenv").config(); // For loading environment variables from a .env file
const app = express();
const port = process.env.PORT || 3000;
// Middleware to parse JSON bodies
app.use(express.json());
// Check if the API key is available
if (!process.env.GOOGLE_GENERATIVE_AI_KEY) {
console.error("Google Generative AI API key is missing");
process.exit(1);
}
// Initialize the Google Generative AI with the API key from environment variables
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_GENERATIVE_AI_KEY);
// Configure multer for handling form-data (including file uploads)
const upload = multer({
limits: { fileSize: 10 * 1024 * 1024 }, // 10 MB limit
fileFilter: (req, file, cb) => {
const allowedTypes = ['image/jpeg', 'image/png', 'application/pdf'];
if (allowedTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(new Error('Invalid file type'), false);
}
}
});
// Converts file information to a GoogleGenerativeAI.Part object
function fileToGenerativePart(file) {
return {
inlineData: {
data: file.buffer.toString("base64"),
mimeType: file.mimetype,
},
};
}
// POST endpoint to read a file (image or PDF)
app.post("/read-file", upload.single("file"), async (req, res) => {
try {
const file = req.file;
if (!file) {
return res.status(400).json({ error: "File is required" });
}
const prompt = "What's in this file? Explain in 10 points.";
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const filePart = fileToGenerativePart(file);
const result = await model.generateContent([prompt, filePart]);
const response = await result.response;
const text = await response.text();
res.json({ generatedText: text });
} catch (error) {
console.error("Error generating content:", error);
res.status(500).json({ error: "An error occurred while generating content", details: error.message });
}
});
// Error handling middleware for Multer
app.use((err, req, res, next) => {
if (err instanceof multer.MulterError) {
return res.status(400).json({ error: err.message });
}
next(err);
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});