Spaces:
Sleeping
Sleeping
File size: 4,457 Bytes
e803694 a0ac5bc e803694 a0ac5bc e803694 a0ac5bc e803694 a0ac5bc e803694 a0ac5bc d8b0f81 a0ac5bc d8b0f81 a0ac5bc d8b0f81 a0ac5bc e803694 a0ac5bc e803694 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | import streamlit as st
from huggingface_hub import InferenceClient
import os
# -------------------------
# PAGE CONFIG
# -------------------------
st.set_page_config(page_title="FitPlan-AI", page_icon="💪")
st.title("💪 FitPlan-AI: Personalized Fitness Profile")
# -------------------------
# LOAD HF CLIENT (CACHED)
# -------------------------
@st.cache_resource
def get_hf_client():
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
raise ValueError("HF_TOKEN not set in environment variables.")
return InferenceClient(
model="mistralai/Mistral-7B-Instruct-v0.2",
token=hf_token
)
# -------------------------
# BMI FUNCTIONS
# -------------------------
def calculate_bmi(weight, height_cm):
height_m = height_cm / 100
return round(weight / (height_m ** 2), 2)
def get_category(bmi):
if bmi < 18.5:
return "Underweight"
elif bmi < 24.9:
return "Normal"
elif bmi < 29.9:
return "Overweight"
else:
return "Obese"
# -------------------------
# PROMPT BUILDER
# -------------------------
def build_prompt(name, height, weight, goal, level, equipment, bmi, bmi_status):
equipment_list = ", ".join(equipment)
prompt = f"""
Create a detailed 5-day personalized workout plan.
Client Details:
Name: {name}
Height: {height} cm
Weight: {weight} kg
BMI: {bmi} ({bmi_status})
Goal: {goal}
Fitness Level: {level}
Available Equipment: {equipment_list}
Instructions:
- Provide a structured 5-day plan.
- Each day must include:
- Warm-up
- Main Workout (Sets x Reps)
- Rest time
- Cooldown
- Ensure exercises match fitness level and equipment.
- Keep it practical and realistic.
- Format clearly as:
Day 1:
Warm-up:
Main Workout:
Cooldown:
Continue up to Day 5.
"""
return prompt.strip()
# -------------------------
# MODEL QUERY
# -------------------------
def query_model(prompt):
try:
client = get_hf_client()
response = client.chat_completion(
messages=[
{"role": "system", "content": "You are a certified professional fitness trainer."},
{"role": "user", "content": prompt}
],
max_tokens=800,
temperature=0.7,
top_p=0.9
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"⚠️ Model Error: {str(e)}"
# -------------------------
# FORM
# -------------------------
with st.form("fitness_form"):
st.subheader("Personal Information")
name = st.text_input("Full Name*", placeholder="Enter your name")
col1, col2 = st.columns(2)
with col1:
height = st.number_input("Height (cm)*", min_value=1.0, step=0.1)
with col2:
weight = st.number_input("Weight (kg)*", min_value=1.0, step=0.1)
st.subheader("Fitness Details")
goal = st.selectbox(
"Fitness Goal",
["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexibility"]
)
level = st.radio(
"Fitness Level",
["Beginner", "Intermediate", "Advanced"]
)
equipment = st.multiselect(
"Available Equipment",
["Dumbbells", "Resistance Band", "Yoga Mat", "No Equipment",
"Kettlebell", "Pull-up Bar"]
)
submit = st.form_submit_button("Submit Profile")
# -------------------------
# HANDLE SUBMISSION
# -------------------------
if submit:
if not name:
st.error("Please enter your name.")
elif height <= 0 or weight <= 0:
st.error("Please enter valid height and weight.")
elif not equipment:
st.error("Please select at least one equipment option.")
else:
st.success("Profile Submitted Successfully!")
# BMI Calculation
bmi = calculate_bmi(weight, height)
bmi_status = get_category(bmi)
st.write(f"### 📊 Your BMI: {bmi} ({bmi_status})")
# Generate Plan
with st.spinner("Generating your 5-day workout plan..."):
prompt = build_prompt(
name=name,
height=height,
weight=weight,
goal=goal,
level=level,
equipment=equipment,
bmi=bmi,
bmi_status=bmi_status
)
full_plan = query_model(prompt)
# Display Plan
st.subheader("🏋️ Your Personalized 5-Day Workout Plan")
st.write(full_plan) |