UNPKG

trace.ai-cli

Version:

A powerful AI-powered CLI tool

66 lines (59 loc) 2.61 kB
const fs = require('fs').promises; const fetch = require('node-fetch'); const { encryptData, decryptData } = require('../utils/encryption'); const { getMimeType } = require('../utils/fileUtils'); async function convertImageToBase64(imagePath) { try { const imageBuffer = await fs.readFile(imagePath); const base64 = imageBuffer.toString('base64'); const mimeType = getMimeType(imagePath); return { base64, mimeType }; } catch (error) { throw new Error(`Failed to read image: ${error.message}`); } } async function extractTextFromImage(imagePath, question = '') { try { const { base64, mimeType } = await convertImageToBase64(imagePath); const visionModels = ['kimi', 'mvrk', 'gma3', 'qvl72', 'llama-vision']; const modelRequests = visionModels.map(model => fetch('https://traceai.dukeindustries7.workers.dev/', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: encryptData({ a: model, q: 'Extract and return only the text from this image. Format it naturally.', r: [], i: [{ inlineData: { data: base64, mimeType } }], c: '' }) }) .then(res => res.text()) .then(decryptData) ); const responses = await Promise.all(modelRequests); const responseTexts = responses.map(r => r.text); const finalResponse = await fetch('https://traceai.dukeindustries7.workers.dev/', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: encryptData({ a: 'gfinal', q: question ? `First extract and combine the text from this image, then answer this question about the extracted text: ${question}` : 'Combine these extracted texts from an image into a single, coherent, and naturally formatted text. Remove duplicates and ensure clarity.', r: responseTexts, i: [{ inlineData: { data: base64, mimeType } }], c: '' }) }); const encryptedResult = await finalResponse.text(); const decryptedResult = decryptData(encryptedResult); return decryptedResult.text || 'Unable to analyze image' } catch (error) { console.error('❌ Error analyzing image:', error); throw error; } } module.exports = { extractTextFromImage };