First commit
This commit is contained in:
41
router/deepseek_router.py
Normal file
41
router/deepseek_router.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, File, HTTPException, UploadFile
|
||||
|
||||
from services.ocr_engine import process_document
|
||||
|
||||
router = APIRouter(prefix="/ocr", tags=["OCR"])
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@router.post("", description="요청된 파일에서 Deepseek OCR을 수행하고 텍스트를 추출합니다.")
|
||||
async def perform_ocr(document: UploadFile = File(..., description="OCR을 수행할 PDF 또는 이미지 파일")):
|
||||
"""
|
||||
클라이언트로부터 받은 파일을 OCR 엔진에 전달하고, 추출된 텍스트를 반환합니다.
|
||||
- **document**: `multipart/form-data` 형식으로 전송된 파일.
|
||||
"""
|
||||
logger.info(f"'{document.filename}' 파일에 대한 OCR 요청 수신 (Content-Type: {document.content_type})")
|
||||
|
||||
try:
|
||||
file_content = await document.read()
|
||||
if not file_content:
|
||||
raise HTTPException(status_code=400, detail="파일 내용이 비어있습니다.")
|
||||
|
||||
# 모든 처리 로직을 OCR 엔진에 위임
|
||||
result = process_document(
|
||||
file_bytes=file_content,
|
||||
content_type=document.content_type,
|
||||
filename=document.filename,
|
||||
)
|
||||
return result
|
||||
|
||||
except (ValueError, TypeError) as e:
|
||||
# 엔진에서 발생한 예상된 오류 (e.g., 지원하지 않는 파일 형식)
|
||||
logger.warning(f"요청 처리 중 오류 발생: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
# 예상치 못한 서버 내부 오류
|
||||
logger.exception(f"OCR 처리 중 예상치 못한 오류 발생: {e}")
|
||||
raise HTTPException(status_code=500, detail=f"서버 내부 오류가 발생했습니다: {e}")
|
||||
finally:
|
||||
await document.close()
|
||||
Reference in New Issue
Block a user