mirror of
https://github.com/hwanny1128/HGZero.git
synced 2025-12-06 11:26:25 +00:00
84 lines
3.0 KiB
Python
84 lines
3.0 KiB
Python
"""AI 요약 API 라우터"""
|
|
from fastapi import APIRouter, HTTPException
|
|
from app.models.summary import SummaryRequest, SummaryResponse
|
|
from app.services.claude_service import claude_service
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/generate", response_model=SummaryResponse)
|
|
async def generate_summary(request: SummaryRequest):
|
|
"""
|
|
텍스트 요약 생성 API
|
|
|
|
- **text**: 요약할 텍스트 (필수)
|
|
- **language**: 요약 언어 (ko: 한국어, en: 영어) - 기본값: ko
|
|
- **style**: 요약 스타일 (bullet: 불릿포인트, paragraph: 단락형) - 기본값: bullet
|
|
- **max_length**: 최대 요약 길이 (단어 수) - 선택사항
|
|
|
|
Returns:
|
|
요약 결과 (요약문, 핵심 포인트, 통계 정보)
|
|
"""
|
|
try:
|
|
# 입력 검증
|
|
if not request.text or len(request.text.strip()) == 0:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="요약할 텍스트가 비어있습니다."
|
|
)
|
|
|
|
if len(request.text) < 20:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="텍스트가 너무 짧습니다. 최소 20자 이상의 텍스트를 입력해주세요."
|
|
)
|
|
|
|
if len(request.text) > 10000:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="텍스트가 너무 깁니다. 최대 10,000자까지 요약 가능합니다."
|
|
)
|
|
|
|
# 언어 검증
|
|
if request.language not in ["ko", "en"]:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="지원하지 않는 언어입니다. 'ko' 또는 'en'만 사용 가능합니다."
|
|
)
|
|
|
|
# 스타일 검증
|
|
if request.style not in ["bullet", "paragraph"]:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="지원하지 않는 스타일입니다. 'bullet' 또는 'paragraph'만 사용 가능합니다."
|
|
)
|
|
|
|
# 최대 길이 검증
|
|
if request.max_length and request.max_length < 10:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="최대 길이는 10단어 이상이어야 합니다."
|
|
)
|
|
|
|
logger.info(f"요약 요청 - 텍스트 길이: {len(request.text)}, 언어: {request.language}, 스타일: {request.style}")
|
|
|
|
# Claude 서비스 호출
|
|
result = await claude_service.generate_summary(
|
|
text=request.text,
|
|
language=request.language,
|
|
style=request.style,
|
|
max_length=request.max_length
|
|
)
|
|
|
|
return SummaryResponse(**result)
|
|
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"요약 생성 중 오류 발생: {e}", exc_info=True)
|
|
raise HTTPException(
|
|
status_code=500,
|
|
detail=f"요약 생성 중 오류가 발생했습니다: {str(e)}"
|
|
) |