Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion genon/preprocessor/facade/intelligent_processor_law.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ def __init__(self):
'''
initialize Document Converter
'''
self.ocr_endpoint = "http://192.168.81.170:48080/ocr"
self.ocr_endpoint = "http://192.168.73.172:48080/ocr"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

OCR 엔드포인트 URL이 코드에 직접 하드코딩되어 있습니다. 이렇게 하면 개발, 스테이징, 운영 환경에 따라 코드를 수정해야 하므로 유지보수가 어렵고 잠재적인 보안 위험이 있습니다. os.environ.get() 등을 사용하여 환경 변수에서 URL을 읽어오도록 변경하는 것을 강력히 권장합니다.

Suggested change
self.ocr_endpoint = "http://192.168.73.172:48080/ocr"
self.ocr_endpoint = os.environ.get("OCR_ENDPOINT", "http://192.168.73.172:48080/ocr")

ocr_options = PaddleOcrOptions(
force_full_page_ocr=False,
lang=['korean'],
Expand Down
2 changes: 1 addition & 1 deletion genon/preprocessor/facade/intelligent_processor_ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ def __init__(self):
'''
initialize Document Converter
'''
self.ocr_endpoint = "http://192.168.81.170:48080/ocr"
self.ocr_endpoint = "http://192.168.73.172:48080/ocr"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

OCR 엔드포인트 URL이 코드에 직접 하드코딩되어 있습니다. 이렇게 하면 개발, 스테이징, 운영 환경에 따라 코드를 수정해야 하므로 유지보수가 어렵고 잠재적인 보안 위험이 있습니다. os.environ.get() 등을 사용하여 환경 변수에서 URL을 읽어오도록 변경하는 것을 강력히 권장합니다.

Suggested change
self.ocr_endpoint = "http://192.168.73.172:48080/ocr"
self.ocr_endpoint = os.environ.get("OCR_ENDPOINT", "http://192.168.73.172:48080/ocr")

ocr_options = PaddleOcrOptions(
force_full_page_ocr=False,
lang=['korean'],
Expand Down
79 changes: 54 additions & 25 deletions genon/preprocessor/facade/legacy/적재용(규정)v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,9 +793,11 @@ def __init__(self):
initialize Document Converter
'''

self.ocr_endpoint = "http://192.168.73.172:48080/ocr"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

OCR 엔드포인트 URL이 코드에 직접 하드코딩되어 있습니다. 이렇게 하면 개발, 스테이징, 운영 환경에 따라 코드를 수정해야 하므로 유지보수가 어렵고 잠재적인 보안 위험이 있습니다. os.environ.get() 등을 사용하여 환경 변수에서 URL을 읽어오도록 변경하는 것을 강력히 권장합니다.

Suggested change
self.ocr_endpoint = "http://192.168.73.172:48080/ocr"
self.ocr_endpoint = os.environ.get("OCR_ENDPOINT", "http://192.168.73.172:48080/ocr")

ocr_options = PaddleOcrOptions(
force_full_page_ocr=False,
lang=['korean'],
ocr_endpoint=self.ocr_endpoint,
text_score=0.3)

self.page_chunk_counts = defaultdict(int)
Expand Down Expand Up @@ -1185,20 +1187,54 @@ def ocr_all_table_cells(self, document: DoclingDocument, pdf_path) -> List[Dict[
Returns:
OCR이 완료된 문서의 DoclingDocument 객체
"""
try:
import fitz
import grpc
import docling.models.ocr_pb2 as ocr_pb2
import docling.models.ocr_pb2_grpc as ocr_pb2_grpc
import itertools
import fitz
import base64
import requests

def post_ocr_bytes(img_bytes: bytes, timeout=60) -> dict:
HEADERS = {"Accept": "application/json", "Content-Type": "application/json"}
payload = {"file": base64.b64encode(img_bytes).decode("ascii"), "fileType": 1, "visualize": False}
r = requests.post(self.ocr_endpoint, json=payload, headers=HEADERS, timeout=timeout)
if not r.ok:
# 진단에 도움되도록 본문 일부 출력
raise RuntimeError(f"OCR HTTP {r.status_code}: {r.text[:500]}")
return r.json()

def extract_ocr_fields(resp: dict):
"""
resp: 위와 같은 OCR 응답 JSON(dict)
return: (rec_texts, rec_scores, rec_boxes) — 모두 list
"""
if resp is None:
return [], [], []

# 최상위 상태 체크
if resp.get("errorCode") not in (0, None):
return [], [], []

ocr_results = (
resp.get("result", {})
.get("ocrResults", [])
)
if not ocr_results:
return [], [], []

pruned = (
ocr_results[0]
.get("prunedResult", {})
)
if not pruned:
return [], [], []

grpc_server_count = self.ocr_pipe_line_options.ocr_options.grpc_server_count
rec_texts = pruned.get("rec_texts", []) # list[str]
rec_scores = pruned.get("rec_scores", []) # list[float]
rec_boxes = pruned.get("rec_boxes", []) # list[[x1,y1,x2,y2]]

PORTS = [50051 + i for i in range(grpc_server_count)]
channels = [grpc.insecure_channel(f"localhost:{p}") for p in PORTS]
stubs = [(ocr_pb2_grpc.OCRServiceStub(ch), p) for ch, p in zip(channels, PORTS)]
rr = itertools.cycle(stubs)
# 길이 불일치 방어: 최소 길이에 맞춰 자르기
n = min(len(rec_texts), len(rec_scores), len(rec_boxes))
return rec_texts[:n], rec_scores[:n], rec_boxes[:n]

try:
doc = fitz.open(pdf_path)

for table_idx, table_item in enumerate(document.tables):
Expand All @@ -1217,7 +1253,7 @@ def ocr_all_table_cells(self, document: DoclingDocument, pdf_path) -> List[Dict[

for cell_idx, cell in enumerate(table_item.data.table_cells):

# # Provenance 정보에서 위치 정보 추출
# Provenance 정보에서 위치 정보 추출
if not table_item.prov:
continue

Expand Down Expand Up @@ -1249,23 +1285,16 @@ def ocr_all_table_cells(self, document: DoclingDocument, pdf_path) -> List[Dict[
pix = page.get_pixmap(matrix=mat, clip=cell_bbox)
img_data = pix.tobytes("png")

# gRPC 서버와 연결
# channel = grpc.insecure_channel('localhost:50051')
# stub = ocr_pb2_grpc.OCRServiceStub(channel)

# # OCR 요청: 이미지 데이터를 바이너리로 전송
# response = stub.PerformOCR(ocr_pb2.OCRRequest(image_data=img_data))

req = ocr_pb2.OCRRequest(image_data=img_data)
stub, port = next(rr) # 라운드 로빈 방식으로 스텁 선택
response = stub.PerformOCR(req)
result = post_ocr_bytes(img_data, timeout=60)
rec_texts, rec_scores, rec_boxes = extract_ocr_fields(result)

cell.text = ""
for result in response.results:
for t in rec_texts:
if len(cell.text) > 0:
cell.text += " "
cell.text += result.text if result else ""
except grpc.RpcError as e:
cell.text += t if t else ""
Comment on lines 1291 to +1295

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

현재 텍스트 조합 로직은 rec_texts에 빈 문자열이 있을 경우 불필요한 공백을 추가하는 버그가 있습니다. 예를 들어, ['a', '', 'b']가 입력되면 결과는 'a b'가 됩니다. " ".join(filter(None, rec_texts))를 사용하면 빈 문자열을 제외하고 하나의 공백으로 텍스트를 합칠 수 있어 더 간결하고 정확합니다.

Suggested change
cell.text = ""
for result in response.results:
for t in rec_texts:
if len(cell.text) > 0:
cell.text += " "
cell.text += result.text if result else ""
except grpc.RpcError as e:
cell.text += t if t else ""
cell.text = " ".join(filter(None, rec_texts))

except Exception as e:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Exception은 너무 광범위한 예외입니다. OCR 요청 실패 시 발생하는 requests.exceptions.RequestException이나 RuntimeError와 같이 더 구체적인 예외를 잡는 것이 좋습니다. 이렇게 하면 예상치 못한 다른 오류들이 숨겨지는 것을 방지할 수 있습니다.

Suggested change
except Exception as e:
except (requests.exceptions.RequestException, RuntimeError) as e:

print(f"OCR processing failed: {e}")
pass

return document
Expand Down