/
app-image.js
42 lines (34 loc) · 1.18 KB
/
app-image.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
import { GoogleGenerativeAI } from "@google/generative-ai";
import fetch from 'node-fetch';
import dotenv from "dotenv";
import { promises as fs } from 'fs';
dotenv.config();
global.fetch = fetch;
// 1. configure the API key
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY);
// 2. Generate text
async function generateText() {
try {
const model = genAI.getGenerativeModel({ model: "gemini-pro-vision"});
const imagePath = 'img/lagos-dona-ana.jpg';
const imageData = await fs.readFile(imagePath);
const imageBase64 = imageData.toString('base64');
const parts = [
{
text: "Describe the image below in a few sentences and could you guess where this image was taken?",
},
{
inlineData: {
mimeType: 'image/jpeg',
data: imageBase64,
}
}
];
const result = await model.generateContent({ contents: [{role: "user", parts }] });
const response = await result.response;
console.log(response.text());
} catch(error) {
console.log(error);
}
}
generateText();