Skip to content

Commit

Permalink
풀 리퀘스트 병합 #4
Browse files Browse the repository at this point in the history
andrew
  • Loading branch information
whdms2008 authored Sep 6, 2024
2 parents 8f8db0f + eb0371c commit c4d9352
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions app/router/analysis_router.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import json
import os
from idlelib.iomenu import encoding
from fastapi import HTTPException
from fastapi import APIRouter
from pydantic import BaseModel
Expand All @@ -25,13 +24,15 @@ class Diary(BaseModel):
except FileNotFoundError:
emotionTypeDescriptions = {}
print("Error : emotionTypeDescriptions.json 파일을 찾을 수 없습니다.")
raise HTTPException(status_code=400, detail="emotionTypeDescriptions.json 파일을 찾을 수 없습니다.")

try:
with open(os.path.join(data_dir, "typeDescription.json"), "r", encoding="utf-8") as f:
typeDescriptions = json.load(f)
except FileNotFoundError:
typeDescriptions = {}
print("Error : typeDescription.json 파일을 찾을 수 없습니다.")
raise HTTPException(status_code=400, detail="typeDescription.json 파일을 찾을 수 없습니다.")


def get_default_response():
return {
Expand All @@ -52,20 +53,22 @@ def get_default_response():
"happy": {"per": 0}
}


def get_default_type_data():
return {
"type": "N", # Neutral
"per": 100,
"desc": "분석할 수 없습니다."
}


@router.post("/analysis")
async def analysis_router(diary: Diary) -> JSONResponse:
content = diary.content
if content is None:
return JSONResponse({"error": "본문이 비어있습니다.", "content": content}, status_code=400)
try:
response = json.loads(analysis(content))
print(response)

if not response or "rjmd" not in response:
# 기본값 설정
Expand All @@ -88,19 +91,15 @@ async def analysis_router(diary: Diary) -> JSONResponse:
raise HTTPException(status_code=500, detail="분석 결과를 JSON으로 파싱하는데 실패했습니다.")
except Exception as e:
error_message = str(e)
print(error_message)
if "Message content must be non-empty" in error_message:
raise HTTPException(status_code=400, detail="분석할 내용이 비어 있습니다. 유효한 텍스트를 입력해 주세요.")
elif "invalid_request_error" in error_message:
raise HTTPException(status_code=400, detail="잘못된 요청입니다. 입력 내용을 확인해 주세요.")
else:
raise HTTPException(status_code=500, detail=f"일기 분석 중 오류가 발생했습니다: {error_message}")
response_data = JSONResponse(

return JSONResponse(
content=response,
media_type="application/json",
headers={"Content-Type": "application/json; charset=utf-8"}, status_code=200
)
decoded_string = json.dumps(response_data.body.decode('utf-8'))
print(decoded_string)

return response_data

0 comments on commit c4d9352

Please sign in to comment.