const fs = require('fs'); const markdownIt = require('markdown-it'); const dotenv = require('dotenv'); const { GoogleGenerativeAI, HarmBlockThreshold, HarmCategory } = require('@google/generative-ai'); dotenv.config(); const genAI = new GoogleGenerativeAI(process.env.GOOGLE_APPLICATION_CREDENTIALS_GENAI); // Converts local file information to a GoogleGenerativeAI.Part object. function fileToGenerativePart(path, mimeType) { return { inlineData: { data: Buffer.from(fs.readFileSync(path)).toString("base64"), mimeType }, }; } function generateHTMLFile(contentArray, outputFileName) { const md = new markdownIt({ html: true, linkify: true, typographer: true, }); let tabsNav = ''; let tabsContent = ''; // Loop through the content array to build tabs and content contentArray.forEach((contentObj, index) => { const tabId = 'tab' + index; const tabName = contentObj.tabName || 'Tab ' + (index + 1); const markdownContent = contentObj.content || ''; const htmlContentConverted = md.render(markdownContent); // Build the tabs navigation buttons tabsNav += ``; // Build the content for each tab tabsContent += `
${htmlContentConverted}
`; }); // Assemble the final HTML content const htmlContent = ` Tabbed Content
${tabsNav}
${tabsContent}
`; // Write the HTML content to the specified output file fs.writeFileSync(outputFileName, htmlContent, 'utf8'); console.log(`HTML file "${outputFileName}" has been generated.`); } const ocr_prompt = ` Extract all readable text from the image, preserving the layout and formatting as closely as possible. Capture details such as: All visible words, phrases, and sentences Numbers, dates, or other numeric information Special characters, including punctuation, currency symbols, and mathematical operators Text orientation (horizontal, vertical, or rotated) Any text embedded within logos, signs, or complex backgrounds Make sure to avoid any graphical elements and focus solely on text. Correctly identify letters with accents, diacritics, and special symbols. Ensure no part of the text is missed or misinterpreted `; function getGeminiModel(index) { var models = [ 'gemini-1.5-pro-exp-0827', 'gemini-1.5-pro', 'gemini-1.5-flash', ] const safetySettings = [ { category: HarmCategory.HARM_CATEGORY_HARASSMENT, threshold: HarmBlockThreshold.BLOCK_NONE, }, { category: HarmCategory.HARM_CATEGORY_HATE_SPEECH, threshold: HarmBlockThreshold.BLOCK_NONE, }, { category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold: HarmBlockThreshold.BLOCK_NONE }, { category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, threshold: HarmBlockThreshold.BLOCK_NONE } ]; var modelname = models[index]; const model = genAI.getGenerativeModel({ model: modelname, safetySettings: safetySettings }); return model; } async function delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } // Function to pass image to OCR async function passtoLENSOCR(imagePath) { try { const { default: Lens } = await import("chrome-lens-ocr"); const lens = new Lens(); var data = await lens.scanByFile(imagePath); data = data.segments.map((segment) => segment.text).join('\n'); return data; } catch (e) { console.log(e); await delay(1500); return passtoLENSOCR(imagePath); } } async function getGeminiResponse(textContent) { const prompt = ` solve the following question ${textContent} `; const model = getGeminiModel(0); const generatedContent = await model.generateContent([prompt]); return generatedContent.response.text(); } async function passtoGemini(imagePath) { const imagePart = fileToGenerativePart(imagePath, "image/jpeg"); const model = getGeminiModel(0); // const prompt = ` // Extract the entire question from the provided image and solve it. Return the result in the following JSON format, ensuring both the question and answer are formatted in Markdown for readability. Make sure the solution is accurate, clear, and concise. // if its a coding question, provide the code in the answer section // { // "question": "Full question text extracted from the image in Markdown format", // "response": "Comprehensive answer to the question in Markdown format", // "answer":"ans" // } // Ensure all key details from the image are included in the extracted question. // Solve the question clearly and provide a structured response using Markdown elements (like lists, code blocks, or math symbols if applicable). // Format the output correctly in the specified JSON structure. // `; const prompt = ` Extract the entire question from the provided image and solve it. if its a coding question, provide the code in the answer section in cpp language provide the answer in a format specific to whatsapp application provide a seperator "SPACESPACESPACE" space between the question and answer `; // const imageParts = [ // filePart1, // filePart2, // filePart3, // ]; //for multiple images const generatedContent = await model.generateContent([prompt, imagePart]); return generatedContent.response.text(); } //export functions module.exports = { passtoGemini, generateHTMLFile, passtoLENSOCR };