|
1 | 1 | const express = require("express"); |
2 | 2 | const multer = require("multer"); |
3 | 3 | const fs = require("fs"); |
4 | | -const fetch = require("node-fetch"); |
5 | 4 | const vision = require("@google-cloud/vision"); |
| 5 | +const { VertexAI } = require("@google-cloud/vertexai"); |
6 | 6 | const { verifyRecaptcha } = require("../helpers/verifyRecaptcha"); |
7 | 7 |
|
8 | 8 | const router = express.Router(); |
9 | | -const upload = multer({ dest: "uploads/" }); |
10 | 9 |
|
11 | | -const GEMINI_API_KEY = process.env.GEMINI_API_KEY; |
12 | | -const RECAPTCHA_SECRET_KEY = process.env.RECAPTCHA_SECRET_KEY; |
13 | | - |
14 | | -if (!GEMINI_API_KEY) throw new Error("Gemini API key is required."); |
15 | | -if (!RECAPTCHA_SECRET_KEY) throw new Error("reCAPTCHA secret key is required."); |
| 10 | +// Multer config |
| 11 | +const upload = multer({ |
| 12 | + dest: "uploads/", |
| 13 | + limits: { fileSize: 5 * 1024 * 1024 }, // 5 MB max |
| 14 | +}); |
16 | 15 |
|
17 | 16 | const visionClient = new vision.ImageAnnotatorClient(); |
| 17 | +const vertexAI = new VertexAI({ |
| 18 | + project: process.env.GCLOUD_PROJECT, |
| 19 | + location: process.env.GCLOUD_LOCATION || "us-central1", |
| 20 | +}); |
18 | 21 |
|
19 | | -router.post("/", upload.single("image"), async (req, res) => { |
20 | | - const userPrompt = req.body.prompt || "Describe this image."; |
21 | | - const prompt = `Respond briefly: ${userPrompt} (Limit your answer to one short sentence.)`; |
22 | | - const cleanPrompt = prompt.trim().replace(/[^a-zA-Z0-9 ?.,!"()\-]/g, ""); |
23 | | - const recaptchaToken = req.body.recaptchaToken; |
24 | | - |
25 | | - if (!recaptchaToken) { |
26 | | - return res.status(400).json({ error: "Missing reCAPTCHA token." }); |
| 22 | +// Function-calling schema |
| 23 | +const analyzeImageSchema = { |
| 24 | + name: "analyze_image", |
| 25 | + description: "Analyze an image and return a short structured description.", |
| 26 | + parameters: { |
| 27 | + type: "object", |
| 28 | + properties: { |
| 29 | + description: { |
| 30 | + type: "string", |
| 31 | + description: "A short, safe description of the image content." |
| 32 | + } |
| 33 | + }, |
| 34 | + required: ["description"] |
27 | 35 | } |
| 36 | +}; |
28 | 37 |
|
29 | | - if (!req.file) { |
30 | | - return res.status(400).json({ error: "Image file is required." }); |
31 | | - } |
| 38 | +router.post("/", upload.single("image"), async (req, res) => { |
| 39 | + const prompt = (req.body.prompt || "Describe this image.").trim().slice(0, 300); |
| 40 | + const recaptchaToken = req.body.recaptchaToken; |
32 | 41 |
|
33 | | - if (!cleanPrompt || cleanPrompt.length > 300) { |
34 | | - return res.status(400).json({ error: "Prompt must be 1–300 characters." }); |
35 | | - } |
| 42 | + if (!recaptchaToken) return res.status(400).json({ error: "Missing reCAPTCHA token." }); |
| 43 | + if (!req.file) return res.status(400).json({ error: "Image file is required." }); |
36 | 44 |
|
37 | | - // ✅ reCAPTCHA verification |
| 45 | + // Verify reCAPTCHA |
38 | 46 | try { |
39 | | - const recaptchaResult = await verifyRecaptcha(recaptchaToken); |
40 | | - |
41 | | - const score = recaptchaResult.score ?? 0; |
42 | | - const success = recaptchaResult.success === true; |
43 | | - |
| 47 | + const { success, score } = await verifyRecaptcha(recaptchaToken); |
44 | 48 | if (!success || score < 0.5) { |
45 | | - console.warn("⚠️ reCAPTCHA verification failed", { |
46 | | - ip: req.ip, |
47 | | - score, |
48 | | - success, |
49 | | - }); |
50 | 49 | return res.status(403).json({ error: "reCAPTCHA verification failed." }); |
51 | 50 | } |
52 | 51 | } catch (err) { |
53 | | - console.error("Error verifying reCAPTCHA:", err); |
| 52 | + console.error("reCAPTCHA error:", err); |
54 | 53 | return res.status(500).json({ error: "Failed to verify reCAPTCHA." }); |
55 | 54 | } |
56 | 55 |
|
57 | 56 | const imagePath = req.file.path; |
58 | 57 |
|
59 | 58 | try { |
60 | | - // NSFW filtering using Cloud Vision SafeSearch |
| 59 | + // ✅ Content safety via Vision API |
61 | 60 | const [result] = await visionClient.safeSearchDetection(imagePath); |
62 | 61 | const safe = result.safeSearchAnnotation; |
63 | | - |
64 | 62 | if ( |
65 | | - safe.adult === "LIKELY" || |
66 | | - safe.adult === "VERY_LIKELY" || |
67 | | - safe.violence === "LIKELY" || |
68 | | - safe.violence === "VERY_LIKELY" || |
| 63 | + ["LIKELY", "VERY_LIKELY"].includes(safe.adult) || |
| 64 | + ["LIKELY", "VERY_LIKELY"].includes(safe.violence) || |
69 | 65 | safe.racy === "VERY_LIKELY" |
70 | 66 | ) { |
71 | | - console.warn("Blocked NSFW image:", safe); |
72 | | - return res |
73 | | - .status(403) |
74 | | - .json({ error: "Image flagged as unsafe by content filter." }); |
| 67 | + return res.status(403).json({ error: "Image flagged as unsafe." }); |
75 | 68 | } |
76 | 69 |
|
77 | | - const imageBuffer = fs.readFileSync(imagePath); |
78 | | - const base64Image = imageBuffer.toString("base64"); |
79 | | - |
80 | | - const geminiRes = await fetch( |
81 | | - `https://generativelanguage.googleapis.com/v1/models/gemini-1.5-pro:generateContent?key=${GEMINI_API_KEY}`, |
82 | | - { |
83 | | - method: "POST", |
84 | | - headers: { "Content-Type": "application/json" }, |
85 | | - body: JSON.stringify({ |
86 | | - contents: [ |
87 | | - { |
88 | | - parts: [ |
89 | | - { |
90 | | - inline_data: { |
91 | | - mime_type: req.file.mimetype, |
92 | | - data: base64Image, |
93 | | - }, |
94 | | - }, |
95 | | - { |
96 | | - text: cleanPrompt, |
97 | | - }, |
98 | | - ], |
99 | | - }, |
| 70 | + // ✅ Read + encode |
| 71 | + const base64Image = fs.readFileSync(imagePath).toString("base64"); |
| 72 | + |
| 73 | + // ✅ Vertex AI with function calling |
| 74 | + const model = vertexAI.getGenerativeModel({ |
| 75 | + model: "gemini-2.5-flash", |
| 76 | + tools: [{ functionDeclarations: [analyzeImageSchema] }], |
| 77 | + }); |
| 78 | + |
| 79 | + const resultAI = await model.generateContent({ |
| 80 | + contents: [ |
| 81 | + { |
| 82 | + role: "user", |
| 83 | + parts: [ |
| 84 | + { inline_data: { mime_type: req.file.mimetype, data: base64Image } }, |
| 85 | + { text: prompt }, |
100 | 86 | ], |
101 | | - }), |
| 87 | + }, |
| 88 | + ], |
| 89 | + toolConfig: { |
| 90 | + functionCallingConfig: { |
| 91 | + mode: "ANY", // Force Gemini to pick a function instead of free text |
| 92 | + }, |
102 | 93 | }, |
103 | | - ); |
104 | | - |
105 | | - if (!geminiRes.ok) { |
106 | | - const errorData = await geminiRes.json(); |
107 | | - console.error("Gemini API error:", errorData); |
108 | | - return res.status(geminiRes.status).json({ |
109 | | - error: errorData.error?.message || "Unknown error from Gemini API.", |
110 | | - }); |
111 | | - } |
| 94 | + }); |
112 | 95 |
|
113 | | - const data = await geminiRes.json(); |
114 | | - console.log("Gemini API response:", JSON.stringify(data, null, 2)); |
| 96 | + // ✅ Extract function call |
| 97 | + const fnCall = resultAI.response?.candidates?.[0]?.content?.parts?.find( |
| 98 | + (p) => p.functionCall |
| 99 | + )?.functionCall; |
115 | 100 |
|
116 | | - const responseText = data.candidates?.length |
117 | | - ? data.candidates[0].content?.parts?.[0]?.text || |
118 | | - "Response format unexpected." |
119 | | - : "No candidates returned from Gemini."; |
| 101 | + if (!fnCall || !fnCall.args) { |
| 102 | + console.error("❌ No structured functionCall:", JSON.stringify(resultAI, null, 2)); |
| 103 | + return res.status(500).json({ error: "No structured description returned." }); |
| 104 | + } |
120 | 105 |
|
121 | | - res.json({ response: responseText }); |
| 106 | + // ✅ Clean, structured result |
| 107 | + const response = fnCall.args; |
| 108 | + res.json({ response }); |
122 | 109 | } catch (err) { |
123 | | - console.error("Error analyzing image:", err); |
| 110 | + console.error("Image analysis error:", err); |
124 | 111 | res.status(500).json({ error: "Error analyzing image." }); |
125 | 112 | } finally { |
126 | | - if (fs.existsSync(imagePath)) { |
127 | | - fs.unlinkSync(imagePath); // Cleanup temp file |
128 | | - } |
| 113 | + if (fs.existsSync(imagePath)) fs.unlinkSync(imagePath); // cleanup temp upload |
129 | 114 | } |
130 | 115 | }); |
131 | 116 |
|
|
0 commit comments