Skip to content

Commit

Permalink
feat: refactor models
Browse files Browse the repository at this point in the history
  • Loading branch information
sspzoa committed Oct 11, 2024
1 parent 87f1e1d commit 58d9e05
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.12.5-alpine
FROM python:3.12.7-alpine

WORKDIR /code

Expand Down
6 changes: 1 addition & 5 deletions app/models/food/ingredient_detect_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ class IngredientDetectRequest(BaseModel):
image: UploadFile


class DetectedIngredient(BaseModel):
ingredient_name: str


class IngredientDetectResponse(BaseModel):
ingredients: List[DetectedIngredient]
ingredients: List[str]


class NoIngredientsFoundResponse(BaseModel):
Expand Down
4 changes: 0 additions & 4 deletions app/models/recipe/cooking_step_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ class CookingStep(BaseModel):
recipe_id: str
step_number: int
description: str
duration: str
tools_needed: List[str]
ingredients_used: List[str]
tips: str


class CookingStepResponse(BaseModel):
Expand Down
14 changes: 0 additions & 14 deletions app/models/recipe/ingredient_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,9 @@ class IngredientRequest(BaseModel):
ingredient_name: str


class NutritionalInfo(BaseModel):
calories: int
protein: float
carbohydrates: float
fat: float
fiber: float
vitamins: str
minerals: str


class Ingredient(BaseModel):
name: str
description: str
category: str
nutritional_info: NutritionalInfo
storage_tips: str
culinary_uses: List[str]


class IngredientResponse(BaseModel):
Expand Down
8 changes: 1 addition & 7 deletions app/models/recipe/recipe_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,10 @@ class Nutrition(BaseModel):
class Recipe(BaseModel):
name: str
description: str
servings: int
prepTime: str
cookTime: str
totalTime: str
difficulty: str
nutrition: Nutrition
ingredients: List[Ingredient]
instructions: List[Instruction]
nutrition: Nutrition
tags: List[str]
source: str


class RecipeRequest(BaseModel):
Expand Down
22 changes: 9 additions & 13 deletions app/routes/food/ingredient_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

from app.config import client
from app.models.error_models import ErrorResponse
from app.models.food.ingredient_detect_models import IngredientDetectResponse, DetectedIngredient, \
NoIngredientsFoundResponse
from app.models.food.ingredient_detect_models import IngredientDetectResponse, NoIngredientsFoundResponse

router = APIRouter()



@router.post("/ingredient_detect", tags=["Food"], response_model=IngredientDetectResponse,
@router.post("/ingredient_detect", tags=["Food"],
response_model=IngredientDetectResponse,
responses={400: {"model": ErrorResponse}, 404: {"model": NoIngredientsFoundResponse}})
async def ingredient_detect(image: UploadFile = File(...)):
"""
Expand All @@ -32,9 +30,9 @@ async def ingredient_detect(image: UploadFile = File(...)):
{{
"ingredients": [
{{"ingredient_name": "식재료1"}},
{{"ingredient_name": "식재료2"}},
{{"ingredient_name": "식재료3"}}
"식재료1",
"식재료2",
"식재료3"
]
}}
Expand Down Expand Up @@ -67,19 +65,17 @@ async def ingredient_detect(image: UploadFile = File(...)):

try:
ingredient_detect_json = json.loads(ingredient_detect_response.choices[0].message.content)
detected_ingredients = ingredient_detect_json.get("ingredients", [])
except json.JSONDecodeError:
raise HTTPException(status_code=400, detail="생성된 식재료 정보를 JSON으로 파싱할 수 없습니다.")

detected_ingredients = [DetectedIngredient(**ingredient) for ingredient in
ingredient_detect_json["ingredients"]]

if not detected_ingredients:
return NoIngredientsFoundResponse(status_code=404)
return NoIngredientsFoundResponse()

return IngredientDetectResponse(ingredients=detected_ingredients)

except HTTPException as http_ex:
raise http_ex
except Exception as e:
logging.error(f"Unexpected error: {str(e)}")
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
4 changes: 0 additions & 4 deletions app/routes/recipe/cooking_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ async def get_or_create_cooking_step_info(request: CookingStepRequest):
"recipe_id": "레시피 ID",
"step_number": 단계 번호,
"description": "조리 과정에 대한 상세한 설명",
"duration": "예상 소요 시간",
"tools_needed": ["도구1", "도구2", "도구3"],
"ingredients_used": ["재료1", "재료2", "재료3"],
"tips": "이 단계를 위한 요리 팁"
}}
레시피 ID: {request.recipe_id}
Expand Down
14 changes: 1 addition & 13 deletions app/routes/recipe/ingredient.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,7 @@ async def get_or_create_ingredient(request: IngredientRequest):
{{
"name": "식재료 이름",
"description": "식재료에 대한 간단한 설명",
"category": "식재료 카테고리",
"nutritional_info": {{
"calories": 100g당 칼로리(정수),
"protein": 단백질(g),
"carbohydrates": 탄수화물(g),
"fat": 지방(g),
"fiber": 식이섬유(g),
"vitamins": "주요 비타민",
"minerals": "주요 미네랄"
}},
"storage_tips": "보관 방법",
"culinary_uses": ["요리법1", "요리법2", "요리법3"]
"description": "식재료에 대한 설명",
}}
식재료 이름: {request.ingredient_name}
Expand Down
18 changes: 6 additions & 12 deletions app/routes/recipe/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ async def get_or_create_recipe(request: RecipeRequest):
{{
"name": "레시피 이름",
"description": "레시피에 대한 간단한 설명",
"servings": 인분 수(정수),
"prepTime": "준비 시간",
"cookTime": "조리 시간",
"totalTime": "총 소요 시간",
"difficulty": "난이도",
"nutrition": {{
"calories": 칼로리(정수),
"protein": "단백질(g)",
"carbohydrates": "탄수화물(g)",
"fat": "지방(g)"
}},
"ingredients": [
{{
"name": "재료 이름",
Expand All @@ -50,14 +52,6 @@ async def get_or_create_recipe(request: RecipeRequest):
"description": "단계 설명"
}}
],
"nutrition": {{
"calories": 칼로리(정수),
"protein": "단백질(g)",
"carbohydrates": "탄수화물(g)",
"fat": "지방(g)"
}},
"tags": ["태그1", "태그2", "태그3"],
"source": "출처 또는 원작자"
}}
음식 이름: {request.food_name}
Expand Down

0 comments on commit 58d9e05

Please sign in to comment.