srbhavya01 commited on
Commit
055c690
Β·
verified Β·
1 Parent(s): 848cecc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -45
app.py CHANGED
@@ -1,82 +1,134 @@
1
  import streamlit as st
 
2
 
3
- st.set_page_config(page_title="FitPlan AI - BMI Calculator", page_icon="πŸ’ͺ")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- st.title("πŸ’ͺ FitPlan AI - Fitness Profile & BMI Calculator")
6
 
7
- st.write("Fill in your details to calculate your BMI and fitness category.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  # -------------------------
10
- # 1. Personal Information
11
  # -------------------------
12
 
13
- name = st.text_input("Enter Your Name *")
 
 
 
 
14
 
15
- height_cm = st.number_input("Enter Height (in centimeters) *", min_value=0.0, format="%.2f")
16
- weight_kg = st.number_input("Enter Weight (in kilograms) *", min_value=0.0, format="%.2f")
17
 
18
  # -------------------------
19
- # 2. Fitness Details
20
  # -------------------------
21
 
22
- st.subheader("Fitness Details")
 
 
 
23
 
24
  goal = st.selectbox(
25
- "Select Your Fitness Goal",
26
  ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"]
27
  )
28
 
29
  equipment = st.multiselect(
30
- "Available Equipment (Select multiple if available)",
31
  ["Dumbbells", "Resistance Band", "Yoga Mat", "Skipping Rope",
32
  "Weight Plates", "Cycling", "Inclined Bench", "Pullups Bar", "No Equipment"]
33
  )
34
 
35
  fitness_level = st.radio(
36
- "Select Your Fitness Level",
37
  ["Beginner", "Intermediate", "Advanced"]
38
  )
39
 
40
  # -------------------------
41
- # BMI Calculation Function
42
  # -------------------------
43
 
44
- def calculate_bmi(weight, height_cm):
45
- height_m = height_cm / 100 # Convert cm to meters
46
- bmi = weight / (height_m ** 2)
47
- return round(bmi, 2)
48
 
49
- def bmi_category(bmi):
50
- if bmi < 18.5:
51
- return "Underweight"
52
- elif 18.5 <= bmi < 24.9:
53
- return "Normal"
54
- elif 25 <= bmi < 29.9:
55
- return "Overweight"
56
  else:
57
- return "Obese"
58
 
59
- # -------------------------
60
- # Submit Button
61
- # -------------------------
 
62
 
63
- if st.button("Calculate BMI"):
64
-
65
- # Validation
66
- if not name or height_cm <= 0 or weight_kg <= 0:
67
- st.error("⚠ Please fill all required fields with valid values!")
68
- else:
69
- bmi = calculate_bmi(weight_kg, height_cm)
70
- category = bmi_category(bmi)
71
 
72
- st.success("βœ… Calculation Successful!")
 
 
 
 
 
73
 
74
- st.write(f"### πŸ‘€ Name: {name}")
75
- st.write(f"### πŸ“Š Your BMI: {bmi}")
76
- st.write(f"### 🏷 BMI Category: {category}")
77
 
78
- st.write("---")
79
- st.write("### πŸ‹ Fitness Summary")
80
- st.write(f"**Goal:** {goal}")
81
- st.write(f"**Fitness Level:** {fitness_level}")
82
- st.write(f"**Equipment Available:** {', '.join(equipment) if equipment else 'None selected'}")
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
 
4
+ # -------------------------
5
+ # Page Config
6
+ # -------------------------
7
+
8
+ st.set_page_config(page_title="FitPlan AI", page_icon="πŸ’ͺ")
9
+
10
+ st.title("πŸ’ͺ FitPlan AI β€” Personalized Workout Generator")
11
+
12
+ # -------------------------
13
+ # BMI Functions
14
+ # -------------------------
15
+
16
+ def calculate_bmi(weight, height):
17
+ height_m = height / 100
18
+ return weight / (height_m ** 2)
19
+
20
+ def bmi_category(bmi):
21
+ if bmi < 18.5:
22
+ return "Underweight"
23
+ elif bmi < 25:
24
+ return "Normal Weight"
25
+ elif bmi < 30:
26
+ return "Overweight"
27
+ else:
28
+ return "Obese"
29
+
30
+ # -------------------------
31
+ # Prompt Builder Function
32
+ # -------------------------
33
+
34
+ def build_prompt(name, gender, height, weight, goal, fitness_level, equipment):
35
+
36
+ bmi = calculate_bmi(weight, height)
37
+ bmi_status = bmi_category(bmi)
38
+
39
+ equipment_list = ", ".join(equipment) if equipment else "No Equipment"
40
+
41
+ prompt = f"""
42
+ You are a certified professional fitness trainer.
43
 
44
+ Create a STRICTLY FORMATTED 5-day personalized workout plan.
45
 
46
+ User Profile:
47
+ Name: {name}
48
+ Gender: {gender}
49
+ Height: {height} cm
50
+ Weight: {weight} kg
51
+ BMI: {bmi:.2f} ({bmi_status})
52
+ Goal: {goal}
53
+ Fitness Level: {fitness_level}
54
+ Available Equipment: {equipment_list}
55
+
56
+ Instructions:
57
+ β€’ Divide into Day 1 to Day 5
58
+ β€’ Include Warm-up
59
+ β€’ Include Exercises with Sets Γ— Reps
60
+ β€’ Include Rest Time
61
+ β€’ Adjust intensity based on BMI
62
+ """
63
+
64
+ return prompt, bmi, bmi_status
65
 
66
  # -------------------------
67
+ # Load Model (Small for HF)
68
  # -------------------------
69
 
70
+ @st.cache_resource
71
+ def load_model():
72
+ tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
73
+ model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
74
+ return tokenizer, model
75
 
76
+ tokenizer, model = load_model()
 
77
 
78
  # -------------------------
79
+ # User Inputs
80
  # -------------------------
81
 
82
+ name = st.text_input("Enter Your Name")
83
+ gender = st.selectbox("Gender", ["Male", "Female", "Other"])
84
+ height = st.number_input("Height (cm)", min_value=0.0)
85
+ weight = st.number_input("Weight (kg)", min_value=0.0)
86
 
87
  goal = st.selectbox(
88
+ "Fitness Goal",
89
  ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"]
90
  )
91
 
92
  equipment = st.multiselect(
93
+ "Available Equipment",
94
  ["Dumbbells", "Resistance Band", "Yoga Mat", "Skipping Rope",
95
  "Weight Plates", "Cycling", "Inclined Bench", "Pullups Bar", "No Equipment"]
96
  )
97
 
98
  fitness_level = st.radio(
99
+ "Fitness Level",
100
  ["Beginner", "Intermediate", "Advanced"]
101
  )
102
 
103
  # -------------------------
104
+ # Generate Plan
105
  # -------------------------
106
 
107
+ if st.button("Generate Workout Plan"):
 
 
 
108
 
109
+ if not name or height <= 0 or weight <= 0:
110
+ st.error("Please fill all required fields")
 
 
 
 
 
111
  else:
 
112
 
113
+ prompt, bmi, bmi_status = build_prompt(
114
+ name, gender, height, weight,
115
+ goal, fitness_level, equipment
116
+ )
117
 
118
+ st.success(f"BMI: {bmi:.2f} ({bmi_status})")
119
+
120
+ with st.spinner("Generating your personalized workout plan..."):
121
+
122
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
 
 
 
123
 
124
+ outputs = model.generate(
125
+ **inputs,
126
+ max_new_tokens=500,
127
+ temperature=0.3,
128
+ do_sample=True
129
+ )
130
 
131
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
132
 
133
+ st.subheader("πŸ‹οΈ Your Workout Plan")
134
+ st.write(result)