Springboardmen commited on
Commit
bf87aeb
Β·
verified Β·
1 Parent(s): c891cee

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +55 -46
src/streamlit_app.py CHANGED
@@ -1,23 +1,32 @@
1
  import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
- import torch
4
 
5
  st.set_page_config(page_title="FitPlan AI", layout="centered")
6
 
7
- # LOAD MODEL
8
- @st.cache_resource
9
- def load_model():
10
- tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
11
- model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
12
- return tokenizer, model
13
 
14
- tokenizer, model = load_model()
 
 
 
15
 
 
 
 
 
 
16
  # TITLE
17
- st.title(" FitPlan AI – User Fitness Profile")
 
 
18
 
 
19
  # PERSONAL INFORMATION
20
- st.subheader(" Personal Information")
 
 
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
- #BMI Function
 
 
 
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
- if st.button(" Submit Profile"):
 
 
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
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
183
-
184
- outputs = model.generate(
185
- **inputs,
186
- max_new_tokens=600,
187
- temperature=0.3,
188
- do_sample=False,
189
- repetition_penalty=1.2
190
- )
191
-
192
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
193
-
 
194
  st.subheader("πŸ‹οΈ Your Personalized Workout Plan")
195
- st.write(response)
 
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)