Spaces:
Build error
Build error
Upload 2 files
Browse files- aifitness.py +105 -0
- requirements.txt +27 -0
aifitness.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from crewai import Agent, Task, Crew, Process
|
| 4 |
+
|
| 5 |
+
# LiteLLM wrapper for Groq models
|
| 6 |
+
from langchain_community.chat_models import ChatLiteLLM
|
| 7 |
+
|
| 8 |
+
# === API Key ===
|
| 9 |
+
os.environ["GROQ_API_KEY"] ="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" # 🔒 Replace this with your actual key
|
| 10 |
+
|
| 11 |
+
# === Streamlit Page Config ===
|
| 12 |
+
st.set_page_config(page_title="🧠 Doctor AI Assistant", page_icon="🩺", layout="wide")
|
| 13 |
+
|
| 14 |
+
# === Session State Initialization ===
|
| 15 |
+
if "chat_history" not in st.session_state:
|
| 16 |
+
st.session_state.chat_history = []
|
| 17 |
+
|
| 18 |
+
# === Core Function: Doctor AI Agent ===
|
| 19 |
+
def run_doctor_ai(query, model_name="gemma2-9b-it", temperature=0.7):
|
| 20 |
+
try:
|
| 21 |
+
llm = ChatLiteLLM(
|
| 22 |
+
model=model_name,
|
| 23 |
+
api_base="https://api.groq.com/openai/v1",
|
| 24 |
+
api_key=os.getenv("GROQ_API_KEY"),
|
| 25 |
+
temperature=temperature
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
agent = Agent(
|
| 29 |
+
role="AI Fitness Coach",
|
| 30 |
+
goal="Help users achieve their fitness goals by creating custom workout and diet plans.",
|
| 31 |
+
backstory="""
|
| 32 |
+
You are an expert fitness trainer and nutritionist who provides personalized fitness plans for users.
|
| 33 |
+
Ask clarifying questions if needed (e.g., user's goal, age, gender, weight, lifestyle).
|
| 34 |
+
Provide suggestions for workouts, rest days, and diet plans based on the input.
|
| 35 |
+
Respond with a **daily fitness plan** (including workout type, duration, rest), and a **balanced diet chart** for the user’s goal.
|
| 36 |
+
Use a **table format** for both workout and diet.
|
| 37 |
+
Highlight **important tips** in **bold** (like hydration, protein intake, rest).
|
| 38 |
+
|
| 39 |
+
🧠 Example Prompt:
|
| 40 |
+
"I'm a 22-year-old male, 75kg, and want to lose fat. I have 1 hour a day to workout."
|
| 41 |
+
|
| 42 |
+
📅 Sample Workout Plan:
|
| 43 |
+
| Day | Workout Type | Duration | Focus Area | Rest |
|
| 44 |
+
|----------|----------------|----------|------------------|------|
|
| 45 |
+
| Monday | HIIT Cardio | 30 mins | Full Body | No |
|
| 46 |
+
| Tuesday | Strength + Core | 1 hr | Core & Upper | No |
|
| 47 |
+
|
| 48 |
+
🍱 Sample Diet Chart:
|
| 49 |
+
| Meal | Food Ideas | Notes |
|
| 50 |
+
|-----------|------------------------------------------|----------------------------------|
|
| 51 |
+
| Breakfast | Oats, eggs, fruits | High protein, low sugar |
|
| 52 |
+
| Lunch | Grilled chicken, veggies, brown rice | Balanced macro intake |
|
| 53 |
+
| Dinner | Lentil soup, salad, yogurt | Light and nutritious |
|
| 54 |
+
|
| 55 |
+
💡 Tips:
|
| 56 |
+
- **Drink at least 3L of water daily**
|
| 57 |
+
- **Avoid sugar and processed snacks**
|
| 58 |
+
- **Get 7-8 hours of sleep for recovery**
|
| 59 |
+
""",
|
| 60 |
+
tools=[],
|
| 61 |
+
verbose=True,
|
| 62 |
+
allow_delegation=False,
|
| 63 |
+
llm=llm
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
task = Task(
|
| 67 |
+
description=f"Understand the user's fitness or health concern: '{query}' and generate a tailored fitness/diet plan.",
|
| 68 |
+
expected_output="Return a daily workout and diet plan in table format with additional helpful fitness tips.",
|
| 69 |
+
agent=agent
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
crew = Crew(
|
| 73 |
+
agents=[agent],
|
| 74 |
+
tasks=[task],
|
| 75 |
+
process=Process.sequential,
|
| 76 |
+
verbose=True
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
return crew.kickoff()
|
| 80 |
+
|
| 81 |
+
except Exception as e:
|
| 82 |
+
return f"❌ Error: {str(e)}"
|
| 83 |
+
|
| 84 |
+
# === Streamlit Chat Interface ===
|
| 85 |
+
st.title("🏋 FITNESS AI COach")
|
| 86 |
+
st.markdown("Get personalized **workout and diet plans** based on your goals, health status, and lifestyle.")
|
| 87 |
+
|
| 88 |
+
# Display previous messages
|
| 89 |
+
for msg in st.session_state.chat_history:
|
| 90 |
+
with st.chat_message(msg["role"]):
|
| 91 |
+
st.markdown(msg["content"])
|
| 92 |
+
|
| 93 |
+
# Input prompt
|
| 94 |
+
user_prompt = st.chat_input("Ask me about your health or fitness goals...")
|
| 95 |
+
|
| 96 |
+
if user_prompt:
|
| 97 |
+
# Show user message
|
| 98 |
+
st.chat_message("user").markdown(user_prompt)
|
| 99 |
+
st.session_state.chat_history.append({"role": "user", "content": user_prompt})
|
| 100 |
+
|
| 101 |
+
# Generate AI response
|
| 102 |
+
with st.chat_message("assistant"):
|
| 103 |
+
response = run_doctor_ai(user_prompt)
|
| 104 |
+
st.markdown(response)
|
| 105 |
+
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
requirements.txt
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
crewai
|
| 3 |
+
langchain
|
| 4 |
+
langchain-community
|
| 5 |
+
langchainhub
|
| 6 |
+
python-dotenv
|
| 7 |
+
grok
|
| 8 |
+
groqcloud
|
| 9 |
+
|
| 10 |
+
beautifulsoup4
|
| 11 |
+
requests
|
| 12 |
+
pandas
|
| 13 |
+
openai
|
| 14 |
+
chromadb
|
| 15 |
+
streamlit==1.32.2
|
| 16 |
+
crewai==0.22.2
|
| 17 |
+
chromadb==0.4.24
|
| 18 |
+
langchain
|
| 19 |
+
groq
|
| 20 |
+
langchain-groq
|
| 21 |
+
litellm
|
| 22 |
+
yfinance
|
| 23 |
+
pandas
|
| 24 |
+
plotly
|
| 25 |
+
yfinance
|
| 26 |
+
plotly-express
|
| 27 |
+
|