mirror of
https://github.com/escalante29/healthy-fit.git
synced 2026-03-21 09:08:46 +01:00
Converted frontend codebase from JavaScript to TypeScript, including pages, components, and context. Added new layout and UI kit components. Updated backend user model and schemas to support profile fields (firstname, lastname, age, gender, height, weight, unit_preference) and added endpoints for reading/updating current user. Introduced food log listing endpoint and migration script for user table. Updated dependencies and build configs for TypeScript and Tailwind v4.
94 lines
2.5 KiB
Python
94 lines
2.5 KiB
Python
from typing import Any
|
|
|
|
import litellm
|
|
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
|
|
from pydantic import BaseModel
|
|
from sqlmodel import Session
|
|
|
|
from app.ai.nutrition import NutritionalInfo, analyze_nutrition_from_image, nutrition_module
|
|
from app.api import deps
|
|
from app.models.food import FoodLog # Added FoodItem
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
class AnalyzeRequest(BaseModel):
|
|
description: str
|
|
|
|
|
|
@router.post("/analyze", response_model=NutritionalInfo)
|
|
def analyze_food(
|
|
request: AnalyzeRequest,
|
|
) -> Any:
|
|
"""
|
|
Analyze food description and return nutritional info using DSPy.
|
|
"""
|
|
try:
|
|
result = nutrition_module(description=request.description)
|
|
return result.nutritional_info
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.post("/analyze/image", response_model=NutritionalInfo)
|
|
async def analyze_food_image(
|
|
file: UploadFile = File(...),
|
|
description: str = Form(""),
|
|
) -> Any:
|
|
"""
|
|
Analyze food image and return nutritional info.
|
|
"""
|
|
try:
|
|
contents = await file.read()
|
|
return analyze_nutrition_from_image(contents, description)
|
|
except litellm.exceptions.BadRequestError as e:
|
|
raise HTTPException(status_code=400, detail=f"Invalid image or request: {str(e)}")
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.post("/log", response_model=FoodLog)
|
|
def log_food(
|
|
*,
|
|
session: Session = Depends(deps.get_session),
|
|
nutrition_info: NutritionalInfo,
|
|
current_user: deps.CurrentUser,
|
|
) -> Any:
|
|
"""
|
|
Save food log to database.
|
|
"""
|
|
food_log = FoodLog(
|
|
user_id=current_user.id,
|
|
name=nutrition_info.name,
|
|
calories=nutrition_info.calories,
|
|
protein=nutrition_info.protein,
|
|
carbs=nutrition_info.carbs,
|
|
fats=nutrition_info.fats,
|
|
)
|
|
session.add(food_log)
|
|
session.commit()
|
|
session.refresh(food_log)
|
|
return food_log
|
|
|
|
|
|
@router.get("/logs", response_model=list[FoodLog])
|
|
def read_logs(
|
|
current_user: deps.CurrentUser,
|
|
session: Session = Depends(deps.get_session),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
) -> Any:
|
|
"""
|
|
Get food logs for current user.
|
|
"""
|
|
from sqlmodel import select
|
|
|
|
statement = (
|
|
select(FoodLog)
|
|
.where(FoodLog.user_id == current_user.id)
|
|
.order_by(FoodLog.timestamp.desc())
|
|
.offset(skip)
|
|
.limit(limit)
|
|
)
|
|
return session.exec(statement).all()
|