Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,50 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# 1. Page Configuration
|
| 6 |
st.set_page_config(page_title="Fitness Profile Generator", layout="wide")
|
| 7 |
|
| 8 |
-
# 2. Load AI Model
|
| 9 |
@st.cache_resource
|
| 10 |
def load_model():
|
| 11 |
-
# Note: flan-t5-large is ~3GB. Ensure your Hugging Face Space has enough RAM.
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
|
| 13 |
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
|
| 14 |
return tokenizer, model
|
| 15 |
|
| 16 |
tokenizer, model = load_model()
|
| 17 |
|
| 18 |
-
# 3. Custom Styling (
|
| 19 |
st.markdown("""
|
| 20 |
<style>
|
| 21 |
-
.stApp {
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
-
|
| 26 |
-
background-color: #
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
color: #FFFFFF !important;
|
| 30 |
-
}
|
| 31 |
-
h1, h2, h3, p, label {
|
| 32 |
-
color: #2D3436 !important;
|
| 33 |
-
font-weight: 600;
|
| 34 |
-
}
|
| 35 |
-
div[data-baseweb="input"], div[data-baseweb="select"] {
|
| 36 |
-
background-color: rgba(255, 255, 255, 0.9) !important;
|
| 37 |
-
border-radius: 8px !important;
|
| 38 |
-
}
|
| 39 |
-
.stButton > button {
|
| 40 |
-
background-color: #004d57 !important;
|
| 41 |
-
color: white !important;
|
| 42 |
-
border-radius: 8px !important;
|
| 43 |
-
padding: 10px 24px !important;
|
| 44 |
-
font-weight: bold !important;
|
| 45 |
-
width: 100% !important;
|
| 46 |
-
border: none !important;
|
| 47 |
-
}
|
| 48 |
-
.result-card {
|
| 49 |
-
background-color: white;
|
| 50 |
-
padding: 25px;
|
| 51 |
-
border-radius: 15px;
|
| 52 |
-
box-shadow: 0 10px 25px rgba(0,0,0,0.1);
|
| 53 |
-
margin-top: 25px;
|
| 54 |
-
border-left: 10px solid #004d57;
|
| 55 |
}
|
| 56 |
</style>
|
| 57 |
""", unsafe_allow_html=True)
|
| 58 |
|
| 59 |
# --- SIDEBAR ---
|
| 60 |
with st.sidebar:
|
| 61 |
-
st.markdown("# 👤 Personalised Fitness Plan
|
| 62 |
st.write("---")
|
| 63 |
-
st.write("Your digital coach
|
| 64 |
|
| 65 |
# --- MAIN CONTENT ---
|
| 66 |
st.title("🏋️ Create Your Fitness Profile")
|
| 67 |
|
| 68 |
-
# SECTION 1: PERSONAL INFORMATION
|
| 69 |
st.markdown("### 1️⃣ Personal Information")
|
| 70 |
col_name, col_gender, col_age = st.columns([2, 1, 1])
|
| 71 |
with col_name:
|
|
@@ -82,51 +61,53 @@ with col_w:
|
|
| 82 |
weight_kg = st.number_input("Weight in kilograms *", min_value=0.0, step=0.1)
|
| 83 |
|
| 84 |
st.write("---")
|
| 85 |
-
|
| 86 |
-
# SECTION 2: FITNESS DETAILS
|
| 87 |
st.markdown("### 2️⃣ Fitness Details")
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
with col_level:
|
| 92 |
-
fitness_level = st.selectbox("Fitness Level", ["Beginner", "Intermediate", "Advanced"])
|
| 93 |
-
|
| 94 |
-
equipment = st.multiselect(
|
| 95 |
-
"Available Equipment *",
|
| 96 |
-
["Dumbbells", "Resistance Band", "Yoga Mat", "Kettlebells", "Pull-up Bar", "No Equipment"],
|
| 97 |
-
default=["No Equipment"]
|
| 98 |
-
)
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
# --- SUBMIT BUTTON & LOGIC ---
|
| 103 |
if st.button("Submit Profile"):
|
| 104 |
-
if not name:
|
| 105 |
-
st.error("Please
|
| 106 |
-
elif height_cm <= 0 or weight_kg <= 0:
|
| 107 |
-
st.error("Please enter valid height and weight.")
|
| 108 |
-
elif not equipment:
|
| 109 |
-
st.error("Please select at least one equipment option.")
|
| 110 |
else:
|
| 111 |
-
|
| 112 |
-
bmi
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
if bmi_rounded < 18.5:
|
| 116 |
-
bmi_status, status_color = "Underweight", "#3498db"
|
| 117 |
-
elif 18.5 <= bmi_rounded < 25:
|
| 118 |
-
bmi_status, status_color = "Normal", "#2ecc71"
|
| 119 |
-
elif 25 <= bmi_rounded < 30:
|
| 120 |
-
bmi_status, status_color = "Overweight", "#f1c40f"
|
| 121 |
-
else:
|
| 122 |
-
bmi_status, status_color = "Obese", "#e74c3c"
|
| 123 |
|
| 124 |
st.success("Profile Submitted Successfully!")
|
| 125 |
|
|
|
|
| 126 |
st.markdown(f"""
|
| 127 |
<div class="result-card">
|
| 128 |
<h2 style="color: #004d57; margin-top:0;">Assessment for {name}</h2>
|
| 129 |
<p>{gender} | {age} Years Old</p>
|
| 130 |
-
<p>Calculated BMI: <strong>{
|
| 131 |
</div>
|
| 132 |
-
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import torch
|
| 4 |
+
# IMPORT your new prompt builder functions
|
| 5 |
+
from prompt_builder import build_prompt
|
| 6 |
|
| 7 |
# 1. Page Configuration
|
| 8 |
st.set_page_config(page_title="Fitness Profile Generator", layout="wide")
|
| 9 |
|
| 10 |
+
# 2. Load AI Model
|
| 11 |
@st.cache_resource
|
| 12 |
def load_model():
|
|
|
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
|
| 14 |
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
|
| 15 |
return tokenizer, model
|
| 16 |
|
| 17 |
tokenizer, model = load_model()
|
| 18 |
|
| 19 |
+
# 3. Custom Styling (Same as your original)
|
| 20 |
st.markdown("""
|
| 21 |
<style>
|
| 22 |
+
.stApp { background: linear-gradient(135deg, #f6d365 0%, #fda085 100%); background-attachment: fixed; }
|
| 23 |
+
section[data-testid="stSidebar"] { background-color: #000000 !important; }
|
| 24 |
+
section[data-testid="stSidebar"] h1, section[data-testid="stSidebar"] p { color: #FFFFFF !important; }
|
| 25 |
+
h1, h2, h3, p, label { color: #2D3436 !important; font-weight: 600; }
|
| 26 |
+
.result-card {
|
| 27 |
+
background-color: white; padding: 25px; border-radius: 15px;
|
| 28 |
+
box-shadow: 0 10px 25px rgba(0,0,0,0.1); margin-top: 25px; border-left: 10px solid #004d57;
|
| 29 |
+
color: #000000 !important;
|
| 30 |
}
|
| 31 |
+
.day-card {
|
| 32 |
+
background-color: #ffffff !important; padding: 20px; border-radius: 12px;
|
| 33 |
+
margin-bottom: 15px; border-left: 5px solid #004d57; color: #000000 !important;
|
| 34 |
+
white-space: pre-wrap !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
</style>
|
| 37 |
""", unsafe_allow_html=True)
|
| 38 |
|
| 39 |
# --- SIDEBAR ---
|
| 40 |
with st.sidebar:
|
| 41 |
+
st.markdown("# 👤 Personalised Fitness Plan")
|
| 42 |
st.write("---")
|
| 43 |
+
st.write("Your digital coach.")
|
| 44 |
|
| 45 |
# --- MAIN CONTENT ---
|
| 46 |
st.title("🏋️ Create Your Fitness Profile")
|
| 47 |
|
|
|
|
| 48 |
st.markdown("### 1️⃣ Personal Information")
|
| 49 |
col_name, col_gender, col_age = st.columns([2, 1, 1])
|
| 50 |
with col_name:
|
|
|
|
| 61 |
weight_kg = st.number_input("Weight in kilograms *", min_value=0.0, step=0.1)
|
| 62 |
|
| 63 |
st.write("---")
|
|
|
|
|
|
|
| 64 |
st.markdown("### 2️⃣ Fitness Details")
|
| 65 |
+
goal = st.selectbox("Fitness Goal", ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"])
|
| 66 |
+
fitness_level = st.selectbox("Fitness Level", ["Beginner", "Intermediate", "Advanced"])
|
| 67 |
+
equipment = st.multiselect("Available Equipment *", ["Dumbbells", "Resistance Band", "Yoga Mat", "Kettlebells", "Pull-up Bar", "No Equipment"], default=["No Equipment"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# --- SUBMIT BUTTON ---
|
|
|
|
|
|
|
| 70 |
if st.button("Submit Profile"):
|
| 71 |
+
if not name or height_cm <= 0 or weight_kg <= 0 or not equipment:
|
| 72 |
+
st.error("Please fill in all required fields.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
else:
|
| 74 |
+
# CALL the function from prompt_builder.py
|
| 75 |
+
prompt, bmi, bmi_status, status_color = build_prompt(
|
| 76 |
+
name, gender, height_cm, weight_kg, goal, fitness_level, equipment
|
| 77 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
st.success("Profile Submitted Successfully!")
|
| 80 |
|
| 81 |
+
# Display Assessment Card
|
| 82 |
st.markdown(f"""
|
| 83 |
<div class="result-card">
|
| 84 |
<h2 style="color: #004d57; margin-top:0;">Assessment for {name}</h2>
|
| 85 |
<p>{gender} | {age} Years Old</p>
|
| 86 |
+
<p>Calculated BMI: <strong>{bmi:.2f}</strong> | Category: <span style="color: {status_color}; font-weight: bold;">{bmi_status}</span></p>
|
| 87 |
</div>
|
| 88 |
+
""", unsafe_allow_html=True)
|
| 89 |
+
|
| 90 |
+
# AI Generation
|
| 91 |
+
with st.spinner("Generating your aligned workout plan..."):
|
| 92 |
+
try:
|
| 93 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
|
| 94 |
+
outputs = model.generate(
|
| 95 |
+
**inputs, max_new_tokens=600, min_new_tokens=250,
|
| 96 |
+
do_sample=True, temperature=0.7, repetition_penalty=2.5
|
| 97 |
+
)
|
| 98 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
| 99 |
+
|
| 100 |
+
# Cleanup and Display
|
| 101 |
+
clean_result = result.split("Day 6")[0]
|
| 102 |
+
full_text = "Day 1: " + clean_result
|
| 103 |
+
|
| 104 |
+
st.markdown("### 📋 Your Personalized Routine")
|
| 105 |
+
days = ["Day 1:", "Day 2:", "Day 3:", "Day 4:", "Day 5:"]
|
| 106 |
+
for i in range(len(days)):
|
| 107 |
+
start = full_text.find(days[i])
|
| 108 |
+
if start != -1:
|
| 109 |
+
end = full_text.find(days[i+1]) if i+1 < len(days) else len(full_text)
|
| 110 |
+
day_content = full_text[start:end].strip()
|
| 111 |
+
st.markdown(f'<div class="day-card">{day_content}</div>', unsafe_allow_html=True)
|
| 112 |
+
except Exception as e:
|
| 113 |
+
st.error(f"Generation failed: {e}")
|