Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,368 +1,508 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
from
|
| 8 |
-
import
|
| 9 |
-
import
|
| 10 |
-
from
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
import
|
| 14 |
-
import
|
| 15 |
-
import
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
def
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
st.
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
st.
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from typing import List, Dict, Optional
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
import os
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from langchain_groq import ChatGroq
|
| 10 |
+
from langchain.schema import HumanMessage, SystemMessage
|
| 11 |
+
import torch
|
| 12 |
+
import numpy as np
|
| 13 |
+
import plotly.graph_objects as go
|
| 14 |
+
from fpdf import FPDF
|
| 15 |
+
import time
|
| 16 |
+
|
| 17 |
+
# Load environment variables
|
| 18 |
+
load_dotenv()
|
| 19 |
+
|
| 20 |
+
# Configure LLMs
|
| 21 |
+
llm = ChatGroq(
|
| 22 |
+
temperature=0.2,
|
| 23 |
+
groq_api_key=os.environ.get("GROQ_API_KEY"),
|
| 24 |
+
model_name="llama-3.1-70b-versatile"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 28 |
+
model = genai.GenerativeModel(
|
| 29 |
+
model_name="gemini-1.5-pro",
|
| 30 |
+
generation_config={
|
| 31 |
+
"temperature": 0.2,
|
| 32 |
+
"top_p": 0.8,
|
| 33 |
+
"top_k": 40,
|
| 34 |
+
"max_output_tokens": 2048,
|
| 35 |
+
}
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Initialize session state for realtime.py components
|
| 39 |
+
if 'chat_history' not in st.session_state:
|
| 40 |
+
st.session_state.chat_history = []
|
| 41 |
+
if 'user_data' not in st.session_state:
|
| 42 |
+
st.session_state.user_data = {
|
| 43 |
+
'progress': [],
|
| 44 |
+
'meal_logs': [],
|
| 45 |
+
'workout_logs': []
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
# Code from acountable.py
|
| 49 |
+
@dataclass
|
| 50 |
+
class TransformationJourney:
|
| 51 |
+
user_id: str
|
| 52 |
+
goal_type: str # fitness, skill, personal growth etc
|
| 53 |
+
start_date: datetime
|
| 54 |
+
progress_images: List[Dict[str, str]] # [{date: image_path}]
|
| 55 |
+
feedback_history: List[Dict[str, str]] # [{date: feedback}]
|
| 56 |
+
chat_history: List[Dict[str, str]] # [{role: content}]
|
| 57 |
+
|
| 58 |
+
class TransformationCoach:
|
| 59 |
+
def __init__(self):
|
| 60 |
+
self.model = model
|
| 61 |
+
self.llm = llm
|
| 62 |
+
|
| 63 |
+
def analyze_image(self, image: Image) -> str:
|
| 64 |
+
prompt = """
|
| 65 |
+
As a transformation coach, analyze this image in relation to the user's goals.
|
| 66 |
+
Provide detailed observations about:
|
| 67 |
+
1. Current state/progress visible in the image
|
| 68 |
+
2. Areas of improvement
|
| 69 |
+
3. Notable achievements
|
| 70 |
+
Be specific and objective in your analysis.
|
| 71 |
+
"""
|
| 72 |
+
response = self.model.generate_content([prompt, image])
|
| 73 |
+
return response.text
|
| 74 |
+
|
| 75 |
+
def generate_feedback(self, analysis: str, goal_type: str) -> Dict[str, List[str]]:
|
| 76 |
+
prompt = f"""
|
| 77 |
+
Based on the following analysis of the user's progress image for their {goal_type} transformation:
|
| 78 |
+
{analysis}
|
| 79 |
+
|
| 80 |
+
Provide structured feedback in the following format:
|
| 81 |
+
1. Specific "DO" recommendations (3-5 points)
|
| 82 |
+
2. Specific "DON'T" recommendations (2-3 points)
|
| 83 |
+
3. A motivational message
|
| 84 |
+
Make all feedback actionable and tailored to the analysis.
|
| 85 |
+
"""
|
| 86 |
+
response = self.model.generate_content(prompt)
|
| 87 |
+
feedback_parts = response.text.split("\n\n")
|
| 88 |
+
return {
|
| 89 |
+
"dos": feedback_parts[0].split("\n")[1:],
|
| 90 |
+
"donts": feedback_parts[1].split("\n")[1:],
|
| 91 |
+
"motivation": feedback_parts[2]
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
def chat_response(self, question: str, context: List[Dict[str, str]], analysis: str = None) -> str:
|
| 95 |
+
prompt = f"""
|
| 96 |
+
You are a supportive transformation coach. Based on the following context:
|
| 97 |
+
|
| 98 |
+
Previous chat: {str(context)}
|
| 99 |
+
Latest analysis: {analysis if analysis else 'No recent analysis'}
|
| 100 |
+
|
| 101 |
+
User question: {question}
|
| 102 |
+
|
| 103 |
+
Provide a helpful, encouraging response focused on their transformation journey.
|
| 104 |
+
"""
|
| 105 |
+
response = self.llm.invoke(prompt)
|
| 106 |
+
return response.content
|
| 107 |
+
|
| 108 |
+
# Functions from realtime.py
|
| 109 |
+
def load_llm():
|
| 110 |
+
api_key = os.environ.get("GROQ_API_KEY")
|
| 111 |
+
if api_key is None:
|
| 112 |
+
st.error("GROQ_API_KEY is not set in the environment variables.")
|
| 113 |
+
return None
|
| 114 |
+
llm = ChatGroq(
|
| 115 |
+
api_key=api_key,
|
| 116 |
+
model_name="llama-3.1-70b-versatile",
|
| 117 |
+
temperature=0.7,
|
| 118 |
+
max_tokens=2048
|
| 119 |
+
)
|
| 120 |
+
return llm
|
| 121 |
+
|
| 122 |
+
def analyze_image_realtime(image, task="meal"):
|
| 123 |
+
try:
|
| 124 |
+
if task == "meal":
|
| 125 |
+
prompt = """
|
| 126 |
+
As a nutrition expert, analyze this meal image and provide:
|
| 127 |
+
1. Estimated caloric content
|
| 128 |
+
2. Macro-nutrient breakdown
|
| 129 |
+
3. Nutritional assessment
|
| 130 |
+
4. Suggestions for improvement
|
| 131 |
+
5. Any potential health concerns
|
| 132 |
+
Be specific and detailed in your analysis.
|
| 133 |
+
"""
|
| 134 |
+
else: # workout form analysis
|
| 135 |
+
prompt = """
|
| 136 |
+
As a fitness expert, analyze this workout form and provide:
|
| 137 |
+
1. Form assessment
|
| 138 |
+
2. Potential injury risks
|
| 139 |
+
3. Specific corrections needed
|
| 140 |
+
4. Benefits of proper form
|
| 141 |
+
Be specific and detailed in your analysis.
|
| 142 |
+
"""
|
| 143 |
+
response = model.generate_content([prompt, image])
|
| 144 |
+
return response.text
|
| 145 |
+
except Exception as e:
|
| 146 |
+
return f"Error analyzing image: {str(e)}"
|
| 147 |
+
|
| 148 |
+
def track_progress(metric, value, date=None):
|
| 149 |
+
if date is None:
|
| 150 |
+
date = datetime.now().strftime("%Y-%m-%d")
|
| 151 |
+
|
| 152 |
+
st.session_state.user_data['progress'].append({
|
| 153 |
+
'date': date,
|
| 154 |
+
'metric': metric,
|
| 155 |
+
'value': value
|
| 156 |
+
})
|
| 157 |
+
|
| 158 |
+
def display_progress_chart():
|
| 159 |
+
if not st.session_state.user_data['progress']:
|
| 160 |
+
return
|
| 161 |
+
|
| 162 |
+
data = st.session_state.user_data['progress']
|
| 163 |
+
fig = go.Figure()
|
| 164 |
+
|
| 165 |
+
metrics = set(item['metric'] for item in data)
|
| 166 |
+
for metric in metrics:
|
| 167 |
+
metric_data = [item for item in data if item['metric'] == metric]
|
| 168 |
+
dates = [item['date'] for item in metric_data]
|
| 169 |
+
values = [item['value'] for item in metric_data]
|
| 170 |
+
|
| 171 |
+
fig.add_trace(go.Scatter(x=dates, y=values, name=metric))
|
| 172 |
+
|
| 173 |
+
fig.update_layout(title="Your Progress Over Time",
|
| 174 |
+
xaxis_title="Date",
|
| 175 |
+
yaxis_title="Value")
|
| 176 |
+
st.plotly_chart(fig)
|
| 177 |
+
|
| 178 |
+
# Functions from workout.py
|
| 179 |
+
def generate_meal_plan(weight, height, age, gender, activity_level, goal, dietary_restrictions, health_history, current_eating_habits, meal_preparation, budget, sleep):
|
| 180 |
+
"""Generate personalized meal plan using LLM"""
|
| 181 |
+
prompt = f"""
|
| 182 |
+
Based on the following user data, generate a highly personalized and detailed daily meal plan with easily available ingredients:
|
| 183 |
+
|
| 184 |
+
User Profile:
|
| 185 |
+
- Weight: {weight} kg
|
| 186 |
+
- Height: {height} cm
|
| 187 |
+
- Age: {age}
|
| 188 |
+
- Gender: {gender}
|
| 189 |
+
- Activity Level: {activity_level}
|
| 190 |
+
- Goal: {goal}
|
| 191 |
+
- Dietary Restrictions: {', '.join(dietary_restrictions)}
|
| 192 |
+
- Health History: {health_history}
|
| 193 |
+
- Current Eating Habits: {current_eating_habits}
|
| 194 |
+
- Meal Preparation: {meal_preparation}
|
| 195 |
+
- Budget: {budget}
|
| 196 |
+
- Sleep: {sleep}
|
| 197 |
+
|
| 198 |
+
Generate a clear and structured meal plan with:
|
| 199 |
+
|
| 200 |
+
1. Total Daily Calorie Requirements
|
| 201 |
+
2. Macronutrient Distribution (in grams):
|
| 202 |
+
- Protein
|
| 203 |
+
- Carbohydrates
|
| 204 |
+
- Fats
|
| 205 |
+
|
| 206 |
+
Daily Meal Schedule:
|
| 207 |
+
[Detailed meal schedule content...]
|
| 208 |
+
"""
|
| 209 |
+
|
| 210 |
+
response = llm.invoke(prompt)
|
| 211 |
+
return response.content
|
| 212 |
+
|
| 213 |
+
def generate_workout_plan(goal, activity_level, target_areas, workout_time, current_fitness_level, exercise_preferences):
|
| 214 |
+
"""Generate personalized workout plan using LLM"""
|
| 215 |
+
prompt = f"""
|
| 216 |
+
Based on:
|
| 217 |
+
- Fitness Goal: {goal}
|
| 218 |
+
- Activity Level: {activity_level}
|
| 219 |
+
- Target Areas: {target_areas}
|
| 220 |
+
- Workout Time: {workout_time}
|
| 221 |
+
- Current Fitness Level: {current_fitness_level}
|
| 222 |
+
- Exercise Preferences: {exercise_preferences}
|
| 223 |
+
|
| 224 |
+
Generate a clear and structured weekly workout plan:
|
| 225 |
+
[Detailed workout plan content...]
|
| 226 |
+
"""
|
| 227 |
+
|
| 228 |
+
response = llm.invoke(prompt)
|
| 229 |
+
return response.content
|
| 230 |
+
|
| 231 |
+
def create_pdf(name, meal_plan, workout_plan):
|
| 232 |
+
pdf = FPDF()
|
| 233 |
+
pdf.add_page()
|
| 234 |
+
|
| 235 |
+
pdf.set_font('Arial', 'B', 20)
|
| 236 |
+
pdf.cell(0, 10, f'Personalized Fitness Plan for {name}', ln=True, align='C')
|
| 237 |
+
pdf.ln(10)
|
| 238 |
+
|
| 239 |
+
pdf.set_font('Arial', 'B', 16)
|
| 240 |
+
pdf.cell(0, 10, 'Meal Plan', ln=True)
|
| 241 |
+
pdf.set_font('Arial', '', 12)
|
| 242 |
+
pdf.multi_cell(0, 10, meal_plan)
|
| 243 |
+
|
| 244 |
+
pdf.add_page()
|
| 245 |
+
pdf.set_font('Arial', 'B', 16)
|
| 246 |
+
pdf.cell(0, 10, 'Workout Plan', ln=True)
|
| 247 |
+
pdf.set_font('Arial', '', 12)
|
| 248 |
+
pdf.multi_cell(0, 10, workout_plan)
|
| 249 |
+
|
| 250 |
+
return pdf
|
| 251 |
+
|
| 252 |
+
def main():
|
| 253 |
+
st.set_page_config(page_title="AI Fitness Coach", layout="wide")
|
| 254 |
+
|
| 255 |
+
# Chat input needs to be outside tabs
|
| 256 |
+
user_input = st.chat_input("Type your message here...")
|
| 257 |
+
if user_input:
|
| 258 |
+
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
| 259 |
+
|
| 260 |
+
# Process user input with LLM
|
| 261 |
+
llm = load_llm()
|
| 262 |
+
if llm is not None:
|
| 263 |
+
messages = [
|
| 264 |
+
SystemMessage(content="You are an expert health and fitness coach. Provide detailed and personalized advice."),
|
| 265 |
+
HumanMessage(content=user_input)
|
| 266 |
+
]
|
| 267 |
+
response = llm(messages)
|
| 268 |
+
|
| 269 |
+
st.session_state.chat_history.append({"role": "assistant", "content": response.content})
|
| 270 |
+
|
| 271 |
+
tab1, tab2, tab3 = st.tabs(["Transformation Journey", "Realtime Tracking", "Workout & Meal Plans"])
|
| 272 |
+
|
| 273 |
+
with tab1:
|
| 274 |
+
# Implementation from acountable.py
|
| 275 |
+
st.title("Transformation Journey Tracker")
|
| 276 |
+
|
| 277 |
+
if 'coach' not in st.session_state:
|
| 278 |
+
st.session_state.coach = TransformationCoach()
|
| 279 |
+
if 'journey' not in st.session_state:
|
| 280 |
+
st.session_state.journey = None
|
| 281 |
+
|
| 282 |
+
# Initialize or load journey
|
| 283 |
+
if not st.session_state.journey:
|
| 284 |
+
with st.form("journey_setup"):
|
| 285 |
+
st.write("Let's start your transformation journey!")
|
| 286 |
+
user_id = st.text_input("Enter your user ID")
|
| 287 |
+
goal_type = st.selectbox(
|
| 288 |
+
"What type of transformation?",
|
| 289 |
+
["Fitness", "Skill Development", "Personal Growth", "Other"]
|
| 290 |
+
)
|
| 291 |
+
if st.form_submit_button("Start Journey"):
|
| 292 |
+
st.session_state.journey = TransformationJourney(
|
| 293 |
+
user_id=user_id,
|
| 294 |
+
goal_type=goal_type,
|
| 295 |
+
start_date=datetime.now(),
|
| 296 |
+
progress_images=[],
|
| 297 |
+
feedback_history=[],
|
| 298 |
+
chat_history=[]
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
# Main interaction area
|
| 302 |
+
if st.session_state.journey:
|
| 303 |
+
st.subheader("Upload Progress Image")
|
| 304 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"], key="transform_uploader")
|
| 305 |
+
|
| 306 |
+
if uploaded_file:
|
| 307 |
+
image = Image.open(uploaded_file)
|
| 308 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 309 |
+
|
| 310 |
+
if st.button("Analyze and Get Feedback"):
|
| 311 |
+
with st.spinner("Analyzing your progress..."):
|
| 312 |
+
analysis = st.session_state.coach.analyze_image(image)
|
| 313 |
+
feedback = st.session_state.coach.generate_feedback(
|
| 314 |
+
analysis,
|
| 315 |
+
st.session_state.journey.goal_type
|
| 316 |
+
)
|
| 317 |
+
|
| 318 |
+
# Display feedback
|
| 319 |
+
st.subheader("Your Personalized Feedback")
|
| 320 |
+
|
| 321 |
+
st.write("🎯 DO:")
|
| 322 |
+
for do in feedback["dos"]:
|
| 323 |
+
st.write(f"✓ {do}")
|
| 324 |
+
|
| 325 |
+
st.write("⚠️ DON'T:")
|
| 326 |
+
for dont in feedback["donts"]:
|
| 327 |
+
st.write(f"✗ {dont}")
|
| 328 |
+
|
| 329 |
+
st.write("💪 Motivation:")
|
| 330 |
+
st.write(feedback["motivation"])
|
| 331 |
+
|
| 332 |
+
# Store progress
|
| 333 |
+
current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 334 |
+
image_path = f"progress_images/{st.session_state.journey.user_id}_{current_time}.jpg"
|
| 335 |
+
st.session_state.journey.progress_images.append({
|
| 336 |
+
current_time: image_path
|
| 337 |
+
})
|
| 338 |
+
st.session_state.journey.feedback_history.append({
|
| 339 |
+
current_time: feedback
|
| 340 |
+
})
|
| 341 |
+
|
| 342 |
+
# Chat interface
|
| 343 |
+
st.write("---")
|
| 344 |
+
st.subheader("Chat with Your Coach")
|
| 345 |
+
|
| 346 |
+
# Display chat history
|
| 347 |
+
for message in st.session_state.journey.chat_history:
|
| 348 |
+
if message["role"] == "user":
|
| 349 |
+
st.write("You: " + message["content"])
|
| 350 |
+
else:
|
| 351 |
+
st.write("Coach: " + message["content"])
|
| 352 |
+
|
| 353 |
+
# Chat input
|
| 354 |
+
user_question = st.text_input("Ask your coach anything:", key="transform_chat")
|
| 355 |
+
if st.button("Send", key="transform_send"):
|
| 356 |
+
if user_question:
|
| 357 |
+
# Add user message
|
| 358 |
+
st.session_state.journey.chat_history.append({
|
| 359 |
+
"role": "user",
|
| 360 |
+
"content": user_question
|
| 361 |
+
})
|
| 362 |
+
|
| 363 |
+
# Get coach's response
|
| 364 |
+
latest_analysis = st.session_state.coach.analyze_image(image) if uploaded_file else None
|
| 365 |
+
response = st.session_state.coach.chat_response(
|
| 366 |
+
user_question,
|
| 367 |
+
st.session_state.journey.chat_history,
|
| 368 |
+
latest_analysis
|
| 369 |
+
)
|
| 370 |
+
|
| 371 |
+
# Add coach's response
|
| 372 |
+
st.session_state.journey.chat_history.append({
|
| 373 |
+
"role": "coach",
|
| 374 |
+
"content": response
|
| 375 |
+
})
|
| 376 |
+
|
| 377 |
+
st.experimental_rerun()
|
| 378 |
+
|
| 379 |
+
with tab2:
|
| 380 |
+
# Implementation from realtime.py
|
| 381 |
+
st.title("AI Health Coach")
|
| 382 |
+
|
| 383 |
+
st.warning("""This system is for informational purposes only and is not a substitute for professional medical advice,
|
| 384 |
+
diagnosis, or treatment. Always seek the advice of your physician or other qualified health provider.""")
|
| 385 |
+
|
| 386 |
+
# Display chat history
|
| 387 |
+
for message in st.session_state.chat_history:
|
| 388 |
+
with st.chat_message(message["role"]):
|
| 389 |
+
st.write(message["content"])
|
| 390 |
+
|
| 391 |
+
# Image upload and analysis
|
| 392 |
+
uploaded_file = st.file_uploader("Upload an image for analysis (meal or workout form)", type=['png', 'jpg', 'jpeg'], key="realtime_uploader")
|
| 393 |
+
if uploaded_file:
|
| 394 |
+
image = Image.open(uploaded_file)
|
| 395 |
+
st.image(image, caption="Uploaded Image")
|
| 396 |
+
|
| 397 |
+
analysis_type = st.radio("What would you like to analyze?", ["Meal", "Workout Form"])
|
| 398 |
+
if st.button("Analyze Image", key="realtime_analyze"):
|
| 399 |
+
with st.spinner("Analyzing image..."):
|
| 400 |
+
results = analyze_image_realtime(image, analysis_type.lower())
|
| 401 |
+
st.write("Analysis Results:", results)
|
| 402 |
+
|
| 403 |
+
# Progress tracking section
|
| 404 |
+
with st.expander("Track Your Progress"):
|
| 405 |
+
metric = st.selectbox("Select metric to track", ["Weight", "Body Fat %", "Workout Duration", "Calories"])
|
| 406 |
+
value = st.number_input("Enter value", key="progress_value")
|
| 407 |
+
if st.button("Log Progress", key="log_progress"):
|
| 408 |
+
track_progress(metric, value)
|
| 409 |
+
st.success("Progress logged successfully!")
|
| 410 |
+
|
| 411 |
+
# Display progress charts
|
| 412 |
+
display_progress_chart()
|
| 413 |
+
|
| 414 |
+
with tab3:
|
| 415 |
+
# Implementation from workout.py
|
| 416 |
+
st.title("AI Fitness Coach")
|
| 417 |
+
st.markdown("<h1 style='text-align: center; color: #4CAF50;'>Welcome to Your Personalized Fitness Journey!</h1>", unsafe_allow_html=True)
|
| 418 |
+
|
| 419 |
+
# Get user inputs
|
| 420 |
+
st.header("Personal Information")
|
| 421 |
+
|
| 422 |
+
col1, col2, col3 = st.columns(3)
|
| 423 |
+
|
| 424 |
+
with col1:
|
| 425 |
+
name = st.text_input("What's your name?", placeholder="Enter your name")
|
| 426 |
+
age = st.number_input("What's your age?", min_value=1, max_value=120, value=25)
|
| 427 |
+
gender = st.selectbox("What's your gender?", ["Male", "Female"])
|
| 428 |
+
health_history = st.text_input("Do you have any medical conditions?")
|
| 429 |
+
|
| 430 |
+
with col2:
|
| 431 |
+
weight = st.number_input("What's your weight (in kg)?", min_value=1, value=70)
|
| 432 |
+
height = st.number_input("What's your height (in cm)?", min_value=1, value=170)
|
| 433 |
+
goal = st.selectbox(
|
| 434 |
+
"What's your fitness goal?",
|
| 435 |
+
["Weight Loss", "Muscle Gain", "Maintenance", "General Fitness"],
|
| 436 |
+
index=0
|
| 437 |
+
)
|
| 438 |
+
activity_level = st.selectbox(
|
| 439 |
+
"What's your activity level?",
|
| 440 |
+
[
|
| 441 |
+
"Sedentary (little to no exercise)",
|
| 442 |
+
"Lightly Active (1-3 days/week)",
|
| 443 |
+
"Moderately Active (3-5 days/week)",
|
| 444 |
+
"Very Active (6-7 days/week)",
|
| 445 |
+
"Extra Active (very intense exercise daily)"
|
| 446 |
+
]
|
| 447 |
+
)
|
| 448 |
+
|
| 449 |
+
with col3:
|
| 450 |
+
dietary_restrictions = st.multiselect(
|
| 451 |
+
"Do you have any dietary restrictions?",
|
| 452 |
+
["None", "Vegetarian", "Vegan", "Gluten-Free", "Dairy-Free", "Keto"]
|
| 453 |
+
)
|
| 454 |
+
current_eating_habits = st.text_input("What does your current diet look like?")
|
| 455 |
+
meal_preparation = st.text_input("Do you cook your meals or rely on takeout?")
|
| 456 |
+
budget = st.number_input("What is your monthly budget for groceries/food?", min_value=0)
|
| 457 |
+
|
| 458 |
+
if st.button("Generate Plans", key="generate_plans"):
|
| 459 |
+
if not name or not goal or not age or not gender or not weight or not height or not activity_level:
|
| 460 |
+
st.error("Please fill in all required fields")
|
| 461 |
+
return
|
| 462 |
+
|
| 463 |
+
with st.spinner("Generating your personalized plans..."):
|
| 464 |
+
meal_plan = generate_meal_plan(
|
| 465 |
+
weight=weight,
|
| 466 |
+
height=height,
|
| 467 |
+
age=age,
|
| 468 |
+
gender=gender,
|
| 469 |
+
activity_level=activity_level,
|
| 470 |
+
goal=goal,
|
| 471 |
+
dietary_restrictions=dietary_restrictions if dietary_restrictions else ["None"],
|
| 472 |
+
health_history=health_history,
|
| 473 |
+
current_eating_habits=current_eating_habits,
|
| 474 |
+
meal_preparation=meal_preparation,
|
| 475 |
+
budget=budget,
|
| 476 |
+
sleep="8 hours" # Default value
|
| 477 |
+
)
|
| 478 |
+
workout_plan = generate_workout_plan(
|
| 479 |
+
goal=goal,
|
| 480 |
+
activity_level=activity_level,
|
| 481 |
+
target_areas="full body", # Default value
|
| 482 |
+
workout_time=60, # Default value
|
| 483 |
+
current_fitness_level="Beginner", # Default value
|
| 484 |
+
exercise_preferences="None" # Default value
|
| 485 |
+
)
|
| 486 |
+
|
| 487 |
+
# Create PDF
|
| 488 |
+
pdf = create_pdf(name, meal_plan, workout_plan)
|
| 489 |
+
|
| 490 |
+
# Display plans in two columns
|
| 491 |
+
col1, col2 = st.columns(2)
|
| 492 |
+
with col1:
|
| 493 |
+
st.subheader("Your Meal Plan")
|
| 494 |
+
st.write(meal_plan)
|
| 495 |
+
with col2:
|
| 496 |
+
st.subheader("Your Workout Plan")
|
| 497 |
+
st.write(workout_plan)
|
| 498 |
+
|
| 499 |
+
# Add download button
|
| 500 |
+
st.download_button(
|
| 501 |
+
label="Download Plan as PDF",
|
| 502 |
+
data=pdf.output(dest='S').encode('latin-1'),
|
| 503 |
+
file_name=f"fitness_plan_{name}.pdf",
|
| 504 |
+
mime="application/pdf"
|
| 505 |
+
)
|
| 506 |
+
|
| 507 |
+
if __name__ == "__main__":
|
| 508 |
+
main()
|