File size: 1,483 Bytes
8de10f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import axios from 'axios';
import { GoogleGenerativeAI } from '@google/generative-ai';
import config from '../../config.cjs';

const geminiResponse = async (m, Matrix) => {
  const prefix = config.PREFIX;
const cmd = m.body.startsWith(prefix) ? m.body.slice(prefix.length).split(' ')[0].toLowerCase() : '';
const text = m.body.slice(prefix.length + cmd.length).trim();

  const apiKey = config.GEMINI_KEY;
  const genAI = new GoogleGenerativeAI(apiKey);
  const validCommands = ['gemini', 'vision'];

  if (validCommands.includes(cmd)) {
    if (!m.quoted || m.quoted.mtype !== 'imageMessage') {
      return m.reply(`*Send/Reply with an Image ${prefix + cmd}*`);
    }
    
    m.reply("Please wait...");

    try {
      const prompt = text;
      const media = await m.quoted.download();

      const imagePart = {
        inlineData: {
          data: Buffer.from(media).toString("base64"),
          mimeType: "image/png",
        },
      };

      const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash-latest" });
      const result = await model.generateContent([prompt, imagePart]);
      const response = result.response;

      const textResponse = await response.text();
      m.reply(`${textResponse}`);
    } catch (error) {
      console.error('Error in Gemini Pro Vision:', error);
      m.reply(`An error occurred: ${error.message}`);
      await m.React("❌");
    }
  }
};

export default geminiResponse;