- unwind ai
- Posts
- Build a Personal Health and Fitness AI Agent using Google Gemini
Build a Personal Health and Fitness AI Agent using Google Gemini
Fully-functional AI agent app in just 50 lines of Python Code (step-by-step instructions)
Building AI applications that can understand and respond to specific user needs is a common challenge. While there are many tutorials on chatbots and Q&A systems, creating specialized agents that can work together to solve specific problems requires a different approach.
In this tutorial, we'll build a Personal Health & Fitness AI Agent that demonstrates how to create task-specific AI agents that collaborate effectively. Using Google Gemini and Phidata, we'll create a system where two specialized agents - one for diet and one for fitness - work together to generate personalized recommendations based on user inputs.
Phidata makes this multi-agent approach straightforward by providing a framework designed for building and coordinating AI agents. It handles the complexity of agent communication, memory management, and response generation, letting us focus on defining our agents' roles and behaviors.
What We’re Building
The AI Health & Fitness Planner is a personalized health and fitness Agent powered by Phidata's AI Agent framework. This app generates tailored dietary and fitness plans based on user inputs such as age, weight, height, activity level, dietary preferences, and fitness goals.
Features
Multi-agents: The app has two phidata agents that are specialists in giving Diet advice and Fitness/workout advice respectively.
Personalized Dietary Plans:
Generates detailed meal plans (breakfast, lunch, dinner, and snacks).
Includes important considerations like hydration, electrolytes, and fiber intake.
Supports various dietary preferences like Keto, Vegetarian, Low Carb, etc.
Personalized Fitness Plans:
Provides customized exercise routines based on fitness goals.
Covers warm-ups, main workouts, and cool-downs.
Includes actionable fitness tips and progress tracking advice.
Interactive Q&A: Allows users to ask follow-up questions about their plans.
Prerequisites
Before we begin, make sure you have:
Python installed on your machine (version 3.7 or higher is recommended)
Your Google Gemini API key (get it for free)
Basic familiarity with Python programming
A code editor of your choice (we recommend VS Code or PyCharm for their excellent Python support)
Step-by-Step Instructions
Setting Up the Environment
First, let's get our development environment ready:
Clone the GitHub repository:
git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
Go to the ai_health_fitness_agent folder:
cd ai_agent_tutorials/ai_health_fitness_agent
Install the required dependencies:
pip install -r requirements.txt
Get your API Key: Sign up for Google AI Studio account and get your API key.
Creating the Streamlit App
Let’s create our app. Create a new file health_agent.py
and add the following code:
Import necessary libraries:
• Streamlit for building the web app
• Phidata for building AI agents and tools
• Google Gemini as LLM
import streamlit as st
from phi.agent import Agent
from phi.model.google import Gemini
Set up the Streamlit interface with custom styling:
st.set_page_config(
page_title="AI Health & Fitness Planner",
page_icon="🏋️♂️",
layout="wide",
initial_sidebar_state="expanded"
)
st.markdown("""
<style>
.main { padding: 2rem; }
.stButton>button { width: 100%; }
</style>
""", unsafe_allow_html=True)
Create the Dietary Expert Agent:
dietary_agent = Agent(
name="Dietary Expert",
role="Provides personalized dietary recommendations",
model=gemini_model,
instructions=[
"Consider the user's input, including dietary restrictions and preferences.",
"Suggest a detailed meal plan for the day, including breakfast, lunch, dinner, and snacks.",
"Provide a brief explanation of why the plan is suited to the user's goals.",
"Focus on clarity, coherence, and quality of the recommendations.",
]
)
Create the Fitness Expert Agent:
fitness_agent = Agent(
name="Fitness Expert",
role="Provides personalized fitness recommendations",
model=gemini_model,
instructions=[
"Provide exercises tailored to the user's goals.",
"Include warm-up, main workout, and cool-down exercises.",
"Explain the benefits of each recommended exercise.",
"Ensure the plan is actionable and detailed.",
]
)
Collect user information:
col1, col2 = st.columns(2)
with col1:
age = st.number_input("Age", min_value=10, max_value=100)
height = st.number_input("Height (cm)", min_value=100.0)
activity_level = st.selectbox("Activity Level",options=["Sedentary", "Lightly Active", "Moderately Active", "Very Active", "Extremely Active"],
with col2:
weight = st.number_input("Weight (kg)", min_value=20.0)
sex = st.selectbox("Sex", options=["Male", "Female", "Other"])
fitness_goals = st.selectbox("Fitness Goals",options=["Lose Weight", "Gain Muscle", "Endurance", "Stay Fit", "Strength Training"])
Process the information:
user_profile = f"""
Age: {age}
Weight: {weight}kg
Height: {height}cm
Sex: {sex}
Activity Level: {activity_level}
Dietary Preferences: {dietary_preferences}
Fitness Goals: {fitness_goals}
"""
dietary_plan_response = dietary_agent.run(user_profile)
fitness_plan_response = fitness_agent.run(user_profile)
Display personalized plans:
def display-plan (plan_content, plan_type) :
with st.expander(f"Your {plan_type) Plan", expanded=True) :
st.markdown ("#### Goals")
st. success(plan_content.get ("goals" ))
st.markdown ("### Routine")
st.write(plan_content.get ("routine") )
Add Q&A functionality:
question = st. text_input ("Questions about your plan?")
if question:
context = f"{dietary_plan}\n{fitness_plan}\n{question}"
answer = Agent(model=gemini_model). run (context)
st.write (answer)
Running the App
With our code in place, it's time to launch the app.
In your terminal, navigate to the project folder, and run the following command
streamlit run health_agent.py
Streamlit will provide a local URL (typically http://localhost:8501). Open this in your web browser, put in your API key, give it an area you’d want to explore, and watch your AI agent doing the research for you.
Working Application Demo
Conclusion
You've successfully built a sophisticated Health & Fitness AI Agent that provides personalized guidance using Google Gemini's powerful model multiple AI agents working together.
This foundation can be enhanced in several ways:
Add progress tracking and goal monitoring
Implement meal and workout logging features
Add support for multiple users with persistent profiles
Keep experimenting and refining to build smarter AI solutions!
We share hands-on tutorials like this 2-3 times a week, to help you stay ahead in the world of AI. If you're serious about leveling up your AI skills and staying ahead of the curve, subscribe now and be the first to access our latest tutorials.
Reply