mirror of
https://github.com/escalante29/healthy-fit.git
synced 2026-03-21 12:28: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:
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
|
||||
Reference in New Issue
Block a user