Files
paperlessmanager/paperless-backend/src/preprocessing/ocr.service.ts
T
bjoernpoettker 66aeab282c
Build and Push Multi-Platform Images / build-and-push (push) Successful in 19s
Revert "fix: resolve all ESLint errors in backend and frontend"
This reverts commit 07dfd7e840.
2026-06-16 16:19:11 +02:00

55 lines
1.6 KiB
TypeScript

import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import axios from 'axios';
@Injectable()
export class OcrService {
private readonly logger = new Logger(OcrService.name);
private readonly ollamaUrl: string;
private readonly ollamaModel: string;
constructor(private readonly configService: ConfigService) {
this.ollamaUrl = this.configService.get<string>(
'OLLAMA_URL',
'http://localhost:11434',
);
this.ollamaModel = this.configService.get<string>('OLLAMA_MODEL', 'llava');
}
/**
* Sendet ein Bild an Ollama Vision und erhält den Inhalt als Markdown.
*/
async extractTextAsMarkdown(imageBuffer: Buffer): Promise<string> {
const base64Image = imageBuffer.toString('base64');
const prompt = `Analysiere dieses Dokument und extrahiere den gesamten Text.
Gib den Text als sauberes Markdown zurück, behalte die Struktur bei (Überschriften, Tabellen, Listen).
Antworte nur mit dem extrahierten Markdown-Text, keine Erklärungen.`;
try {
const response = await axios.post(
`${this.ollamaUrl}/api/generate`,
{
model: this.ollamaModel,
prompt,
images: [base64Image],
stream: false,
options: {
temperature: 0.1,
},
},
{ timeout: 120000 },
);
const markdown = response.data.response?.trim() ?? '';
this.logger.log(
`OCR abgeschlossen: ${markdown.length} Zeichen extrahiert`,
);
return markdown;
} catch (error: any) {
this.logger.error(`Ollama OCR fehlgeschlagen: ${error.message}`);
throw error;
}
}
}