Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,28 @@
|
|
| 1 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
-
import torch
|
| 3 |
import streamlit as st
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# -------------------------
|
| 6 |
# PAGE CONFIG
|
| 7 |
# -------------------------
|
| 8 |
st.set_page_config(page_title="FitPlan-AI", page_icon="💪")
|
| 9 |
-
|
| 10 |
st.title("💪 FitPlan-AI: Personalized Fitness Profile")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# -------------------------
|
| 13 |
# BMI FUNCTIONS
|
| 14 |
# -------------------------
|
|
@@ -26,6 +40,67 @@ def get_category(bmi):
|
|
| 26 |
else:
|
| 27 |
return "Obese"
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
# -------------------------
|
| 30 |
# FORM
|
| 31 |
# -------------------------
|
|
@@ -78,46 +153,27 @@ if submit:
|
|
| 78 |
else:
|
| 79 |
st.success("Profile Submitted Successfully!")
|
| 80 |
|
| 81 |
-
#
|
| 82 |
bmi = calculate_bmi(weight, height)
|
| 83 |
bmi_status = get_category(bmi)
|
| 84 |
|
| 85 |
st.write(f"### 📊 Your BMI: {bmi} ({bmi_status})")
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
# GENERATE 5-DAY PLAN (SINGLE PROMPT METHOD)
|
| 89 |
-
# -------------------------
|
| 90 |
-
from prompt_builder import build_prompt
|
| 91 |
-
|
| 92 |
with st.spinner("Generating your 5-day workout plan..."):
|
| 93 |
-
|
| 94 |
-
prompt, bmi, bmi_status = build_prompt(
|
| 95 |
name=name,
|
| 96 |
-
gender="Not specified", # You can add gender field later
|
| 97 |
height=height,
|
| 98 |
weight=weight,
|
| 99 |
goal=goal,
|
| 100 |
-
|
| 101 |
-
equipment=equipment
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 105 |
-
|
| 106 |
-
outputs = model.generate(
|
| 107 |
-
**inputs,
|
| 108 |
-
max_new_tokens=2000,
|
| 109 |
-
temperature=0.7,
|
| 110 |
-
do_sample=True,
|
| 111 |
-
repetition_penalty=1.1
|
| 112 |
)
|
| 113 |
|
| 114 |
-
full_plan =
|
| 115 |
-
outputs[0],
|
| 116 |
-
skip_special_tokens=True
|
| 117 |
-
).strip()
|
| 118 |
|
| 119 |
-
#
|
| 120 |
-
# DISPLAY PLAN
|
| 121 |
-
# -------------------------
|
| 122 |
st.subheader("🏋️ Your Personalized 5-Day Workout Plan")
|
| 123 |
-
st.write(full_plan)
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
# -------------------------
|
| 6 |
# PAGE CONFIG
|
| 7 |
# -------------------------
|
| 8 |
st.set_page_config(page_title="FitPlan-AI", page_icon="💪")
|
|
|
|
| 9 |
st.title("💪 FitPlan-AI: Personalized Fitness Profile")
|
| 10 |
|
| 11 |
+
# -------------------------
|
| 12 |
+
# LOAD HF CLIENT (CACHED)
|
| 13 |
+
# -------------------------
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def get_hf_client():
|
| 16 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 17 |
+
|
| 18 |
+
if not hf_token:
|
| 19 |
+
raise ValueError("HF_TOKEN not set in environment variables.")
|
| 20 |
+
|
| 21 |
+
return InferenceClient(
|
| 22 |
+
model="mistralai/Mistral-7B-Instruct-v0.2",
|
| 23 |
+
token=hf_token
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
# -------------------------
|
| 27 |
# BMI FUNCTIONS
|
| 28 |
# -------------------------
|
|
|
|
| 40 |
else:
|
| 41 |
return "Obese"
|
| 42 |
|
| 43 |
+
# -------------------------
|
| 44 |
+
# PROMPT BUILDER
|
| 45 |
+
# -------------------------
|
| 46 |
+
def build_prompt(name, height, weight, goal, level, equipment, bmi, bmi_status):
|
| 47 |
+
equipment_list = ", ".join(equipment)
|
| 48 |
+
|
| 49 |
+
prompt = f"""
|
| 50 |
+
Create a detailed 5-day personalized workout plan.
|
| 51 |
+
|
| 52 |
+
Client Details:
|
| 53 |
+
Name: {name}
|
| 54 |
+
Height: {height} cm
|
| 55 |
+
Weight: {weight} kg
|
| 56 |
+
BMI: {bmi} ({bmi_status})
|
| 57 |
+
Goal: {goal}
|
| 58 |
+
Fitness Level: {level}
|
| 59 |
+
Available Equipment: {equipment_list}
|
| 60 |
+
|
| 61 |
+
Instructions:
|
| 62 |
+
- Provide a structured 5-day plan.
|
| 63 |
+
- Each day must include:
|
| 64 |
+
- Warm-up
|
| 65 |
+
- Main Workout (Sets x Reps)
|
| 66 |
+
- Rest time
|
| 67 |
+
- Cooldown
|
| 68 |
+
- Ensure exercises match fitness level and equipment.
|
| 69 |
+
- Keep it practical and realistic.
|
| 70 |
+
- Format clearly as:
|
| 71 |
+
|
| 72 |
+
Day 1:
|
| 73 |
+
Warm-up:
|
| 74 |
+
Main Workout:
|
| 75 |
+
Cooldown:
|
| 76 |
+
|
| 77 |
+
Continue up to Day 5.
|
| 78 |
+
"""
|
| 79 |
+
|
| 80 |
+
return prompt.strip()
|
| 81 |
+
|
| 82 |
+
# -------------------------
|
| 83 |
+
# MODEL QUERY
|
| 84 |
+
# -------------------------
|
| 85 |
+
def query_model(prompt):
|
| 86 |
+
try:
|
| 87 |
+
client = get_hf_client()
|
| 88 |
+
|
| 89 |
+
response = client.chat_completion(
|
| 90 |
+
messages=[
|
| 91 |
+
{"role": "system", "content": "You are a certified professional fitness trainer."},
|
| 92 |
+
{"role": "user", "content": prompt}
|
| 93 |
+
],
|
| 94 |
+
max_tokens=800,
|
| 95 |
+
temperature=0.7,
|
| 96 |
+
top_p=0.9
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
return response.choices[0].message.content.strip()
|
| 100 |
+
|
| 101 |
+
except Exception as e:
|
| 102 |
+
return f"⚠️ Model Error: {str(e)}"
|
| 103 |
+
|
| 104 |
# -------------------------
|
| 105 |
# FORM
|
| 106 |
# -------------------------
|
|
|
|
| 153 |
else:
|
| 154 |
st.success("Profile Submitted Successfully!")
|
| 155 |
|
| 156 |
+
# BMI Calculation
|
| 157 |
bmi = calculate_bmi(weight, height)
|
| 158 |
bmi_status = get_category(bmi)
|
| 159 |
|
| 160 |
st.write(f"### 📊 Your BMI: {bmi} ({bmi_status})")
|
| 161 |
|
| 162 |
+
# Generate Plan
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
with st.spinner("Generating your 5-day workout plan..."):
|
| 164 |
+
prompt = build_prompt(
|
|
|
|
| 165 |
name=name,
|
|
|
|
| 166 |
height=height,
|
| 167 |
weight=weight,
|
| 168 |
goal=goal,
|
| 169 |
+
level=level,
|
| 170 |
+
equipment=equipment,
|
| 171 |
+
bmi=bmi,
|
| 172 |
+
bmi_status=bmi_status
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
)
|
| 174 |
|
| 175 |
+
full_plan = query_model(prompt)
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
+
# Display Plan
|
|
|
|
|
|
|
| 178 |
st.subheader("🏋️ Your Personalized 5-Day Workout Plan")
|
| 179 |
+
st.write(full_plan)
|