File size: 7,587 Bytes
6b18c86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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 += `<button class="tablinks" onclick="openTab(event, '${tabId}')">${tabName}</button>`;

        // Build the content for each tab
        tabsContent += `<div id="${tabId}" class="tabcontent">${htmlContentConverted}</div>`;
    });

    // Assemble the final HTML content
    const htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Tabbed Content</title>
    <!-- Tailwind CSS CDN -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- MathJax CDN -->
    <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
    <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    <style>
        /* Style the tab links */
        .tablinks {
            @apply bg-gray-200 float-left border-none outline-none cursor-pointer py-2 px-4 transition duration-300 text-lg;
        }
        .tablinks:hover {
            @apply bg-gray-300;
        }
        .tabcontent {
            display: none;
            padding: 1rem;
            border-top: none;
        }
        .tabcontent {
            animation: fadeEffect 1s;
        }
        @keyframes fadeEffect {
            from {opacity: 0;}
            to {opacity: 1;}
        }
    </style>
</head>
<body class="font-sans antialiased">

<div class="tab flex">
    ${tabsNav}
</div>
<div class="tab-contents">
    ${tabsContent}
</div>

<script>
function openTab(evt, tabId) {
    var i, tabcontent, tablinks;
    tabcontent = document.getElementsByClassName("tabcontent");
    for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
    }
    tablinks = document.getElementsByClassName("tablinks");
    for (i = 0; i < tablinks.length; i++) {
        tablinks[i].classList.remove("bg-blue-500", "text-white");
    }
    document.getElementById(tabId).style.display = "block";
    evt.currentTarget.classList.add("bg-blue-500", "text-white");
}

// Open the first tab by default
document.addEventListener("DOMContentLoaded", function() {
    document.querySelector('.tablinks').click();
});
</script>
</body>
</html>
`;

    // 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 };