Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,81 @@
|
|
| 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="
|
| 9 |
|
| 10 |
-
# 2.
|
| 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 |
-
|
| 29 |
-
color: #000000 !important;
|
| 30 |
}
|
| 31 |
.day-card {
|
| 32 |
background-color: #ffffff !important; padding: 20px; border-radius: 12px;
|
| 33 |
-
margin-bottom: 15px; border-left:
|
| 34 |
white-space: pre-wrap !important;
|
| 35 |
}
|
| 36 |
</style>
|
| 37 |
""", unsafe_allow_html=True)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
with st.sidebar:
|
| 41 |
-
st.markdown("# 👤 Personalised Fitness Plan")
|
| 42 |
-
st.write("---")
|
| 43 |
-
st.write("Your digital coach.")
|
| 44 |
|
| 45 |
-
# ---
|
| 46 |
-
st.
|
| 47 |
-
|
| 48 |
-
st.
|
| 49 |
-
|
| 50 |
-
with
|
| 51 |
-
name = st.text_input("Full Name *", placeholder="e.g. John Doe")
|
| 52 |
-
with col_gender:
|
| 53 |
-
gender = st.selectbox("Gender *", ["Male", "Female", "Other"])
|
| 54 |
-
with col_age:
|
| 55 |
-
age = st.number_input("Age *", min_value=1, max_value=120, value=25)
|
| 56 |
|
| 57 |
col_h, col_w = st.columns(2)
|
| 58 |
-
with col_h:
|
| 59 |
-
|
| 60 |
-
with col_w:
|
| 61 |
-
weight_kg = st.number_input("Weight in kilograms *", min_value=0.0, step=0.1)
|
| 62 |
|
| 63 |
-
st.
|
| 64 |
-
st.
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
equipment = st.multiselect("Available Equipment *", ["Dumbbells", "Resistance Band", "Yoga Mat", "Kettlebells", "Pull-up Bar", "No Equipment"], default=["No Equipment"])
|
| 68 |
|
| 69 |
-
# ---
|
| 70 |
-
if st.button("
|
| 71 |
-
if not name or
|
| 72 |
-
st.error("Please fill in all
|
| 73 |
else:
|
| 74 |
-
#
|
| 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 |
-
#
|
| 82 |
st.markdown(f"""
|
| 83 |
<div class="result-card">
|
| 84 |
-
<h2
|
| 85 |
-
<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 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 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 |
-
|
| 105 |
days = ["Day 1:", "Day 2:", "Day 3:", "Day 4:", "Day 5:"]
|
|
|
|
| 106 |
for i in range(len(days)):
|
| 107 |
-
start =
|
| 108 |
if start != -1:
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
st.markdown(f'<div class="day-card">{day_content}</div>', unsafe_allow_html=True)
|
| 112 |
-
|
| 113 |
-
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 2 |
from prompt_builder import build_prompt
|
| 3 |
+
from model_api import query_model # Import your API function
|
| 4 |
|
| 5 |
# 1. Page Configuration
|
| 6 |
+
st.set_page_config(page_title="FitPlan AI", layout="wide")
|
| 7 |
|
| 8 |
+
# 2. Custom Styling (Forced Black Text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
st.markdown("""
|
| 10 |
<style>
|
| 11 |
.stApp { background: linear-gradient(135deg, #f6d365 0%, #fda085 100%); background-attachment: fixed; }
|
|
|
|
|
|
|
| 12 |
h1, h2, h3, p, label { color: #2D3436 !important; font-weight: 600; }
|
| 13 |
.result-card {
|
| 14 |
+
background-color: white !important; padding: 25px; border-radius: 15px;
|
| 15 |
+
border-left: 10px solid #004d57; color: #000000 !important; margin-bottom: 20px;
|
|
|
|
| 16 |
}
|
| 17 |
.day-card {
|
| 18 |
background-color: #ffffff !important; padding: 20px; border-radius: 12px;
|
| 19 |
+
margin-bottom: 15px; border-left: 8px solid #004d57; color: #000000 !important;
|
| 20 |
white-space: pre-wrap !important;
|
| 21 |
}
|
| 22 |
</style>
|
| 23 |
""", unsafe_allow_html=True)
|
| 24 |
|
| 25 |
+
st.title("🏋️ Personalised Fitness Plan")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# --- INPUT SECTION ---
|
| 28 |
+
st.markdown("### 1️⃣ User Information")
|
| 29 |
+
col1, col2, col3 = st.columns([2, 1, 1])
|
| 30 |
+
with col1: name = st.text_input("Name")
|
| 31 |
+
with col2: gender = st.selectbox("Gender", ["Male", "Female", "Other"])
|
| 32 |
+
with col3: age = st.number_input("Age", min_value=1, value=25)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
col_h, col_w = st.columns(2)
|
| 35 |
+
with col_h: height = st.number_input("Height (cm)", min_value=10.0)
|
| 36 |
+
with col_w: weight = st.number_input("Weight (kg)", min_value=1.0)
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
st.markdown("### 2️⃣ Training Goals")
|
| 39 |
+
goal = st.selectbox("Goal", ["Build Muscle", "Weight Loss", "Strength", "Flexibility"])
|
| 40 |
+
level = st.selectbox("Fitness Level", ["Beginner", "Intermediate", "Advanced"])
|
| 41 |
+
equip = st.multiselect("Equipment", ["Dumbbells", "Barbell", "Resistance Bands", "Yoga Mat", "No Equipment"])
|
|
|
|
| 42 |
|
| 43 |
+
# --- GENERATION ---
|
| 44 |
+
if st.button("Generate My Professional Plan"):
|
| 45 |
+
if not name or height <= 0 or weight <= 0:
|
| 46 |
+
st.error("Please fill in all details.")
|
| 47 |
else:
|
| 48 |
+
# Build the prompt using your external file
|
| 49 |
+
prompt, bmi, bmi_status, status_color = build_prompt(name, gender, height, weight, goal, level, equip)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Show Assessment
|
| 52 |
st.markdown(f"""
|
| 53 |
<div class="result-card">
|
| 54 |
+
<h2>Assessment for {name}</h2>
|
| 55 |
+
<p>BMI: <strong>{bmi:.2f}</strong> | Category: <span style="color:{status_color}">{bmi_status}</span></p>
|
|
|
|
| 56 |
</div>
|
| 57 |
""", unsafe_allow_html=True)
|
| 58 |
|
| 59 |
+
with st.spinner("AI Trainer is calculating your routine..."):
|
| 60 |
+
# Call your model_api.py function
|
| 61 |
+
raw_workout = query_model(prompt)
|
| 62 |
+
|
| 63 |
+
if "Error" in raw_workout:
|
| 64 |
+
st.error(raw_workout)
|
| 65 |
+
else:
|
| 66 |
+
st.markdown("### 📋 Your 5-Day Routine")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
# Visual Parsing logic
|
| 69 |
days = ["Day 1:", "Day 2:", "Day 3:", "Day 4:", "Day 5:"]
|
| 70 |
+
|
| 71 |
for i in range(len(days)):
|
| 72 |
+
start = raw_workout.find(days[i])
|
| 73 |
if start != -1:
|
| 74 |
+
# Logic to find the end of the day's text
|
| 75 |
+
end = raw_workout.find(days[i+1]) if i+1 < len(days) else len(raw_workout)
|
| 76 |
+
day_content = raw_workout[start:end].strip()
|
| 77 |
+
|
| 78 |
+
# Render in Black Text Card
|
| 79 |
st.markdown(f'<div class="day-card">{day_content}</div>', unsafe_allow_html=True)
|
| 80 |
+
|
| 81 |
+
st.info("Note: Consult a doctor before starting any new exercise program.")
|