awesome-llm-apps/ai_agent_tutorials/ai_health-fitness_agent/dub.py
2024-11-25 22:02:26 +05:30

221 lines
No EOL
8.8 KiB
Python

import streamlit as st
from phi.agent import Agent
from phi.model.anthropic import Claude
from phi.model.google import Gemini
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%;
border-radius: 5px;
height: 3em;
}
.success-box {
padding: 1rem;
border-radius: 0.5rem;
background-color: #f0fff4;
border: 1px solid #9ae6b4;
}
.warning-box {
padding: 1rem;
border-radius: 0.5rem;
background-color: #fffaf0;
border: 1px solid #fbd38d;
}
div[data-testid="stExpander"] div[role="button"] p {
font-size: 1.1rem;
font-weight: 600;
}
</style>
""", unsafe_allow_html=True)
def display_dietary_plan(plan_content):
with st.expander("📋 Your Personalized Dietary Plan", expanded=True):
col1, col2 = st.columns([2, 1])
with col1:
st.markdown("### 🎯 Why this plan works")
st.info(plan_content["why_this_plan_works"])
st.markdown("### 🍽️ Meal Plan")
st.write(plan_content["meal_plan"])
with col2:
st.markdown("### ⚠️ Important Considerations")
for consideration in plan_content["important_considerations"].split('\n'):
if consideration.strip():
st.warning(consideration)
def display_fitness_plan(plan_content):
with st.expander("💪 Your Personalized Fitness Plan", expanded=True):
col1, col2 = st.columns([2, 1])
with col1:
st.markdown("### 🎯 Goals")
st.success(plan_content["goals"])
st.markdown("### 🏋️‍♂️ Exercise Routine")
st.write(plan_content["routine"])
with col2:
st.markdown("### 💡 Pro Tips")
for tip in plan_content["tips"].split('\n'):
if tip.strip():
st.info(tip)
# Main App Layout
def main():
# Header Section
st.title("🏋️‍♂️ AI Health & Fitness Planner")
st.markdown("""
<div style='background-color: #f8f9fa; padding: 1rem; border-radius: 0.5rem; margin-bottom: 2rem;'>
Get personalized dietary and fitness plans tailored to your goals and preferences.
Our AI-powered system considers your unique profile to create the perfect plan for you.
</div>
""", unsafe_allow_html=True)
# Sidebar Configuration
with st.sidebar:
st.header("🔑 API Configuration")
gemini_api_key = st.text_input(
"Gemini API Key",
type="password",
help="Enter your Gemini API key to access the service"
)
if not gemini_api_key:
st.warning("⚠️ Please enter your Gemini API Key to proceed")
st.markdown("[Get your API key here](https://aistudio.google.com/apikey)")
return
st.success(" API Key accepted!")
if gemini_api_key:
# Initialize Gemini model
try:
gemini_model = Gemini(id="gemini-1.5-flash", api_key=gemini_api_key)
except Exception as e:
st.error(f"❌ Error initializing Gemini model: {e}")
return
# User Profile Section
st.header("👤 Your Profile")
col1, col2 = st.columns(2)
with col1:
age = st.number_input("Age", min_value=10, max_value=100, step=1, help="Enter your age")
height = st.number_input("Height (cm)", min_value=100.0, max_value=250.0, step=0.1)
activity_level = st.selectbox(
"Activity Level",
options=["Sedentary", "Lightly Active", "Moderately Active", "Very Active", "Extremely Active"],
help="Choose your typical activity level"
)
dietary_preferences = st.selectbox(
"Dietary Preferences",
options=["Vegetarian", "Keto", "Gluten Free", "Low Carb", "Dairy Free"],
help="Select your dietary preference"
)
with col2:
weight = st.number_input("Weight (kg)", min_value=20.0, max_value=300.0, step=0.1)
sex = st.selectbox("Sex", options=["Male", "Female", "Other"])
fitness_goals = st.selectbox(
"Fitness Goals",
options=["Lose Weight", "Gain Muscle", "Endurance", "Stay Fit", "Strength Training"],
help="What do you want to achieve?"
)
# Generate Plan Button
if st.button("🎯 Generate My Personalized Plan", use_container_width=True):
with st.spinner(" Creating your perfect health and fitness routine..."):
try:
# Define Agents
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.",
]
)
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.",
]
)
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}
"""
# Generate plans
dietary_plan_response = dietary_agent.run(user_profile)
dietary_plan = {
"why_this_plan_works": "High Protein, Healthy Fats, Moderate Carbohydrates, and Caloric Balance",
"meal_plan": dietary_plan_response.content,
"important_considerations": """
- Hydration: Drink plenty of water throughout the day
- Electrolytes: Monitor sodium, potassium, and magnesium levels
- Fiber: Ensure adequate intake through vegetables and fruits
- Listen to your body: Adjust portion sizes as needed
"""
}
fitness_plan_response = fitness_agent.run(user_profile)
fitness_plan = {
"goals": "Build strength, improve endurance, and maintain overall fitness",
"routine": fitness_plan_response.content,
"tips": """
- Track your progress regularly
- Allow proper rest between workouts
- Focus on proper form
- Stay consistent with your routine
"""
}
# Display plans
display_dietary_plan(dietary_plan)
display_fitness_plan(fitness_plan)
# Q&A Section
st.header("❓ Questions about your plan?")
with st.expander("Ask a question"):
question = st.text_input("What would you like to know?")
if st.button("Get Answer"):
with st.spinner("Finding the best answer for you..."):
context = f"Dietary Plan: {dietary_plan['meal_plan']}\n\nFitness Plan: {fitness_plan['routine']}"
response = gemini_model.run(f"{context}\nUser Question: {question}")
st.info(response.content)
except Exception as e:
st.error(f"❌ An error occurred: {e}")
if __name__ == "__main__":
main()