-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
68 lines (53 loc) · 1.91 KB
/
renderer.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
const { GoogleGenerativeAI } = require('@google/generative-ai');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
const {
showRenameConfirmation,
fileToGenerativePart,
handleFileSelect,
} = require('./helper');
document
.getElementById('imageInput')
.addEventListener('change', handleFileSelect);
document.getElementById('renameButton').addEventListener('click', async () => {
const files = document.getElementById('imageInput').files;
console.log(files);
if (files.length == 0) {
alert('Please select an image to rename.');
return;
}
await renameImages(files);
});
async function renameImages(files) {
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: 'gemini-pro-vision' });
const prompt =
'Rename image to a concise description of the main content using lowercase_separators under 5 words.';
// 'Anaylze the image and provide the best name for it based on the image content in max 3 words.';
for (const file of files) {
const loader = document.getElementById('loader-container');
loader.style.display = 'flex';
try {
const image = fileToGenerativePart(file.path, file.type);
const result = await model.generateContent([prompt, image]);
const response = await result.response;
const newName = response.text();
console.log('name - ', newName);
const newFilePath = path.join(
path.dirname(file.path),
newName + path.extname(file.path)
);
console.log('newFilePath - ', newFilePath);
const confirmName = await showRenameConfirmation(file, newName);
console.log('confirmName - ', confirmName);
if (confirmName) {
fs.renameSync(file.path, newFilePath);
}
} catch (error) {
console.error('Error during renaming:', error);
} finally {
loader.style.display = 'none';
}
}
}