Initial commit with Email Import Wizard and Task Processor updates
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user