Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +55 -46
src/streamlit_app.py
CHANGED
|
@@ -1,23 +1,32 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
import torch
|
| 4 |
|
| 5 |
st.set_page_config(page_title="FitPlan AI", layout="centered")
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 11 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
|
| 12 |
-
return tokenizer, model
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# TITLE
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
|
|
|
| 19 |
# PERSONAL INFORMATION
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
name = st.text_input("Enter Your Name")
|
| 23 |
|
|
@@ -30,6 +39,7 @@ gender = st.radio(
|
|
| 30 |
# ---------------------------------------------------
|
| 31 |
# HEIGHT & WEIGHT
|
| 32 |
# ---------------------------------------------------
|
|
|
|
| 33 |
col1, col2 = st.columns(2)
|
| 34 |
|
| 35 |
with col1:
|
|
@@ -50,7 +60,10 @@ with col2:
|
|
| 50 |
step=0.1
|
| 51 |
)
|
| 52 |
|
| 53 |
-
#
|
|
|
|
|
|
|
|
|
|
| 54 |
def bmi_category(bmi):
|
| 55 |
if bmi < 18.5:
|
| 56 |
return "Underweight"
|
|
@@ -61,7 +74,6 @@ def bmi_category(bmi):
|
|
| 61 |
else:
|
| 62 |
return "Obese"
|
| 63 |
|
| 64 |
-
# BMI CALCULATION
|
| 65 |
bmi = None
|
| 66 |
|
| 67 |
if height > 0 and weight > 0:
|
|
@@ -71,7 +83,10 @@ if height > 0 and weight > 0:
|
|
| 71 |
st.metric("π Your BMI", f"{bmi:.2f}")
|
| 72 |
st.info(f"BMI Category: {bmi_category(bmi)}")
|
| 73 |
|
|
|
|
| 74 |
# FITNESS GOAL
|
|
|
|
|
|
|
| 75 |
st.subheader("π― Fitness Goal")
|
| 76 |
|
| 77 |
goal = st.selectbox(
|
|
@@ -85,9 +100,11 @@ goal = st.selectbox(
|
|
| 85 |
]
|
| 86 |
)
|
| 87 |
|
|
|
|
| 88 |
# EQUIPMENT
|
|
|
|
| 89 |
|
| 90 |
-
st.subheader(" Available Equipment")
|
| 91 |
|
| 92 |
equipment_map = {}
|
| 93 |
|
|
@@ -119,8 +136,10 @@ with col3:
|
|
| 119 |
|
| 120 |
equipment = [item for item, selected in equipment_map.items() if selected]
|
| 121 |
|
| 122 |
-
|
| 123 |
# FITNESS LEVEL
|
|
|
|
|
|
|
| 124 |
st.subheader("π Fitness Level")
|
| 125 |
|
| 126 |
fitness_level = st.radio(
|
|
@@ -129,8 +148,11 @@ fitness_level = st.radio(
|
|
| 129 |
horizontal=True
|
| 130 |
)
|
| 131 |
|
|
|
|
| 132 |
# SUBMIT BUTTON
|
| 133 |
-
|
|
|
|
|
|
|
| 134 |
|
| 135 |
if not name:
|
| 136 |
st.error("Please enter your name.")
|
|
@@ -147,49 +169,36 @@ if st.button(" Submit Profile"):
|
|
| 147 |
bmi_status = bmi_category(bmi)
|
| 148 |
equipment_list = ", ".join(equipment)
|
| 149 |
|
| 150 |
-
prompt = f"""
|
| 151 |
You are a certified professional fitness trainer.
|
| 152 |
|
| 153 |
Generate a structured 5-day workout plan.
|
| 154 |
|
| 155 |
-
You MUST follow the format EXACTLY.
|
| 156 |
-
Do NOT add any extra text.
|
| 157 |
-
Do NOT add explanations.
|
| 158 |
-
Do NOT change spacing.
|
| 159 |
-
Do NOT remove indentation.
|
| 160 |
-
|
| 161 |
Return ONLY the workout plan.
|
| 162 |
-
|
| 163 |
-
Strict Format Example:
|
| 164 |
-
|
| 165 |
-
Day 1: Upper Body
|
| 166 |
-
1. Push-Ups
|
| 167 |
-
Sets: 3
|
| 168 |
-
Reps: 10-12
|
| 169 |
-
Rest: 60
|
| 170 |
-
|
| 171 |
-
Now generate for:
|
| 172 |
|
| 173 |
Gender: {gender}
|
| 174 |
BMI: {bmi:.2f} ({bmi_status})
|
| 175 |
Goal: {goal}
|
| 176 |
Fitness Level: {fitness_level}
|
| 177 |
Equipment: {equipment_list}
|
|
|
|
| 178 |
"""
|
| 179 |
|
| 180 |
with st.spinner("Generating your AI workout plan..."):
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
)
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
|
|
|
| 194 |
st.subheader("ποΈ Your Personalized Workout Plan")
|
| 195 |
-
st.
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
|
|
|
| 3 |
|
| 4 |
st.set_page_config(page_title="FitPlan AI", layout="centered")
|
| 5 |
|
| 6 |
+
# ---------------------------------------------------
|
| 7 |
+
# HUGGING FACE API CONFIG
|
| 8 |
+
# ---------------------------------------------------
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 11 |
+
headers = {
|
| 12 |
+
"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"
|
| 13 |
+
}
|
| 14 |
|
| 15 |
+
def query(payload):
|
| 16 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 17 |
+
return response.json()
|
| 18 |
+
|
| 19 |
+
# ---------------------------------------------------
|
| 20 |
# TITLE
|
| 21 |
+
# ---------------------------------------------------
|
| 22 |
+
|
| 23 |
+
st.title("ποΈ FitPlan AI β User Fitness Profile")
|
| 24 |
|
| 25 |
+
# ---------------------------------------------------
|
| 26 |
# PERSONAL INFORMATION
|
| 27 |
+
# ---------------------------------------------------
|
| 28 |
+
|
| 29 |
+
st.subheader("π€ Personal Information")
|
| 30 |
|
| 31 |
name = st.text_input("Enter Your Name")
|
| 32 |
|
|
|
|
| 39 |
# ---------------------------------------------------
|
| 40 |
# HEIGHT & WEIGHT
|
| 41 |
# ---------------------------------------------------
|
| 42 |
+
|
| 43 |
col1, col2 = st.columns(2)
|
| 44 |
|
| 45 |
with col1:
|
|
|
|
| 60 |
step=0.1
|
| 61 |
)
|
| 62 |
|
| 63 |
+
# ---------------------------------------------------
|
| 64 |
+
# BMI FUNCTION
|
| 65 |
+
# ---------------------------------------------------
|
| 66 |
+
|
| 67 |
def bmi_category(bmi):
|
| 68 |
if bmi < 18.5:
|
| 69 |
return "Underweight"
|
|
|
|
| 74 |
else:
|
| 75 |
return "Obese"
|
| 76 |
|
|
|
|
| 77 |
bmi = None
|
| 78 |
|
| 79 |
if height > 0 and weight > 0:
|
|
|
|
| 83 |
st.metric("π Your BMI", f"{bmi:.2f}")
|
| 84 |
st.info(f"BMI Category: {bmi_category(bmi)}")
|
| 85 |
|
| 86 |
+
# ---------------------------------------------------
|
| 87 |
# FITNESS GOAL
|
| 88 |
+
# ---------------------------------------------------
|
| 89 |
+
|
| 90 |
st.subheader("π― Fitness Goal")
|
| 91 |
|
| 92 |
goal = st.selectbox(
|
|
|
|
| 100 |
]
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# ---------------------------------------------------
|
| 104 |
# EQUIPMENT
|
| 105 |
+
# ---------------------------------------------------
|
| 106 |
|
| 107 |
+
st.subheader("ποΈ Available Equipment")
|
| 108 |
|
| 109 |
equipment_map = {}
|
| 110 |
|
|
|
|
| 136 |
|
| 137 |
equipment = [item for item, selected in equipment_map.items() if selected]
|
| 138 |
|
| 139 |
+
# ---------------------------------------------------
|
| 140 |
# FITNESS LEVEL
|
| 141 |
+
# ---------------------------------------------------
|
| 142 |
+
|
| 143 |
st.subheader("π Fitness Level")
|
| 144 |
|
| 145 |
fitness_level = st.radio(
|
|
|
|
| 148 |
horizontal=True
|
| 149 |
)
|
| 150 |
|
| 151 |
+
# ---------------------------------------------------
|
| 152 |
# SUBMIT BUTTON
|
| 153 |
+
# ---------------------------------------------------
|
| 154 |
+
|
| 155 |
+
if st.button("π Submit Profile"):
|
| 156 |
|
| 157 |
if not name:
|
| 158 |
st.error("Please enter your name.")
|
|
|
|
| 169 |
bmi_status = bmi_category(bmi)
|
| 170 |
equipment_list = ", ".join(equipment)
|
| 171 |
|
| 172 |
+
prompt = f"""<s>[INST]
|
| 173 |
You are a certified professional fitness trainer.
|
| 174 |
|
| 175 |
Generate a structured 5-day workout plan.
|
| 176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
Return ONLY the workout plan.
|
| 178 |
+
Do not add explanations.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
Gender: {gender}
|
| 181 |
BMI: {bmi:.2f} ({bmi_status})
|
| 182 |
Goal: {goal}
|
| 183 |
Fitness Level: {fitness_level}
|
| 184 |
Equipment: {equipment_list}
|
| 185 |
+
[/INST]
|
| 186 |
"""
|
| 187 |
|
| 188 |
with st.spinner("Generating your AI workout plan..."):
|
| 189 |
|
| 190 |
+
output = query({
|
| 191 |
+
"inputs": prompt,
|
| 192 |
+
"parameters": {
|
| 193 |
+
"max_new_tokens": 600,
|
| 194 |
+
"temperature": 0.3
|
| 195 |
+
}
|
| 196 |
+
})
|
| 197 |
+
|
| 198 |
+
if isinstance(output, list):
|
| 199 |
+
response = output[0]["generated_text"]
|
| 200 |
+
else:
|
| 201 |
+
response = "Error generating plan. Please try again."
|
| 202 |
+
|
| 203 |
st.subheader("ποΈ Your Personalized Workout Plan")
|
| 204 |
+
st.markdown(response)
|