Spaces:
Configuration error
Configuration error
| ```javascript | |
| const express = require('express'); | |
| const router = express.Router(); | |
| const multer = require('multer'); | |
| const { createWorker } = require('tesseract.js'); | |
| const { pipeline } = require('stream/promises'); | |
| const FormData = require('form-data'); | |
| const fs = require('fs'); | |
| const axios = require('axios'); | |
| const upload = multer({ dest: 'uploads/' }); | |
| const worker = createWorker(); | |
| // Initialize Tesseract worker | |
| (async () => { | |
| await worker.load(); | |
| await worker.loadLanguage('eng'); | |
| await worker.initialize('eng'); | |
| })(); | |
| router.post('/analyze', upload.single('image'), async (req, res) => { | |
| try { | |
| // 1. Perform OCR on the image first | |
| const { data: { text } } = await worker.recognize(req.file.path); | |
| // 2. Send to LLM for analysis (using LLaMA.cpp as example) | |
| const form = new FormData(); | |
| form.append('image', fs.createReadStream(req.file.path)); | |
| form.append('text_context', text); | |
| const llmResponse = await axios.post('http://localhost:8080/analyze-image', form, { | |
| headers: form.getHeaders() | |
| }); | |
| // Clean up the uploaded file | |
| fs.unlinkSync(req.file.path); | |
| res.json({ | |
| description: llmResponse.data.description, | |
| items: llmResponse.data.items.map(item => ({ | |
| label: item.label, | |
| confidence: item.confidence | |
| })) | |
| }); | |
| } catch (error) { | |
| console.error('Error processing image:', error); | |
| res.status(500).json({ error: 'Error processing image' }); | |
| } | |
| }); | |
| module.exports = router; | |
| ``` |