srbhavya01 commited on
Commit
dbe4a2d
·
verified ·
1 Parent(s): 1ffc411

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +51 -136
src/streamlit_app.py CHANGED
@@ -1,168 +1,83 @@
1
- import streamlit as st
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
- import torch
4
- import time
5
 
6
  # -------------------------
7
- # Page Config
8
  # -------------------------
9
 
10
- st.set_page_config(page_title="FitPlan AI", page_icon="💪")
 
 
 
11
 
12
- # -------------------------
13
- # Load Model
14
- # -------------------------
15
 
16
- @st.cache_resource
17
- def load_model():
18
- tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
19
- model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
20
- return tokenizer, model
21
 
22
- tokenizer, model = load_model()
 
 
 
 
 
23
 
24
- # -------------------------
25
- # Title
26
- # -------------------------
27
 
28
- st.title("💪 FitPlan AI - BMI Calculator & Workout Planner")
29
 
30
  # -------------------------
31
- # Personal Info
32
  # -------------------------
33
 
34
- name = st.text_input("Enter Your Name *")
35
- gender = st.selectbox("Gender", ["Male", "Female", "Other"])
36
- height_cm = st.number_input("Height (cm)", min_value=0.0)
37
- weight_kg = st.number_input("Weight (kg)", min_value=0.0)
 
38
 
39
  # -------------------------
40
- # Fitness Details
41
  # -------------------------
42
 
43
- goal = st.selectbox(
44
- "Fitness Goal",
45
- ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"]
46
- )
47
 
48
- equipment = st.multiselect(
49
- "Available Equipment",
50
- ["Dumbbells", "Resistance Band", "Yoga Mat", "Skipping Rope",
51
- "Weight Plates", "Cycling", "Inclined Bench", "Pullups Bar", "No Equipment"]
52
- )
53
 
54
- fitness_level = st.radio(
55
- "Fitness Level",
56
- ["Beginner", "Intermediate", "Advanced"]
57
- )
 
 
58
 
59
- # -------------------------
60
- # BMI Functions
61
- # -------------------------
62
 
63
- def calculate_bmi(weight, height):
64
- height_m = height / 100
65
- return round(weight / (height_m ** 2), 2)
66
 
67
- def bmi_category(bmi):
68
- if bmi < 18.5:
69
- return "Underweight"
70
- elif bmi < 24.9:
71
- return "Normal"
72
- elif bmi < 29.9:
73
- return "Overweight"
74
- else:
75
- return "Obese"
76
 
77
  # -------------------------
78
- # Generate Plan
79
  # -------------------------
80
 
81
- if st.button("Generate Workout Plan"):
82
-
83
- # Validation
84
- if not name:
85
- st.error("Enter your name")
86
- elif height_cm <= 0 or weight_kg <= 0:
87
- st.error("Enter valid height & weight")
88
- elif not equipment:
89
- st.error("Select equipment")
90
- else:
91
-
92
- # BMI
93
- bmi = calculate_bmi(weight_kg, height_cm)
94
- bmi_status = bmi_category(bmi)
95
-
96
- st.success(f"✅ Welcome {name}! BMI: {bmi} ({bmi_status})")
97
-
98
- # -------------------------
99
- # Progress Bar Animation
100
- # -------------------------
101
-
102
- progress = st.progress(0)
103
- status = st.empty()
104
-
105
- for i in range(100):
106
- time.sleep(0.01)
107
- progress.progress(i + 1)
108
- status.text("Generating personalized fitness plan...")
109
-
110
- st.divider()
111
-
112
- # -------------------------
113
- # Core AI Module Layout
114
- # -------------------------
115
-
116
- st.subheader("🧠 Core AI Inference Module")
117
-
118
- col1, col2, col3 = st.columns(3)
119
-
120
- with col1:
121
- st.info("🔽 Input Layer\nUser goals, equipment, fitness level")
122
-
123
- with col2:
124
- st.info("⚙ Processing\nAI model analysis")
125
-
126
- with col3:
127
- st.info("📤 Output Layer\nWorkout plan")
128
-
129
- st.divider()
130
-
131
- # -------------------------
132
- # Model Input & Output
133
- # -------------------------
134
-
135
- equipment_list = ", ".join(equipment)
136
-
137
- prompt = f"""
138
- Create a structured 5-day workout plan.
139
-
140
- User:
141
- Goal: {goal}
142
- Fitness Level: {fitness_level}
143
- Equipment: {equipment_list}
144
-
145
- Include exercises with sets, reps, and rest time.
146
- """
147
 
148
- with st.spinner("AI is creating your plan..."):
149
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
150
- outputs = model.generate(
151
- **inputs,
152
- max_new_tokens=400,
153
- temperature=0.7,
154
- do_sample=True
155
- )
156
- result = tokenizer.decode(outputs[0], skip_special_tokens=True)
157
 
158
- col4, col5 = st.columns(2)
159
 
160
- with col4:
161
- st.subheader("📥 Model Input")
162
- st.write(prompt)
163
 
164
- with col5:
165
- st.subheader("📤 Model Output")
166
- st.write(result)
167
 
168
- st.success("🏋️ Your Personalized Workout Plan Ready!")
 
 
 
1
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
 
 
2
 
3
  # -------------------------
4
+ # 1. Function to Craft Prompt
5
  # -------------------------
6
 
7
+ def create_workout_prompt(days, level, goal, equipment):
8
+ """
9
+ Creates a detailed prompt for the AI model
10
+ """
11
 
12
+ prompt = f"""
13
+ Generate a {days}-day workout plan.
 
14
 
15
+ User Details:
16
+ Fitness Level: {level}
17
+ Goal: {goal}
18
+ Available Equipment: {equipment}
 
19
 
20
+ Requirements:
21
+ - Include warm-up
22
+ - Include exercises with sets and reps
23
+ - Include rest time
24
+ - Keep it structured day-wise
25
+ """
26
 
27
+ return prompt
 
 
28
 
 
29
 
30
  # -------------------------
31
+ # 2. Load Model
32
  # -------------------------
33
 
34
+ def load_model():
35
+ tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
36
+ model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
37
+ return tokenizer, model
38
+
39
 
40
  # -------------------------
41
+ # 3. Generate Response
42
  # -------------------------
43
 
44
+ def generate_workout_plan(prompt, tokenizer, model):
 
 
 
45
 
46
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
 
 
 
 
47
 
48
+ outputs = model.generate(
49
+ **inputs,
50
+ max_new_tokens=300,
51
+ temperature=0.7,
52
+ do_sample=True
53
+ )
54
 
55
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
56
 
57
+ return result
 
 
58
 
 
 
 
 
 
 
 
 
 
59
 
60
  # -------------------------
61
+ # 4. Example Usage
62
  # -------------------------
63
 
64
+ if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
+ # Create prompt from user input
67
+ prompt = create_workout_prompt(
68
+ days=5,
69
+ level="Beginner",
70
+ goal="Build Muscle",
71
+ equipment="Dumbbells"
72
+ )
 
 
73
 
74
+ print("Generated Prompt:\n", prompt)
75
 
76
+ # Load model
77
+ tokenizer, model = load_model()
 
78
 
79
+ # Generate text response
80
+ workout_plan = generate_workout_plan(prompt, tokenizer, model)
 
81
 
82
+ print("\nAI Workout Plan:\n")
83
+ print(workout_plan)