Harry2406 commited on
Commit
b748302
·
verified ·
1 Parent(s): 8ec3d26

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +46 -40
src/streamlit_app.py CHANGED
@@ -12,14 +12,35 @@ st.set_page_config(
12
  )
13
 
14
  # =========================
15
- # Load Model
16
  # =========================
 
17
  def load_model():
18
  tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
19
  model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
20
  return tokenizer, model
21
-
22
  tokenizer, model = load_model()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # =========================
24
  # 1️⃣ Personal Information
25
  # =========================
@@ -45,34 +66,14 @@ weight_kg = st.number_input(
45
  )
46
 
47
  # =========================
48
- # BMI Calculation
49
  # =========================
50
- # =========================
51
- # BMI Calculation
52
- # =========================
53
- def calculate_bmi(height_cm, weight_kg):
54
- if height_cm > 0 and weight_kg > 0:
55
- height_m = height_cm / 100
56
- return weight_kg / (height_m ** 2)
57
- return 0 # Always return something
58
 
59
- def bmi_category(bmi):
60
- if bmi < 18.5:
61
- return "Underweight"
62
- elif 18.5 <= bmi < 25:
63
- return "Normal weight"
64
- elif 25 <= bmi < 30:
65
- return "Overweight"
66
- else:
67
- return "Obese"
68
-
69
- # ALWAYS define bmi safely
70
- bmi = 0
71
  if height_cm > 0 and weight_kg > 0:
72
  bmi = calculate_bmi(height_cm, weight_kg)
73
  st.info(f"Your BMI is: {bmi:.2f} ({bmi_category(bmi)})")
74
 
75
-
76
  # =========================
77
  # 2️⃣ Fitness Details
78
  # =========================
@@ -99,16 +100,12 @@ st.markdown("---")
99
  # =========================
100
  # Submit Button
101
  # =========================
102
- # =========================
103
- # Submit Button
104
- # =========================
105
-
106
  if st.button("Submit Profile"):
107
 
108
  if not name:
109
  st.error("Please enter your name.")
110
 
111
- elif height_cm <= 0 or weight_kg <= 0:
112
  st.error("Please enter valid height and weight.")
113
 
114
  elif not equipment:
@@ -121,7 +118,6 @@ if st.button("Submit Profile"):
121
  equipment_list = ", ".join(equipment)
122
 
123
  prompt = f"""
124
- prompt = f"""
125
  You are a certified fitness trainer.
126
 
127
  Create a STRICTLY formatted 5-day workout plan.
@@ -142,22 +138,32 @@ IMPORTANT RULES:
142
  Sets:
143
  Reps:
144
  Rest:
145
- 5. Keep exercises suitable for the fitness level.
146
- 6. Do NOT add explanations, tips, introductions, or conclusions.
147
- 7. Keep formatting clean and consistent.
148
 
149
- OUTPUT FORMAT (FOLLOW EXACTLY):
150
 
151
  Day 1: [Workout Focus]
152
  1. Exercise: Push-Ups
153
  Sets: 3
154
  Reps: 12
155
  Rest: 60 sec
 
156
 
157
- 2. Exercise: Squats
158
- Sets: 3
159
- Reps: 15
160
- Rest: 60 sec
161
 
162
- Continue same format for all 5 days.
163
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  )
13
 
14
  # =========================
15
+ # Load Model (Cached)
16
  # =========================
17
+ @st.cache_resource
18
  def load_model():
19
  tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
20
  model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
21
  return tokenizer, model
22
+
23
  tokenizer, model = load_model()
24
+
25
+ # =========================
26
+ # BMI Functions
27
+ # =========================
28
+ def calculate_bmi(height_cm, weight_kg):
29
+ if height_cm > 0 and weight_kg > 0:
30
+ height_m = height_cm / 100
31
+ return weight_kg / (height_m ** 2)
32
+ return None
33
+
34
+ def bmi_category(bmi):
35
+ if bmi < 18.5:
36
+ return "Underweight"
37
+ elif bmi < 25:
38
+ return "Normal weight"
39
+ elif bmi < 30:
40
+ return "Overweight"
41
+ else:
42
+ return "Obese"
43
+
44
  # =========================
45
  # 1️⃣ Personal Information
46
  # =========================
 
66
  )
67
 
68
  # =========================
69
+ # BMI Display
70
  # =========================
71
+ bmi = None
 
 
 
 
 
 
 
72
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  if height_cm > 0 and weight_kg > 0:
74
  bmi = calculate_bmi(height_cm, weight_kg)
75
  st.info(f"Your BMI is: {bmi:.2f} ({bmi_category(bmi)})")
76
 
 
77
  # =========================
78
  # 2️⃣ Fitness Details
79
  # =========================
 
100
  # =========================
101
  # Submit Button
102
  # =========================
 
 
 
 
103
  if st.button("Submit Profile"):
104
 
105
  if not name:
106
  st.error("Please enter your name.")
107
 
108
+ elif bmi is None:
109
  st.error("Please enter valid height and weight.")
110
 
111
  elif not equipment:
 
118
  equipment_list = ", ".join(equipment)
119
 
120
  prompt = f"""
 
121
  You are a certified fitness trainer.
122
 
123
  Create a STRICTLY formatted 5-day workout plan.
 
138
  Sets:
139
  Reps:
140
  Rest:
141
+ 5. Do NOT add explanations or extra text.
 
 
142
 
143
+ FORMAT:
144
 
145
  Day 1: [Workout Focus]
146
  1. Exercise: Push-Ups
147
  Sets: 3
148
  Reps: 12
149
  Rest: 60 sec
150
+ """
151
 
152
+ with st.spinner("Generating your AI workout plan..."):
 
 
 
153
 
154
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
155
+
156
+ outputs = model.generate(
157
+ **inputs,
158
+ max_new_tokens=700,
159
+ temperature=0.3,
160
+ do_sample=True
161
+ )
162
+
163
+ result = tokenizer.decode(
164
+ outputs[0],
165
+ skip_special_tokens=True
166
+ ).strip()
167
+
168
+ st.subheader("Your Personalized Workout Plan")
169
+ st.write(result)