srbhavya01 commited on
Commit
e32ed83
·
verified ·
1 Parent(s): 7da4ce0

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +67 -131
src/streamlit_app.py CHANGED
@@ -1,92 +1,58 @@
1
- import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
-
4
- # -----------------------------
5
- # PAGE CONFIG
6
- # -----------------------------
7
- st.set_page_config(page_title="FitPlan AI", page_icon="💪", layout="centered")
8
-
9
- # -----------------------------
10
- # BACKGROUND + STYLES
11
- # -----------------------------
12
- st.markdown("""
13
- <style>
14
- .stApp {
15
- background: linear-gradient(to right, #ff512f, #dd2476);
16
- }
17
-
18
- /* Title */
19
- .main-title {
20
- font-size: 40px;
21
- font-weight: bold;
22
- text-align: center;
23
- color: white;
24
- }
25
-
26
- /* Workout Output Box */
27
- .workout-box {
28
- background-color: #111;
29
- padding: 25px;
30
- border-radius: 15px;
31
- border: 1px solid #444;
32
- color: white;
33
- font-size: 18px;
34
- line-height: 1.7;
35
- }
36
-
37
- /* Section Title */
38
- .title-box {
39
- font-size: 28px;
40
- font-weight: bold;
41
- color: #ff7b00;
42
- margin-top: 20px;
43
- }
44
- </style>
45
- """, unsafe_allow_html=True)
46
-
47
- st.markdown('<div class="main-title">💪 FitPlan AI</div>', unsafe_allow_html=True)
48
- st.write("### Fitness Profile & BMI Calculator")
49
-
50
- # -----------------------------
51
- # LOAD MODEL (HUGGING FACE)
52
- # -----------------------------
53
  @st.cache_resource
54
  def load_model():
55
- tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
56
- model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
57
- return tokenizer, model
 
 
 
 
 
58
 
59
- tokenizer, model = load_model()
 
 
60
 
61
- # -----------------------------
62
- # USER INPUT
63
- # -----------------------------
64
  name = st.text_input("Enter Your Name *")
65
- gender = st.selectbox("Gender", ["Male", "Female", "Other"])
66
- height_cm = st.number_input("Height (cm)", min_value=0.0)
67
- weight_kg = st.number_input("Weight (kg)", min_value=0.0)
 
 
 
 
 
 
68
 
69
  st.subheader("Fitness Details")
70
 
71
  goal = st.selectbox(
72
- "Fitness Goal",
73
- ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexibility"]
74
  )
75
 
76
  equipment = st.multiselect(
77
  "Available Equipment",
78
- ["Dumbbells", "Resistance Band", "Yoga Mat", "Skipping Rope",
79
- "Weight Plates", "Cycling", "Inclined Bench", "Pull-up Bar", "No Equipment"]
80
  )
81
 
82
- fitness_level = st.radio("Fitness Level", ["Beginner", "Intermediate", "Advanced"])
 
 
 
 
 
 
 
83
 
84
- # -----------------------------
85
- # BMI FUNCTIONS
86
- # -----------------------------
87
- def calculate_bmi(weight, height):
88
- height_m = height / 100
89
- return round(weight / (height_m ** 2), 2)
90
 
91
  def bmi_category(bmi):
92
  if bmi < 18.5:
@@ -98,20 +64,23 @@ def bmi_category(bmi):
98
  else:
99
  return "Obese"
100
 
101
- # -----------------------------
102
- # SUBMIT BUTTON
103
- # -----------------------------
104
- if st.button("Generate Fitness Plan 💥"):
105
 
 
 
 
106
  if not name:
107
- st.error("Enter your name")
108
  elif height_cm <= 0 or weight_kg <= 0:
109
- st.error("Enter valid height & weight")
110
  elif not equipment:
111
- st.error("Select equipment")
112
  else:
113
- st.success("Profile Submitted Successfully!")
114
 
 
115
  bmi = calculate_bmi(weight_kg, height_cm)
116
  bmi_status = bmi_category(bmi)
117
 
@@ -119,55 +88,22 @@ if st.button("Generate Fitness Plan 💥"):
119
 
120
  equipment_list = ", ".join(equipment)
121
 
122
- # -----------------------------
123
- # PROMPT FOR AI
124
- # -----------------------------
125
  prompt = f"""
126
- Create a STRICT 5-day structured workout plan.
127
-
128
- Format EXACTLY like:
129
-
130
- Day 1:
131
- Warm-up:
132
- Exercises:
133
- Sets & Reps:
134
- Rest:
135
-
136
- Day 2:
137
- ...
138
-
139
- User Details:
140
- Name: {name}
141
- Gender: {gender}
142
- BMI: {bmi} ({bmi_status})
143
- Goal: {goal}
144
- Fitness Level: {fitness_level}
145
- Equipment: {equipment_list}
146
- """
147
-
148
- # -----------------------------
149
- # GENERATE PLAN
150
- # -----------------------------
151
  with st.spinner("Generating your AI workout plan..."):
152
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
153
- outputs = model.generate(
154
- **inputs,
155
- max_new_tokens=600,
156
- temperature=0.7,
157
- do_sample=True
158
- )
159
- result = tokenizer.decode(outputs[0], skip_special_tokens=True)
160
-
161
- # -----------------------------
162
- # DISPLAY OUTPUT (Styled)
163
- # -----------------------------
164
- st.markdown(
165
- '<div class="title-box">🏋️ Your Personalized Workout Plan</div>',
166
- unsafe_allow_html=True
167
- )
168
-
169
- st.markdown(f"""
170
- <div class="workout-box">
171
- {result.replace("Day", "<br><br><b>Day").replace("\n", "<br>")}
172
- </div>
173
- """, unsafe_allow_html=True)
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+
4
+ # Load model only once
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  @st.cache_resource
6
  def load_model():
7
+ return pipeline("text-generation", model="google/flan-t5-base")
8
+
9
+ generator = load_model()
10
+
11
+ st.set_page_config(page_title="FitPlan AI - BMI Calculator", page_icon="💪")
12
+ st.title("💪 FitPlan AI - Fitness Profile & BMI Calculator")
13
+
14
+ st.write("Fill in your details to calculate your BMI and get AI workout plan.")
15
 
16
+ # -------------------------
17
+ # 1. Personal Information
18
+ # -------------------------
19
 
 
 
 
20
  name = st.text_input("Enter Your Name *")
21
+
22
+ gender = st.selectbox("Select Gender", ["Male", "Female", "Other"])
23
+
24
+ height_cm = st.number_input("Enter Height (in centimeters) *", min_value=0.0, format="%.2f")
25
+ weight_kg = st.number_input("Enter Weight (in kilograms) *", min_value=0.0, format="%.2f")
26
+
27
+ # -------------------------
28
+ # 2. Fitness Details
29
+ # -------------------------
30
 
31
  st.subheader("Fitness Details")
32
 
33
  goal = st.selectbox(
34
+ "Select Your Fitness Goal",
35
+ ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"]
36
  )
37
 
38
  equipment = st.multiselect(
39
  "Available Equipment",
40
+ ["Dumbbells", "Resistance Band", "Yoga Mat", "No Equipment"]
 
41
  )
42
 
43
+ fitness_level = st.radio(
44
+ "Fitness Level",
45
+ ["Beginner", "Intermediate", "Advanced"]
46
+ )
47
+
48
+ # -------------------------
49
+ # BMI Functions
50
+ # -------------------------
51
 
52
+ def calculate_bmi(weight, height_cm):
53
+ height_m = height_cm / 100
54
+ bmi = weight / (height_m ** 2)
55
+ return round(bmi, 2)
 
 
56
 
57
  def bmi_category(bmi):
58
  if bmi < 18.5:
 
64
  else:
65
  return "Obese"
66
 
67
+ # -------------------------
68
+ # Submit Button
69
+ # -------------------------
 
70
 
71
+ if st.button("Submit Profile"):
72
+
73
+ # Validation
74
  if not name:
75
+ st.error("Please enter your name.")
76
  elif height_cm <= 0 or weight_kg <= 0:
77
+ st.error("Enter valid height and weight.")
78
  elif not equipment:
79
+ st.error("Select at least one equipment option.")
80
  else:
81
+ st.success("Profile Submitted Successfully!")
82
 
83
+ # Calculate BMI
84
  bmi = calculate_bmi(weight_kg, height_cm)
85
  bmi_status = bmi_category(bmi)
86
 
 
88
 
89
  equipment_list = ", ".join(equipment)
90
 
 
 
 
91
  prompt = f"""
92
+ Generate a 5-day structured workout plan.
93
+
94
+ User Details:
95
+ Name: {name}
96
+ Gender: {gender}
97
+ BMI: {bmi} ({bmi_status})
98
+ Goal: {goal}
99
+ Fitness Level: {fitness_level}
100
+ Available Equipment: {equipment_list}
101
+
102
+ Include warmup, exercises with sets & reps, rest time.
103
+ """
104
+
 
 
 
 
 
 
 
 
 
 
 
 
105
  with st.spinner("Generating your AI workout plan..."):
106
+ result = generator(prompt, max_new_tokens=400)[0]["generated_text"]
107
+
108
+ st.subheader("🏋️ Your Personalized Workout Plan")
109
+ st.write(result)