renamer.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import fs from 'fs';
  2. export async function getkeywords(image: string): Promise<string[]> {
  3. const body = {
  4. "model": "llava:13b-v1.5-q5_K_M",
  5. "prompt": `Describe the image as a collection of keywords. Output in JSON format. Use the following schema: { filename: string, keywords: string[] }`,
  6. "format": "json",
  7. "images": [image],
  8. "stream": false
  9. };
  10. const response = await fetch("http://localhost:11434/api/generate", {
  11. method: "POST",
  12. headers: {
  13. "Content-Type": "application/json",
  14. },
  15. body: JSON.stringify(body),
  16. });
  17. const json = await response.json();
  18. const keywords = JSON.parse(json.response);
  19. return keywords?.keywords || [];
  20. }
  21. async function main() {
  22. for (const file of fs.readdirSync(".")) {
  23. if (file.endsWith(".jpg") || file.endsWith(".png")) {
  24. const currentpath = __dirname;
  25. const b64 = fs.readFileSync(`${currentpath}/${file}`, { encoding: 'base64' });
  26. const keywords = await getkeywords(b64.toString());
  27. const fileparts = keywords.map(k => k.replace(/ /g, "_"));
  28. const fileext = file.split(".").pop();
  29. const newfilename = fileparts.join("-") + "." + fileext;
  30. fs.copyFileSync(`${currentpath}/${file}`, `${currentpath}/${newfilename}`);
  31. console.log(`Copied ${file} to ${newfilename}`);
  32. }
  33. }
  34. }
  35. main();