Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +74 -54
src/streamlit_app.py
CHANGED
|
@@ -1,34 +1,34 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from transformers import
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
return pipeline("text-generation", model="google/flan-t5-base")
|
| 8 |
|
| 9 |
-
|
| 10 |
|
| 11 |
-
st.
|
| 12 |
-
st.title("💪 FitPlan AI - Fitness Profile & BMI Calculator")
|
| 13 |
|
| 14 |
-
st.write("Fill
|
| 15 |
|
| 16 |
# -------------------------
|
| 17 |
-
#
|
| 18 |
# -------------------------
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
weight_kg = st.number_input("Enter Weight (in kilograms) *", min_value=0.0, format="%.2f")
|
| 26 |
|
| 27 |
# -------------------------
|
| 28 |
-
#
|
| 29 |
# -------------------------
|
| 30 |
|
| 31 |
-
st.
|
| 32 |
|
| 33 |
goal = st.selectbox(
|
| 34 |
"Select Your Fitness Goal",
|
|
@@ -46,56 +46,76 @@ fitness_level = st.radio(
|
|
| 46 |
)
|
| 47 |
|
| 48 |
# -------------------------
|
| 49 |
-
#
|
| 50 |
# -------------------------
|
| 51 |
|
| 52 |
-
def
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# -------------------------
|
| 68 |
-
#
|
| 69 |
# -------------------------
|
| 70 |
|
| 71 |
-
if st.button("
|
| 72 |
|
| 73 |
-
# Validation
|
| 74 |
if not name:
|
| 75 |
-
st.error("
|
| 76 |
-
elif height_cm <= 0 or weight_kg <= 0:
|
| 77 |
-
st.error("Enter valid height and weight.")
|
| 78 |
elif not equipment:
|
| 79 |
-
st.error("Select at least one equipment
|
| 80 |
else:
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# Calculate BMI
|
| 84 |
-
bmi = calculate_bmi(weight_kg, height_cm)
|
| 85 |
-
bmi_status = bmi_category(bmi)
|
| 86 |
-
|
| 87 |
-
st.write(f"### 📊 BMI: {bmi} ({bmi_status})")
|
| 88 |
|
| 89 |
-
|
| 90 |
|
| 91 |
-
|
| 92 |
-
Generate a 5-day structured workout plan.
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
result = generator(prompt, max_new_tokens=400)[0]["generated_text"]
|
| 99 |
|
| 100 |
-
st.
|
| 101 |
st.write(result)
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# -------------------------
|
| 6 |
+
# Page Config
|
| 7 |
+
# -------------------------
|
|
|
|
| 8 |
|
| 9 |
+
st.set_page_config(page_title="FitPlan AI", page_icon="💪")
|
| 10 |
|
| 11 |
+
st.title("💪 FitPlan AI — Personalized Workout Generator")
|
|
|
|
| 12 |
|
| 13 |
+
st.write("Fill your details to generate AI workout plan.")
|
| 14 |
|
| 15 |
# -------------------------
|
| 16 |
+
# Load Model (Small for HF Spaces)
|
| 17 |
# -------------------------
|
| 18 |
|
| 19 |
+
@st.cache_resource
|
| 20 |
+
def load_model():
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 22 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
|
| 23 |
+
return tokenizer, model
|
| 24 |
|
| 25 |
+
tokenizer, model = load_model()
|
|
|
|
| 26 |
|
| 27 |
# -------------------------
|
| 28 |
+
# User Inputs
|
| 29 |
# -------------------------
|
| 30 |
|
| 31 |
+
name = st.text_input("Enter Your Name")
|
| 32 |
|
| 33 |
goal = st.selectbox(
|
| 34 |
"Select Your Fitness Goal",
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
# -------------------------
|
| 49 |
+
# Function: Create Prompt
|
| 50 |
# -------------------------
|
| 51 |
|
| 52 |
+
def create_prompt(goal, equipment, fitness_level):
|
| 53 |
+
|
| 54 |
+
equipment_list = ", ".join(equipment)
|
| 55 |
+
|
| 56 |
+
# Goal instructions
|
| 57 |
+
goal_map = {
|
| 58 |
+
"Build Muscle": "Focus on muscle hypertrophy training.",
|
| 59 |
+
"Weight Loss": "Focus on fat burning and cardio workouts.",
|
| 60 |
+
"Strength Gain": "Focus on heavy compound strength exercises.",
|
| 61 |
+
"Abs Building": "Focus on core and abdominal exercises.",
|
| 62 |
+
"Flexible": "Focus on stretching and mobility exercises."
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
# Level instructions
|
| 66 |
+
level_map = {
|
| 67 |
+
"Beginner": "Use simple low intensity exercises.",
|
| 68 |
+
"Intermediate": "Use moderate intensity exercises.",
|
| 69 |
+
"Advanced": "Use high intensity advanced exercises."
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
prompt = f"""
|
| 73 |
+
Generate a 5-day workout plan.
|
| 74 |
+
|
| 75 |
+
Goal: {goal}
|
| 76 |
+
Fitness Level: {fitness_level}
|
| 77 |
+
Equipment: {equipment_list}
|
| 78 |
+
|
| 79 |
+
Goal Instruction:
|
| 80 |
+
{goal_map[goal]}
|
| 81 |
+
|
| 82 |
+
Level Instruction:
|
| 83 |
+
{level_map[fitness_level]}
|
| 84 |
+
|
| 85 |
+
Requirements:
|
| 86 |
+
- Include warm-up
|
| 87 |
+
- Exercises with sets and reps
|
| 88 |
+
- Rest time
|
| 89 |
+
- Day-wise structure
|
| 90 |
+
"""
|
| 91 |
+
|
| 92 |
+
return prompt
|
| 93 |
|
| 94 |
# -------------------------
|
| 95 |
+
# Generate Plan
|
| 96 |
# -------------------------
|
| 97 |
|
| 98 |
+
if st.button("Generate Workout Plan"):
|
| 99 |
|
|
|
|
| 100 |
if not name:
|
| 101 |
+
st.error("Enter your name")
|
|
|
|
|
|
|
| 102 |
elif not equipment:
|
| 103 |
+
st.error("Select at least one equipment")
|
| 104 |
else:
|
| 105 |
+
prompt = create_prompt(goal, equipment, fitness_level)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
with st.spinner("Generating your AI workout plan..."):
|
| 108 |
|
| 109 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
|
|
|
| 110 |
|
| 111 |
+
outputs = model.generate(
|
| 112 |
+
**inputs,
|
| 113 |
+
max_new_tokens=300,
|
| 114 |
+
temperature=0.7,
|
| 115 |
+
do_sample=True
|
| 116 |
+
)
|
| 117 |
|
| 118 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 119 |
|
| 120 |
+
st.success(f"Workout Plan for {name}")
|
| 121 |
st.write(result)
|