-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (66 loc) · 2.7 KB
/
index.js
File metadata and controls
82 lines (66 loc) · 2.7 KB
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
import express from "express";
import multer from "multer";
import { GoogleGenerativeAI, HarmCategory, HarmBlockThreshold } from "@google/generative-ai";
import path from "path";
import bodyParser from "body-parser";
import dotenv from 'dotenv';
dotenv.config();
const app = express();
const port = 2000;
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
const __filename = new URL(import.meta.url).pathname;
const __dirname = path.dirname(__filename);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'))
app.set('view engine', 'ejs');
let aiGenText = '';
app.get("/", (req, res) => {
res.render("index", { description: aiGenText ? aiGenText : null });
aiGenText = '';
});
app.post("/upload", upload.single("image"), async (req, res) => {
try {
// Your Gemini Pro Vision configuration
const API_KEY = process.env.API_KEY;
const MODEL_NAME = "gemini-pro-vision";
const genAI = new GoogleGenerativeAI(API_KEY);
const model = genAI.getGenerativeModel({ model: MODEL_NAME });
// Process the image
const generationConfig = {
temperature: 0.9,
topK: 40,
topP: 0.95,
maxOutputTokens: 1024,
};
const safetySettings = [
{ category: HarmCategory.HARM_CATEGORY_HARASSMENT, threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE },
{ category: HarmCategory.HARM_CATEGORY_HATE_SPEECH, threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE },
{ category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE },
{ category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE },
];
// Check if an image was uploaded
if (!req.file) {
return res.status(400).send("No image file uploaded");
}
const parts = [
{ text: "What is in this image? Describe it." },
{ inlineData: { mimeType: req.file.mimetype, data: req.file.buffer.toString("base64") } },
];
const result = await model.generateContent({
contents: [{ role: "user", parts }],
generationConfig,
safetySettings,
});
const response = result.response;
aiGenText = response.text();
// Redirect to the root route after processing the form
res.redirect("/");
} catch (error) {
console.error("Error during content generation:", error);
res.status(500).send("Internal Server Error");
}
});
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});