Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +51 -136
src/streamlit_app.py
CHANGED
|
@@ -1,168 +1,83 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
-
import torch
|
| 4 |
-
import time
|
| 5 |
|
| 6 |
# -------------------------
|
| 7 |
-
#
|
| 8 |
# -------------------------
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# -------------------------
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return tokenizer, model
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
# Title
|
| 26 |
-
# -------------------------
|
| 27 |
|
| 28 |
-
st.title("💪 FitPlan AI - BMI Calculator & Workout Planner")
|
| 29 |
|
| 30 |
# -------------------------
|
| 31 |
-
#
|
| 32 |
# -------------------------
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
# -------------------------
|
| 40 |
-
#
|
| 41 |
# -------------------------
|
| 42 |
|
| 43 |
-
|
| 44 |
-
"Fitness Goal",
|
| 45 |
-
["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"]
|
| 46 |
-
)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
"Available Equipment",
|
| 50 |
-
["Dumbbells", "Resistance Band", "Yoga Mat", "Skipping Rope",
|
| 51 |
-
"Weight Plates", "Cycling", "Inclined Bench", "Pullups Bar", "No Equipment"]
|
| 52 |
-
)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
# BMI Functions
|
| 61 |
-
# -------------------------
|
| 62 |
|
| 63 |
-
|
| 64 |
-
height_m = height / 100
|
| 65 |
-
return round(weight / (height_m ** 2), 2)
|
| 66 |
|
| 67 |
-
def bmi_category(bmi):
|
| 68 |
-
if bmi < 18.5:
|
| 69 |
-
return "Underweight"
|
| 70 |
-
elif bmi < 24.9:
|
| 71 |
-
return "Normal"
|
| 72 |
-
elif bmi < 29.9:
|
| 73 |
-
return "Overweight"
|
| 74 |
-
else:
|
| 75 |
-
return "Obese"
|
| 76 |
|
| 77 |
# -------------------------
|
| 78 |
-
#
|
| 79 |
# -------------------------
|
| 80 |
|
| 81 |
-
if
|
| 82 |
-
|
| 83 |
-
# Validation
|
| 84 |
-
if not name:
|
| 85 |
-
st.error("Enter your name")
|
| 86 |
-
elif height_cm <= 0 or weight_kg <= 0:
|
| 87 |
-
st.error("Enter valid height & weight")
|
| 88 |
-
elif not equipment:
|
| 89 |
-
st.error("Select equipment")
|
| 90 |
-
else:
|
| 91 |
-
|
| 92 |
-
# BMI
|
| 93 |
-
bmi = calculate_bmi(weight_kg, height_cm)
|
| 94 |
-
bmi_status = bmi_category(bmi)
|
| 95 |
-
|
| 96 |
-
st.success(f"✅ Welcome {name}! BMI: {bmi} ({bmi_status})")
|
| 97 |
-
|
| 98 |
-
# -------------------------
|
| 99 |
-
# Progress Bar Animation
|
| 100 |
-
# -------------------------
|
| 101 |
-
|
| 102 |
-
progress = st.progress(0)
|
| 103 |
-
status = st.empty()
|
| 104 |
-
|
| 105 |
-
for i in range(100):
|
| 106 |
-
time.sleep(0.01)
|
| 107 |
-
progress.progress(i + 1)
|
| 108 |
-
status.text("Generating personalized fitness plan...")
|
| 109 |
-
|
| 110 |
-
st.divider()
|
| 111 |
-
|
| 112 |
-
# -------------------------
|
| 113 |
-
# Core AI Module Layout
|
| 114 |
-
# -------------------------
|
| 115 |
-
|
| 116 |
-
st.subheader("🧠 Core AI Inference Module")
|
| 117 |
-
|
| 118 |
-
col1, col2, col3 = st.columns(3)
|
| 119 |
-
|
| 120 |
-
with col1:
|
| 121 |
-
st.info("🔽 Input Layer\nUser goals, equipment, fitness level")
|
| 122 |
-
|
| 123 |
-
with col2:
|
| 124 |
-
st.info("⚙ Processing\nAI model analysis")
|
| 125 |
-
|
| 126 |
-
with col3:
|
| 127 |
-
st.info("📤 Output Layer\nWorkout plan")
|
| 128 |
-
|
| 129 |
-
st.divider()
|
| 130 |
-
|
| 131 |
-
# -------------------------
|
| 132 |
-
# Model Input & Output
|
| 133 |
-
# -------------------------
|
| 134 |
-
|
| 135 |
-
equipment_list = ", ".join(equipment)
|
| 136 |
-
|
| 137 |
-
prompt = f"""
|
| 138 |
-
Create a structured 5-day workout plan.
|
| 139 |
-
|
| 140 |
-
User:
|
| 141 |
-
Goal: {goal}
|
| 142 |
-
Fitness Level: {fitness_level}
|
| 143 |
-
Equipment: {equipment_list}
|
| 144 |
-
|
| 145 |
-
Include exercises with sets, reps, and rest time.
|
| 146 |
-
"""
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
)
|
| 156 |
-
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 157 |
|
| 158 |
-
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
st.write(prompt)
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
st.write(result)
|
| 167 |
|
| 168 |
-
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# -------------------------
|
| 4 |
+
# 1. Function to Craft Prompt
|
| 5 |
# -------------------------
|
| 6 |
|
| 7 |
+
def create_workout_prompt(days, level, goal, equipment):
|
| 8 |
+
"""
|
| 9 |
+
Creates a detailed prompt for the AI model
|
| 10 |
+
"""
|
| 11 |
|
| 12 |
+
prompt = f"""
|
| 13 |
+
Generate a {days}-day workout plan.
|
|
|
|
| 14 |
|
| 15 |
+
User Details:
|
| 16 |
+
Fitness Level: {level}
|
| 17 |
+
Goal: {goal}
|
| 18 |
+
Available Equipment: {equipment}
|
|
|
|
| 19 |
|
| 20 |
+
Requirements:
|
| 21 |
+
- Include warm-up
|
| 22 |
+
- Include exercises with sets and reps
|
| 23 |
+
- Include rest time
|
| 24 |
+
- Keep it structured day-wise
|
| 25 |
+
"""
|
| 26 |
|
| 27 |
+
return prompt
|
|
|
|
|
|
|
| 28 |
|
|
|
|
| 29 |
|
| 30 |
# -------------------------
|
| 31 |
+
# 2. Load Model
|
| 32 |
# -------------------------
|
| 33 |
|
| 34 |
+
def load_model():
|
| 35 |
+
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 36 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
|
| 37 |
+
return tokenizer, model
|
| 38 |
+
|
| 39 |
|
| 40 |
# -------------------------
|
| 41 |
+
# 3. Generate Response
|
| 42 |
# -------------------------
|
| 43 |
|
| 44 |
+
def generate_workout_plan(prompt, tokenizer, model):
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
outputs = model.generate(
|
| 49 |
+
**inputs,
|
| 50 |
+
max_new_tokens=300,
|
| 51 |
+
temperature=0.7,
|
| 52 |
+
do_sample=True
|
| 53 |
+
)
|
| 54 |
|
| 55 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
return result
|
|
|
|
|
|
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
# -------------------------
|
| 61 |
+
# 4. Example Usage
|
| 62 |
# -------------------------
|
| 63 |
|
| 64 |
+
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# Create prompt from user input
|
| 67 |
+
prompt = create_workout_prompt(
|
| 68 |
+
days=5,
|
| 69 |
+
level="Beginner",
|
| 70 |
+
goal="Build Muscle",
|
| 71 |
+
equipment="Dumbbells"
|
| 72 |
+
)
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
print("Generated Prompt:\n", prompt)
|
| 75 |
|
| 76 |
+
# Load model
|
| 77 |
+
tokenizer, model = load_model()
|
|
|
|
| 78 |
|
| 79 |
+
# Generate text response
|
| 80 |
+
workout_plan = generate_workout_plan(prompt, tokenizer, model)
|
|
|
|
| 81 |
|
| 82 |
+
print("\nAI Workout Plan:\n")
|
| 83 |
+
print(workout_plan)
|