Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +34 -12
src/streamlit_app.py
CHANGED
|
@@ -8,13 +8,23 @@ st.set_page_config(page_title="FitPlan AI", layout="centered")
|
|
| 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 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# ---------------------------------------------------
|
| 20 |
# TITLE
|
|
@@ -174,16 +184,20 @@ You are a certified professional fitness trainer.
|
|
| 174 |
|
| 175 |
Generate a structured 5-day workout plan.
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
@@ -191,14 +205,22 @@ Equipment: {equipment_list}
|
|
| 191 |
"inputs": prompt,
|
| 192 |
"parameters": {
|
| 193 |
"max_new_tokens": 600,
|
| 194 |
-
"temperature": 0.3
|
|
|
|
| 195 |
}
|
| 196 |
})
|
| 197 |
|
| 198 |
-
if isinstance(output,
|
| 199 |
-
response = output[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
else:
|
| 201 |
-
response = "
|
| 202 |
|
| 203 |
st.subheader("🏋️ Your Personalized Workout Plan")
|
| 204 |
st.markdown(response)
|
|
|
|
| 8 |
# ---------------------------------------------------
|
| 9 |
|
| 10 |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 11 |
+
|
| 12 |
headers = {
|
| 13 |
+
"Authorization": f"Bearer {st.secrets['HF_TOKEN']}",
|
| 14 |
+
"Content-Type": "application/json"
|
| 15 |
}
|
| 16 |
|
| 17 |
def query(payload):
|
| 18 |
+
try:
|
| 19 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
|
| 20 |
+
|
| 21 |
+
if response.status_code != 200:
|
| 22 |
+
return {"error": f"API Error {response.status_code}: {response.text}"}
|
| 23 |
+
|
| 24 |
+
return response.json()
|
| 25 |
+
|
| 26 |
+
except requests.exceptions.RequestException as e:
|
| 27 |
+
return {"error": f"Request failed: {str(e)}"}
|
| 28 |
|
| 29 |
# ---------------------------------------------------
|
| 30 |
# TITLE
|
|
|
|
| 184 |
|
| 185 |
Generate a structured 5-day workout plan.
|
| 186 |
|
| 187 |
+
Rules:
|
| 188 |
+
- Clearly label Day 1 to Day 5
|
| 189 |
+
- Include exercises
|
| 190 |
+
- Include sets and reps
|
| 191 |
+
- Include short rest guidance
|
| 192 |
+
- Return only the workout plan
|
| 193 |
|
| 194 |
+
User Details:
|
| 195 |
Gender: {gender}
|
| 196 |
BMI: {bmi:.2f} ({bmi_status})
|
| 197 |
Goal: {goal}
|
| 198 |
Fitness Level: {fitness_level}
|
| 199 |
+
Equipment Available: {equipment_list}
|
| 200 |
+
[/INST]"""
|
|
|
|
| 201 |
|
| 202 |
with st.spinner("Generating your AI workout plan..."):
|
| 203 |
|
|
|
|
| 205 |
"inputs": prompt,
|
| 206 |
"parameters": {
|
| 207 |
"max_new_tokens": 600,
|
| 208 |
+
"temperature": 0.3,
|
| 209 |
+
"return_full_text": False
|
| 210 |
}
|
| 211 |
})
|
| 212 |
|
| 213 |
+
if isinstance(output, dict) and "error" in output:
|
| 214 |
+
response = f"⚠️ {output['error']}\n\nPlease try again."
|
| 215 |
+
|
| 216 |
+
elif isinstance(output, list):
|
| 217 |
+
response = output[0]["generated_text"].strip()
|
| 218 |
+
|
| 219 |
+
if "[/INST]" in response:
|
| 220 |
+
response = response.split("[/INST]")[-1].strip()
|
| 221 |
+
|
| 222 |
else:
|
| 223 |
+
response = "Unexpected response from model."
|
| 224 |
|
| 225 |
st.subheader("🏋️ Your Personalized Workout Plan")
|
| 226 |
st.markdown(response)
|