Spaces:
Sleeping
Sleeping
Update utils/ai.py
Browse files- utils/ai.py +71 -29
utils/ai.py
CHANGED
|
@@ -1,10 +1,41 @@
|
|
| 1 |
"""
|
| 2 |
-
ai.py β Groq-powered workout + diet plan generation.
|
| 3 |
"""
|
| 4 |
import os, time
|
| 5 |
|
| 6 |
GROQ_MODEL = "llama-3.3-70b-versatile"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def _groq_client():
|
| 9 |
key = os.environ.get("GROQ_API_KEY", "").strip()
|
| 10 |
if not key:
|
|
@@ -109,23 +140,25 @@ GOAL: {goal} | LEVEL: {level} | EQUIPMENT: {eq_str}
|
|
| 109 |
{context}
|
| 110 |
|
| 111 |
FORMAT β use EXACTLY this for each day:
|
| 112 |
-
## Day N β [Focus Area]
|
| 113 |
-
**Warm-Up**
|
| 114 |
-
- Exercise
|
| 115 |
-
- Exercise
|
|
|
|
| 116 |
**Main Workout**
|
| 117 |
- Exercise Name β 3Γ12 reps (rest 60s)
|
| 118 |
- Exercise Name β 3Γ12 reps (rest 60s)
|
| 119 |
- Exercise Name β 3Γ12 reps (rest 60s)
|
| 120 |
-
- Exercise Name β 3Γ
|
| 121 |
-
- Exercise Name β 3Γ
|
| 122 |
-
|
| 123 |
-
-
|
| 124 |
-
- Stretch
|
|
|
|
| 125 |
|
| 126 |
RULES: {intensity} | BMI note: {advice} | Only use: {eq_str}
|
| 127 |
-
{'End with one motivational
|
| 128 |
-
Output ONLY Days {start_day}β{end_day}. No preamble."""
|
| 129 |
|
| 130 |
chunk = _call_groq(prompt, max_tokens=1800)
|
| 131 |
all_workout_chunks.append(chunk.strip())
|
|
@@ -134,7 +167,7 @@ Output ONLY Days {start_day}β{end_day}. No preamble."""
|
|
| 134 |
time.sleep(1)
|
| 135 |
|
| 136 |
if progress_cb:
|
| 137 |
-
progress_cb(total_chunks, total_chunks, "Generating nutrition planβ¦")
|
| 138 |
|
| 139 |
# Diet plan
|
| 140 |
diet_prompt = f"""Create a detailed {days}-day nutrition plan for:
|
|
@@ -143,38 +176,47 @@ Output ONLY Days {start_day}β{end_day}. No preamble."""
|
|
| 143 |
FORMAT:
|
| 144 |
## Daily Nutrition Targets
|
| 145 |
- Calories: X kcal
|
| 146 |
-
- Protein: Xg | Carbs: Xg | Fats: Xg
|
| 147 |
- Water: X litres/day
|
|
|
|
| 148 |
|
| 149 |
## Sample Day Meal Plan
|
| 150 |
-
**Breakfast** β [
|
| 151 |
-
-
|
|
|
|
| 152 |
|
| 153 |
-
**Mid-Morning Snack**
|
| 154 |
-
-
|
| 155 |
|
| 156 |
-
**Lunch** β [
|
| 157 |
-
-
|
|
|
|
| 158 |
|
| 159 |
**Pre-Workout** (if applicable)
|
| 160 |
-
-
|
| 161 |
|
| 162 |
-
**Dinner** β [
|
| 163 |
-
-
|
|
|
|
| 164 |
|
| 165 |
-
**Evening Snack**
|
| 166 |
-
-
|
| 167 |
|
| 168 |
## Key Supplements (optional)
|
| 169 |
-
- Supplement: timing + dosage
|
| 170 |
|
| 171 |
## Foods to Prioritise
|
| 172 |
-
-
|
| 173 |
|
| 174 |
## Foods to Limit
|
| 175 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
Be specific with quantities. Tailor everything to the {goal} goal and {bmi_cat} BMI
|
| 178 |
Note: {advice}"""
|
| 179 |
|
| 180 |
diet = _call_groq(diet_prompt, max_tokens=1500)
|
|
|
|
| 1 |
"""
|
| 2 |
+
ai.py β Groq-powered workout + diet plan generation with exercise descriptions.
|
| 3 |
"""
|
| 4 |
import os, time
|
| 5 |
|
| 6 |
GROQ_MODEL = "llama-3.3-70b-versatile"
|
| 7 |
|
| 8 |
+
# ββ Exercise description database βββββββββββββββββββββββββββββββββββββββββββββ
|
| 9 |
+
EXERCISE_DESCRIPTIONS = {
|
| 10 |
+
"push": "Keep body straight, lower chest to floor, push back up explosively.",
|
| 11 |
+
"pull": "Hang from bar, drive elbows down to pull chin above the bar.",
|
| 12 |
+
"squat": "Feet shoulder-width, push knees out, descend until thighs parallel.",
|
| 13 |
+
"deadlift": "Hinge hips back, grip bar outside knees, drive through heels to stand.",
|
| 14 |
+
"lunge": "Step forward, lower back knee toward floor, keep torso upright.",
|
| 15 |
+
"plank": "Forearms on floor, body in straight line, brace core throughout.",
|
| 16 |
+
"curl": "Upper arms fixed, curl weight toward shoulder, control the descent.",
|
| 17 |
+
"press": "Start at shoulders, press overhead until arms fully extended.",
|
| 18 |
+
"row": "Hinge forward, retract shoulder blade, pull elbow past torso.",
|
| 19 |
+
"fly": "Slight elbow bend, open arms wide, squeeze chest to close.",
|
| 20 |
+
"raise": "Keep slight bend in elbow, raise to shoulder height, lower slowly.",
|
| 21 |
+
"dip": "Grip parallel bars, lower until upper arm parallel, press back up.",
|
| 22 |
+
"crunch": "Feet flat, curl upper body off floor, exhale at the top.",
|
| 23 |
+
"leg raise": "Lie flat, raise legs to 90Β°, lower slowly without touching floor.",
|
| 24 |
+
"glute bridge": "Feet flat, drive hips up, squeeze glutes at the top for 1s.",
|
| 25 |
+
"mountain climber": "High plank, drive knees alternately toward chest at pace.",
|
| 26 |
+
"burpee": "Squat to floor, jump feet back, push-up, jump feet in, leap up.",
|
| 27 |
+
"jump": "Load hips, explode upward, land softly with bent knees.",
|
| 28 |
+
"stretch": "Ease into position, hold without bouncing, breathe steadily.",
|
| 29 |
+
"band": "Anchor band, maintain tension throughout full range of motion.",
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
def get_exercise_description(name):
|
| 33 |
+
name_lower = name.lower()
|
| 34 |
+
for key, desc in EXERCISE_DESCRIPTIONS.items():
|
| 35 |
+
if key in name_lower:
|
| 36 |
+
return desc
|
| 37 |
+
return "Perform with controlled movement through full range of motion."
|
| 38 |
+
|
| 39 |
def _groq_client():
|
| 40 |
key = os.environ.get("GROQ_API_KEY", "").strip()
|
| 41 |
if not key:
|
|
|
|
| 140 |
{context}
|
| 141 |
|
| 142 |
FORMAT β use EXACTLY this for each day:
|
| 143 |
+
## Day N β [Focus Area e.g. Chest & Triceps]
|
| 144 |
+
**Warm-Up** (5β8 min)
|
| 145 |
+
- Exercise Name β 2Γ10 reps (rest 30s)
|
| 146 |
+
- Exercise Name β 2Γ10 reps (rest 30s)
|
| 147 |
+
|
| 148 |
**Main Workout**
|
| 149 |
- Exercise Name β 3Γ12 reps (rest 60s)
|
| 150 |
- Exercise Name β 3Γ12 reps (rest 60s)
|
| 151 |
- Exercise Name β 3Γ12 reps (rest 60s)
|
| 152 |
+
- Exercise Name β 3Γ10 reps (rest 75s)
|
| 153 |
+
- Exercise Name β 3Γ15 reps (rest 45s)
|
| 154 |
+
|
| 155 |
+
**Cool-Down & Stretching**
|
| 156 |
+
- Stretch Name β hold 30s each side
|
| 157 |
+
- Stretch Name β hold 30s
|
| 158 |
|
| 159 |
RULES: {intensity} | BMI note: {advice} | Only use: {eq_str}
|
| 160 |
+
{'End with one short motivational sentence for '+name+'.' if end_day==days else 'No closing text β more days follow.'}
|
| 161 |
+
Output ONLY Days {start_day}β{end_day}. No preamble. No extra text."""
|
| 162 |
|
| 163 |
chunk = _call_groq(prompt, max_tokens=1800)
|
| 164 |
all_workout_chunks.append(chunk.strip())
|
|
|
|
| 167 |
time.sleep(1)
|
| 168 |
|
| 169 |
if progress_cb:
|
| 170 |
+
progress_cb(total_chunks, total_chunks, "Generating personalised nutrition planβ¦")
|
| 171 |
|
| 172 |
# Diet plan
|
| 173 |
diet_prompt = f"""Create a detailed {days}-day nutrition plan for:
|
|
|
|
| 176 |
FORMAT:
|
| 177 |
## Daily Nutrition Targets
|
| 178 |
- Calories: X kcal
|
| 179 |
+
- Protein: Xg | Carbs: Xg | Fats: Xg | Fibre: Xg
|
| 180 |
- Water: X litres/day
|
| 181 |
+
- Meal timing: brief note
|
| 182 |
|
| 183 |
## Sample Day Meal Plan
|
| 184 |
+
**Breakfast** β [Meal name] (~Xcal)
|
| 185 |
+
- Food item: quantity (Xcal, Xg protein)
|
| 186 |
+
- Food item: quantity
|
| 187 |
|
| 188 |
+
**Mid-Morning Snack** (~Xcal)
|
| 189 |
+
- Food item: quantity
|
| 190 |
|
| 191 |
+
**Lunch** β [Meal name] (~Xcal)
|
| 192 |
+
- Food item: quantity (Xcal, Xg protein)
|
| 193 |
+
- Food item: quantity
|
| 194 |
|
| 195 |
**Pre-Workout** (if applicable)
|
| 196 |
+
- Food item: quantity
|
| 197 |
|
| 198 |
+
**Post-Workout / Dinner** β [Meal name] (~Xcal)
|
| 199 |
+
- Food item: quantity (Xcal, Xg protein)
|
| 200 |
+
- Food item: quantity
|
| 201 |
|
| 202 |
+
**Evening Snack** (~Xcal)
|
| 203 |
+
- Food item: quantity
|
| 204 |
|
| 205 |
## Key Supplements (optional)
|
| 206 |
+
- Supplement: timing + dosage + benefit
|
| 207 |
|
| 208 |
## Foods to Prioritise
|
| 209 |
+
- List 6β8 foods with brief reasons
|
| 210 |
|
| 211 |
## Foods to Limit
|
| 212 |
+
- List 4β6 foods with brief reasons
|
| 213 |
+
|
| 214 |
+
## Weekly Meal Prep Tips
|
| 215 |
+
- Tip 1
|
| 216 |
+
- Tip 2
|
| 217 |
+
- Tip 3
|
| 218 |
|
| 219 |
+
Be specific with quantities. Tailor everything to the {goal} goal and {bmi_cat} BMI.
|
| 220 |
Note: {advice}"""
|
| 221 |
|
| 222 |
diet = _call_groq(diet_prompt, max_tokens=1500)
|