690525:1541 ADR-028-228-migration-OCR #05 ocr-sidecar working
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
// Change Log
|
// Change Log
|
||||||
// - 2026-05-15: เพิ่ม OCR auto-detection service สำหรับ ADR-023A.
|
// - 2026-05-15: เพิ่ม OCR auto-detection service สำหรับ ADR-023A.
|
||||||
// - 2026-05-25: แก้ไข AggregateError (empty message) จาก axios โดย wrap เป็น Error พร้อม context ที่ชัดเจน.
|
// - 2026-05-25: แก้ไข AggregateError (empty message) จาก axios โดย wrap เป็น Error พร้อม context ที่ชัดเจน.
|
||||||
|
// - 2026-05-25: เพิ่ม path remapping (OCR_UPLOAD_BASE_PATH) เพื่อแปลง local upload path เป็น path ที่ sidecar เห็นผ่าน CIFS.
|
||||||
|
|
||||||
import { Injectable, Logger } from '@nestjs/common';
|
import { Injectable, Logger } from '@nestjs/common';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
@@ -28,6 +29,8 @@ export class OcrService {
|
|||||||
private readonly logger = new Logger(OcrService.name);
|
private readonly logger = new Logger(OcrService.name);
|
||||||
private readonly threshold: number;
|
private readonly threshold: number;
|
||||||
private readonly ocrApiUrl: string;
|
private readonly ocrApiUrl: string;
|
||||||
|
private readonly localUploadBase: string;
|
||||||
|
private readonly sidecarUploadBase: string;
|
||||||
|
|
||||||
constructor(private readonly configService: ConfigService) {
|
constructor(private readonly configService: ConfigService) {
|
||||||
this.threshold = this.configService.get<number>('OCR_CHAR_THRESHOLD', 100);
|
this.threshold = this.configService.get<number>('OCR_CHAR_THRESHOLD', 100);
|
||||||
@@ -35,6 +38,22 @@ export class OcrService {
|
|||||||
'OCR_API_URL',
|
'OCR_API_URL',
|
||||||
'http://localhost:8765'
|
'http://localhost:8765'
|
||||||
);
|
);
|
||||||
|
// path ที่ backend เห็น → path ที่ sidecar เห็น (ผ่าน CIFS mount)
|
||||||
|
this.localUploadBase = this.configService
|
||||||
|
.get<string>('UPLOAD_PERMANENT_DIR', '/app/uploads/permanent')
|
||||||
|
.replace(/\/permanent$/, '');
|
||||||
|
this.sidecarUploadBase = this.configService.get<string>(
|
||||||
|
'OCR_SIDECAR_UPLOAD_BASE',
|
||||||
|
'/mnt/uploads'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** แปลง local upload path เป็น path ที่ sidecar เห็นผ่าน CIFS mount */
|
||||||
|
private remapPath(localPath: string): string {
|
||||||
|
if (this.localUploadBase && localPath.startsWith(this.localUploadBase)) {
|
||||||
|
return localPath.replace(this.localUploadBase, this.sidecarUploadBase);
|
||||||
|
}
|
||||||
|
return localPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** ตรวจสอบ text layer ก่อนเลือก OCR slow path */
|
/** ตรวจสอบ text layer ก่อนเลือก OCR slow path */
|
||||||
@@ -54,9 +73,11 @@ export class OcrService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const sidecarPath = this.remapPath(input.pdfPath);
|
||||||
|
this.logger.debug(`OCR path remap: ${input.pdfPath} → ${sidecarPath}`);
|
||||||
const response = await axios.post<PaddleOcrResponse>(
|
const response = await axios.post<PaddleOcrResponse>(
|
||||||
`${this.ocrApiUrl}/ocr`,
|
`${this.ocrApiUrl}/ocr`,
|
||||||
{ pdfPath: input.pdfPath },
|
{ pdfPath: sidecarPath },
|
||||||
{ timeout: 90000 }
|
{ timeout: 90000 }
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ OLLAMA_URL=http://192.168.10.100:11434
|
|||||||
# PaddleOCR Sidecar (Admin Desktop Desk-5439 — ADR-023A)
|
# PaddleOCR Sidecar (Admin Desktop Desk-5439 — ADR-023A)
|
||||||
OCR_API_URL=http://192.168.10.100:8765
|
OCR_API_URL=http://192.168.10.100:8765
|
||||||
OCR_CHAR_THRESHOLD=100
|
OCR_CHAR_THRESHOLD=100
|
||||||
|
OCR_SIDECAR_UPLOAD_BASE=/mnt/uploads
|
||||||
|
|
||||||
# Thai preprocessing microservice (PyThaiNLP — Admin Desktop)
|
# Thai preprocessing microservice (PyThaiNLP — Admin Desktop)
|
||||||
THAI_PREPROCESS_URL=http://192.168.10.100:8765
|
THAI_PREPROCESS_URL=http://192.168.10.100:8765
|
||||||
|
|||||||
@@ -98,6 +98,7 @@ services:
|
|||||||
# --- PaddleOCR Sidecar (Desk-5439 — ADR-023A) ---
|
# --- PaddleOCR Sidecar (Desk-5439 — ADR-023A) ---
|
||||||
OCR_API_URL: ${OCR_API_URL:?OCR_API_URL required}
|
OCR_API_URL: ${OCR_API_URL:?OCR_API_URL required}
|
||||||
OCR_CHAR_THRESHOLD: ${OCR_CHAR_THRESHOLD:-100}
|
OCR_CHAR_THRESHOLD: ${OCR_CHAR_THRESHOLD:-100}
|
||||||
|
OCR_SIDECAR_UPLOAD_BASE: ${OCR_SIDECAR_UPLOAD_BASE:-/mnt/uploads}
|
||||||
# --- Numbering ---
|
# --- Numbering ---
|
||||||
NUMBERING_LOCK_TIMEOUT: '5000'
|
NUMBERING_LOCK_TIMEOUT: '5000'
|
||||||
NUMBERING_RESERVATION_TTL: '300'
|
NUMBERING_RESERVATION_TTL: '300'
|
||||||
|
|||||||
Reference in New Issue
Block a user