mirror of
https://github.com/escalante29/healthy-fit.git
synced 2026-03-21 13:48:46 +01:00
Add AI-powered nutrition and plan modules
Introduces DSPy-based nutrition and plan generation modules, including image analysis for nutritional info and personalized diet/exercise plans. Adds new API endpoints for health metrics/goals, nutrition image analysis, and plan management. Updates models, schemas, and backend structure to support these features, and includes initial training data and configuration for prompt optimization.
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
from fastapi import APIRouter
|
||||
from app.api.v1.endpoints import users, login, nutrition, health
|
||||
|
||||
from app.api.v1.endpoints import health, login, nutrition, plans, users
|
||||
|
||||
api_router = APIRouter()
|
||||
api_router.include_router(login.router, tags=["login"])
|
||||
api_router.include_router(users.router, prefix="/users", tags=["users"])
|
||||
api_router.include_router(nutrition.router, prefix="/nutrition", tags=["nutrition"])
|
||||
api_router.include_router(health.router, prefix="/health", tags=["health"])
|
||||
api_router.include_router(plans.router, prefix="/plans", tags=["plans"])
|
||||
|
||||
@@ -1,35 +1,80 @@
|
||||
from datetime import datetime
|
||||
from typing import Any, List
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlmodel import Session, select
|
||||
from app.api import deps
|
||||
from app.models.health import HealthMetric
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from pydantic import BaseModel
|
||||
from sqlmodel import Session, select
|
||||
|
||||
from app.api import deps
|
||||
from app.models.health import HealthGoal, HealthMetric
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class HealthMetricCreate(BaseModel):
|
||||
metric_type: str
|
||||
value: float
|
||||
unit: str
|
||||
user_id: int # TODO: remove when auth is fully integrated
|
||||
|
||||
@router.post("/", response_model=HealthMetric)
|
||||
|
||||
class HealthGoalCreate(BaseModel):
|
||||
goal_type: str
|
||||
target_value: float
|
||||
target_date: datetime | None = None
|
||||
|
||||
|
||||
@router.post("/metrics", response_model=HealthMetric)
|
||||
def create_metric(
|
||||
*,
|
||||
session: Session = Depends(deps.get_session),
|
||||
current_user: deps.CurrentUser,
|
||||
metric_in: HealthMetricCreate,
|
||||
) -> Any:
|
||||
metric = HealthMetric(metric_type=metric_in.metric_type, value=metric_in.value, unit=metric_in.unit, user_id=metric_in.user_id)
|
||||
metric = HealthMetric(
|
||||
metric_type=metric_in.metric_type, value=metric_in.value, unit=metric_in.unit, user_id=current_user.id
|
||||
)
|
||||
session.add(metric)
|
||||
session.commit()
|
||||
session.refresh(metric)
|
||||
return metric
|
||||
|
||||
@router.get("/{user_id}", response_model=List[HealthMetric])
|
||||
|
||||
@router.get("/metrics", response_model=List[HealthMetric])
|
||||
def read_metrics(
|
||||
user_id: int,
|
||||
current_user: deps.CurrentUser,
|
||||
session: Session = Depends(deps.get_session),
|
||||
) -> Any:
|
||||
statement = select(HealthMetric).where(HealthMetric.user_id == user_id)
|
||||
statement = (
|
||||
select(HealthMetric).where(HealthMetric.user_id == current_user.id).order_by(HealthMetric.timestamp.desc())
|
||||
)
|
||||
metrics = session.exec(statement).all()
|
||||
return metrics
|
||||
|
||||
|
||||
@router.post("/goals", response_model=HealthGoal)
|
||||
def create_goal(
|
||||
*,
|
||||
session: Session = Depends(deps.get_session),
|
||||
current_user: deps.CurrentUser,
|
||||
goal_in: HealthGoalCreate,
|
||||
) -> Any:
|
||||
goal = HealthGoal(
|
||||
goal_type=goal_in.goal_type,
|
||||
target_value=goal_in.target_value,
|
||||
target_date=goal_in.target_date,
|
||||
user_id=current_user.id,
|
||||
)
|
||||
session.add(goal)
|
||||
session.commit()
|
||||
session.refresh(goal)
|
||||
return goal
|
||||
|
||||
|
||||
@router.get("/goals", response_model=List[HealthGoal])
|
||||
def read_goals(
|
||||
current_user: deps.CurrentUser,
|
||||
session: Session = Depends(deps.get_session),
|
||||
) -> Any:
|
||||
statement = select(HealthGoal).where(HealthGoal.user_id == current_user.id)
|
||||
goals = session.exec(statement).all()
|
||||
return goals
|
||||
|
||||
@@ -1,35 +1,34 @@
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from sqlmodel import Session, select
|
||||
|
||||
from app.api import deps
|
||||
from app.core import security
|
||||
from app.config import settings
|
||||
from app.core import security
|
||||
from app.models.user import User
|
||||
from app.schemas.token import Token
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/login/access-token", response_model=Token)
|
||||
def login_access_token(
|
||||
session: Session = Depends(deps.get_session),
|
||||
form_data: OAuth2PasswordRequestForm = Depends()
|
||||
session: Session = Depends(deps.get_session), form_data: OAuth2PasswordRequestForm = Depends()
|
||||
) -> Any:
|
||||
"""
|
||||
OAuth2 compatible token login, get an access token for future requests
|
||||
"""
|
||||
statement = select(User).where(User.email == form_data.username)
|
||||
user = session.exec(statement).first()
|
||||
|
||||
|
||||
if not user or not security.verify_password(form_data.password, user.password_hash):
|
||||
raise HTTPException(status_code=400, detail="Incorrect email or password")
|
||||
|
||||
|
||||
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
return {
|
||||
"access_token": security.create_access_token(
|
||||
user.id, expires_delta=access_token_expires
|
||||
),
|
||||
"access_token": security.create_access_token(user.id, expires_delta=access_token_expires),
|
||||
"token_type": "bearer",
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
import litellm
|
||||
import dspy
|
||||
from typing import Any
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
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.ai.nutrition import nutrition_module, NutritionalInfo
|
||||
from app.core.security import create_access_token # Just ensuring we have auth imports if needed later
|
||||
from app.models.user import User
|
||||
from app.models.food import FoodLog # Added FoodItem
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class AnalyzeRequest(BaseModel):
|
||||
description: str
|
||||
|
||||
from app.models.food import FoodLog, FoodItem
|
||||
from app.api.deps import get_session
|
||||
from app.core.security import get_password_hash # Not needed
|
||||
from app.config import settings
|
||||
|
||||
@router.post("/analyze", response_model=NutritionalInfo)
|
||||
def analyze_food(
|
||||
@@ -30,6 +30,24 @@ def analyze_food(
|
||||
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(
|
||||
*,
|
||||
|
||||
67
backend/app/api/v1/endpoints/plans.py
Normal file
67
backend/app/api/v1/endpoints/plans.py
Normal file
@@ -0,0 +1,67 @@
|
||||
from typing import Any, List
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from sqlmodel import Session, select
|
||||
|
||||
from app.ai.plans import plan_module
|
||||
from app.api import deps
|
||||
from app.models.plan import Plan
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class PlanRequest(BaseModel):
|
||||
goal: str
|
||||
user_details: str # e.g., "Male, 30, 80kg"
|
||||
|
||||
|
||||
@router.post("/generate", response_model=Plan)
|
||||
def generate_plan(
|
||||
*,
|
||||
current_user: deps.CurrentUser,
|
||||
request: PlanRequest,
|
||||
session: Session = Depends(deps.get_session),
|
||||
) -> Any:
|
||||
"""
|
||||
Generate a new diet/exercise plan using AI.
|
||||
"""
|
||||
try:
|
||||
# Generate plan using DSPy
|
||||
generated = plan_module(user_profile=request.user_details, goal=request.goal)
|
||||
|
||||
# Determine content string (markdown representation)
|
||||
content_md = (
|
||||
f"# {generated.plan.title}\n\n{generated.plan.summary}\n\n## Diet\n"
|
||||
+ "\n".join([f"- {item}" for item in generated.plan.diet_plan])
|
||||
+ "\n\n## Exercise\n"
|
||||
+ "\n".join([f"- {item}" for item in generated.plan.exercise_plan])
|
||||
+ "\n\n## Tips\n"
|
||||
+ "\n".join([f"- {item}" for item in generated.plan.tips])
|
||||
)
|
||||
|
||||
plan = Plan(
|
||||
user_id=current_user.id,
|
||||
goal=request.goal,
|
||||
content=content_md,
|
||||
structured_content=generated.plan.model_dump(),
|
||||
)
|
||||
session.add(plan)
|
||||
session.commit()
|
||||
session.refresh(plan)
|
||||
return plan
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/", response_model=List[Plan])
|
||||
def read_plans(
|
||||
current_user: deps.CurrentUser,
|
||||
session: Session = Depends(deps.get_session),
|
||||
) -> Any:
|
||||
"""
|
||||
Get all plans for the current user.
|
||||
"""
|
||||
statement = select(Plan).where(Plan.user_id == current_user.id).order_by(Plan.created_at.desc())
|
||||
plans = session.exec(statement).all()
|
||||
return plans
|
||||
@@ -1,4 +1,5 @@
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlmodel import Session, select
|
||||
|
||||
@@ -9,6 +10,7 @@ from app.schemas.user import UserCreate, UserRead
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/", response_model=UserRead)
|
||||
def create_user(
|
||||
*,
|
||||
@@ -24,7 +26,7 @@ def create_user(
|
||||
status_code=400,
|
||||
detail="The user with this email already exists in the system",
|
||||
)
|
||||
|
||||
|
||||
user = User(
|
||||
email=user_in.email,
|
||||
username=user_in.username,
|
||||
|
||||
Reference in New Issue
Block a user