|
| 1 | +# BeFit - Full-Stack AI-Powered Fitness Tracking Platform |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +BeFit is a comprehensive fitness tracking platform that combines artificial intelligence with intuitive user interfaces to help users achieve their fitness goals. The platform provides natural language processing for food and exercise logging, personalized calorie and macro tracking, water intake management, and weight monitoring. Users can authenticate securely using JWT tokens or Google OAuth and access a modern dashboard to visualize their fitness progress. |
| 6 | + |
| 7 | +## Folder Structure |
| 8 | + |
| 9 | +``` |
| 10 | +BeFit/ |
| 11 | +├── backend/ |
| 12 | +│ ├── auth_service/ |
| 13 | +│ ├── profile_service/ |
| 14 | +│ ├── goal_service/ |
| 15 | +│ ├── llm_service/ |
| 16 | +│ ├── food_or_workout_log_service/ |
| 17 | +│ ├── water_service/ |
| 18 | +│ ├── weight_service/ |
| 19 | +│ ├── database/ |
| 20 | +│ ├── main.py |
| 21 | +│ ├── database.py |
| 22 | +│ └── requirements.txt |
| 23 | +├── frontend/ |
| 24 | +│ ├── src/ |
| 25 | +│ ├── public/ |
| 26 | +│ ├── package.json |
| 27 | +│ └── vite.config.ts |
| 28 | +└── tests/ |
| 29 | +``` |
| 30 | + |
| 31 | +## Backend Overview |
| 32 | + |
| 33 | +The backend is built with FastAPI, a modern Python web framework for building APIs. It uses SQLAlchemy as the ORM and PostgreSQL as the database. The architecture follows a service-based modular pattern, where each feature is encapsulated in its own service with dedicated API routes, data models, and business logic. |
| 34 | + |
| 35 | +### Core Components |
| 36 | + |
| 37 | +- FastAPI Application: Main application entry point |
| 38 | +- Database Layer: SQLAlchemy ORM models and database configuration |
| 39 | +- Service Layer: Modular services for specific business domains |
| 40 | +- Authentication: JWT and OAuth2 security mechanisms |
| 41 | + |
| 42 | +## Services |
| 43 | + |
| 44 | +### Auth Service |
| 45 | + |
| 46 | +Handles user authentication and authorization. |
| 47 | + |
| 48 | +Features: |
| 49 | +- User registration with email and password |
| 50 | +- Login with JWT token generation |
| 51 | +- Token refresh mechanism |
| 52 | +- Logout functionality |
| 53 | +- Google OAuth integration for third-party authentication |
| 54 | +- Password hashing and security best practices |
| 55 | + |
| 56 | +### Profile Service |
| 57 | + |
| 58 | +Manages user profile information and physical attributes. |
| 59 | + |
| 60 | +Features: |
| 61 | +- User profile setup with initial data |
| 62 | +- Store and retrieve age, height, weight, gender, and activity level |
| 63 | +- Support profile updates for goal recalculation |
| 64 | + |
| 65 | +### Goal Service |
| 66 | + |
| 67 | +Calculates and manages fitness goals based on user profile. |
| 68 | + |
| 69 | +Features: |
| 70 | +- Calculate Basal Metabolic Rate (BMR) using user profile data |
| 71 | +- Calculate Total Daily Energy Expenditure (TDEE) based on activity level |
| 72 | +- Set personalized daily calorie and macronutrient targets |
| 73 | +- Support goal adjustments based on target date and deficit/surplus |
| 74 | + |
| 75 | +### LLM Service |
| 76 | + |
| 77 | +Core artificial intelligence component using LangGraph and Groq LLM. |
| 78 | + |
| 79 | +Features: |
| 80 | +- Multi-agent workflow for intelligent logging |
| 81 | +- Classifier Agent: Determines if user input is food or exercise-related |
| 82 | +- Food Parser Agent: Extracts calorie and macronutrient information |
| 83 | +- Exercise Parser Agent: Calculates calories burned from exercise description |
| 84 | +- Natural language processing for user-friendly logging |
| 85 | + |
| 86 | +### Food or Workout Log Service |
| 87 | + |
| 88 | +Persists and manages logged food and exercise data. |
| 89 | + |
| 90 | +Features: |
| 91 | +- Store food logs with calorie and macro information |
| 92 | +- Store exercise logs with calorie burn information |
| 93 | +- Update user's daily calorie and macro consumption |
| 94 | +- Exercise logs increase calorie deficit only, macros remain unchanged |
| 95 | +- Food logs decrease available calories and macros |
| 96 | +- Prevent exceeding daily macro and calorie targets |
| 97 | + |
| 98 | +### Water Service |
| 99 | + |
| 100 | +Tracks daily water intake. |
| 101 | + |
| 102 | +Features: |
| 103 | +- Set daily water consumption goals |
| 104 | +- Log individual glasses or amounts of water |
| 105 | +- Track daily water intake progress |
| 106 | +- Retrieve daily water statistics |
| 107 | + |
| 108 | +### Weight Service |
| 109 | + |
| 110 | +Monitors weight changes over time. |
| 111 | + |
| 112 | +Features: |
| 113 | +- Log weight entries with timestamp |
| 114 | +- Retrieve weight history for analysis and trending |
| 115 | +- Support weight tracking for goal progress |
| 116 | + |
| 117 | +## API Routes |
| 118 | + |
| 119 | +All routes except authentication endpoints require JWT authentication. |
| 120 | + |
| 121 | +### Authentication Routes |
| 122 | +- POST /auth/register - User registration |
| 123 | +- POST /auth/login - User login |
| 124 | +- POST /auth/refresh - Refresh JWT token |
| 125 | +- POST /auth/logout - User logout |
| 126 | +- POST /auth/google - Google OAuth authentication |
| 127 | + |
| 128 | +### Profile Routes |
| 129 | +- POST /profile/setup - Initial profile setup |
| 130 | +- GET /profile/me - Get current user profile |
| 131 | + |
| 132 | +### Goal Routes |
| 133 | +- POST /goals/set - Set personalized fitness goals |
| 134 | +- GET /goals/me - Get current user goals |
| 135 | + |
| 136 | +### LLM Routes |
| 137 | +- POST /llm/log - Log food or exercise using natural language |
| 138 | + |
| 139 | +### Food Routes |
| 140 | +- GET /food/today - Get today's food logs |
| 141 | + |
| 142 | +### Water Routes |
| 143 | +- POST /water/goal - Set daily water goal |
| 144 | +- POST /water/add-glass - Log water consumption |
| 145 | +- GET /water/today - Get today's water statistics |
| 146 | + |
| 147 | +### Weight Routes |
| 148 | +- POST /weight/log - Log weight entry |
| 149 | +- GET /weight/history - Get weight history |
| 150 | + |
| 151 | +## API Security |
| 152 | + |
| 153 | +All API endpoints except authentication are protected with JWT bearer token authentication. JWT tokens are issued upon successful login or registration and must be included in the Authorization header of subsequent requests. |
| 154 | + |
| 155 | +Security mechanisms: |
| 156 | +- JWT token-based authentication for stateless requests |
| 157 | +- Refresh token rotation for extended sessions |
| 158 | +- OAuth2 integration for secure third-party authentication |
| 159 | +- Password hashing using bcrypt or similar algorithms |
| 160 | +- CORS configuration for cross-origin requests |
| 161 | +- Database connection security and parameterized queries |
| 162 | + |
| 163 | +## Frontend Overview |
| 164 | + |
| 165 | +The frontend is a modern, responsive web application built with React or Next.js. It provides a user-friendly dashboard for accessing all platform features. |
| 166 | + |
| 167 | +Features: |
| 168 | +- User authentication interface with login and registration |
| 169 | +- Profile setup and management pages |
| 170 | +- Dashboard displaying current goals and progress |
| 171 | +- Food logging interface with natural language input |
| 172 | +- Exercise logging interface |
| 173 | +- Water intake tracking |
| 174 | +- Weight tracking and history visualization |
| 175 | +- Goal progress visualization and statistics |
| 176 | + |
| 177 | +## Tech Stack |
| 178 | + |
| 179 | +### Backend |
| 180 | +- FastAPI: Modern Python web framework |
| 181 | +- SQLAlchemy: SQL toolkit and ORM |
| 182 | +- PostgreSQL: Relational database |
| 183 | +- JWT: JSON Web Token authentication |
| 184 | +- OAuth2: Third-party authentication with Google |
| 185 | +- LangGraph: Agent workflow framework |
| 186 | +- Groq: LLM provider for natural language processing |
| 187 | +- Bcrypt: Password hashing library |
| 188 | +- Pydantic: Data validation using Python type annotations |
| 189 | + |
| 190 | +### Frontend |
| 191 | +- React or Next.js: JavaScript/TypeScript UI framework |
| 192 | +- Vite: Build tool and dev server |
| 193 | +- Tailwind CSS: Utility-first CSS framework |
| 194 | +- TypeScript: Type-safe JavaScript |
| 195 | + |
| 196 | +## Setup Instructions |
| 197 | + |
| 198 | +### Prerequisites |
| 199 | + |
| 200 | +- Python 3.11 or higher |
| 201 | +- PostgreSQL 12 or higher |
| 202 | +- Node.js 18 or higher |
| 203 | +- Bun or npm package manager |
| 204 | + |
| 205 | +### Backend Setup |
| 206 | + |
| 207 | +1. Navigate to the backend directory: |
| 208 | + cd backend |
| 209 | + |
| 210 | +2. Create and activate a virtual environment: |
| 211 | + python -m venv cal_env |
| 212 | + source cal_env/bin/activate # On Windows: cal_env\Scripts\activate |
| 213 | + |
| 214 | +3. Install dependencies: |
| 215 | + pip install -r requirements.txt |
| 216 | + |
| 217 | +4. Configure environment variables: |
| 218 | + - Create a .env file with database connection string |
| 219 | + - Add Google OAuth credentials from client-secret.json |
| 220 | + - Add Groq API key for LLM access |
| 221 | + |
| 222 | +5. Initialize the database: |
| 223 | + python -m backend.database |
| 224 | + |
| 225 | +6. Run the FastAPI server: |
| 226 | + python -m backend.main |
| 227 | + |
| 228 | +The API will be available at http://localhost:8000 |
| 229 | + |
| 230 | +### Frontend Setup |
| 231 | + |
| 232 | +1. Navigate to the frontend directory: |
| 233 | + cd frontend |
| 234 | + |
| 235 | +2. Install dependencies: |
| 236 | + bun install |
| 237 | + # or |
| 238 | + npm install |
| 239 | + |
| 240 | +3. Configure environment variables: |
| 241 | + - Create a .env.local file with backend API URL |
| 242 | + - Add Google OAuth client ID |
| 243 | + |
| 244 | +4. Run the development server: |
| 245 | + bun run dev |
| 246 | + # or |
| 247 | + npm run dev |
| 248 | + |
| 249 | +The application will be available at http://localhost:5173 |
| 250 | + |
| 251 | +## LLM Logging Flow |
| 252 | + |
| 253 | +The LLM service implements a multi-agent workflow for intelligent food and exercise logging: |
| 254 | + |
| 255 | +1. User Input: User submits natural language description of food or exercise |
| 256 | +2. Classification: Classifier agent determines input type (food or exercise) |
| 257 | +3. If Food: |
| 258 | + - Food Parser Agent: Extracts food items and quantities |
| 259 | + - Extracts calorie and macronutrient information |
| 260 | + - Returns structured food data |
| 261 | +4. If Exercise: |
| 262 | + - Exercise Parser Agent: Analyzes exercise description |
| 263 | + - Calculates estimated calories burned |
| 264 | + - Returns exercise data with calorie deficit |
| 265 | +5. Persistence: Food or Workout Log Service stores the extracted data |
| 266 | +6. Goal Update: User's daily calorie and macro targets are updated |
| 267 | +7. Response: API returns confirmation and updated goal status |
| 268 | + |
| 269 | +## Database Models |
| 270 | + |
| 271 | +- User: User account information and authentication |
| 272 | +- UserProfile: Physical attributes and activity level |
| 273 | +- UserGoal: Daily calorie and macro targets |
| 274 | +- FoodLog: Food consumption records with nutrition data |
| 275 | +- ExerciseLog: Exercise records with calorie burn information |
| 276 | +- WaterLog: Water consumption tracking |
| 277 | +- WeightLog: Weight entry records with timestamps |
| 278 | + |
| 279 | +## Future Improvements |
| 280 | + |
| 281 | +- Barcode scanning for food logging |
| 282 | +- Integration with wearable devices for activity tracking |
| 283 | +- Meal planning recommendations based on goals |
| 284 | +- Nutritionist consultation through AI |
| 285 | +- Social features for community challenges |
| 286 | +- Mobile application for iOS and Android |
| 287 | +- Advanced analytics and reporting |
| 288 | +- Dietary restriction and allergy management |
| 289 | +- Recipe suggestions based on macros and preferences |
| 290 | +- Workout plan generation and progression tracking |
| 291 | +- Integration with popular fitness apps |
| 292 | +- Voice-based logging for hands-free operation |
| 293 | +- Meal timing optimization for fitness goals |
| 294 | +- Supplement tracking and recommendations |
| 295 | + |
| 296 | +## Development |
| 297 | + |
| 298 | +To run tests: |
| 299 | +cd tests |
| 300 | +pytest test_api_health.py |
| 301 | + |
| 302 | +## Contributing |
| 303 | + |
| 304 | +Contributions are welcome. Please follow standard Git workflow with feature branches and pull requests. |
| 305 | + |
| 306 | +## License |
| 307 | + |
| 308 | +This project is provided as-is for educational and commercial purposes. |
0 commit comments