srbhavya01 commited on
Commit
3f25a6a
·
verified ·
1 Parent(s): 562c12e

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +74 -54
src/streamlit_app.py CHANGED
@@ -1,34 +1,34 @@
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",
@@ -46,56 +46,76 @@ fitness_level = st.radio(
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:
59
- return "Underweight"
60
- elif bmi < 24.9:
61
- return "Normal"
62
- elif bmi < 29.9:
63
- return "Overweight"
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
-
87
- st.write(f"### 📊 BMI: {bmi} ({bmi_status})")
88
 
89
- equipment_list = ", ".join(equipment)
90
 
91
- prompt = f"""
92
- Generate a 5-day structured workout plan.
93
 
94
- Include warmup, exercises with sets & reps, rest time.
95
- """
 
 
 
 
96
 
97
- with st.spinner("Generating your AI workout plan..."):
98
- result = generator(prompt, max_new_tokens=400)[0]["generated_text"]
99
 
100
- st.subheader("🏋️ Your Personalized Workout Plan")
101
  st.write(result)
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
+ import torch
4
 
5
+ # -------------------------
6
+ # Page Config
7
+ # -------------------------
 
8
 
9
+ st.set_page_config(page_title="FitPlan AI", page_icon="💪")
10
 
11
+ st.title("💪 FitPlan AI Personalized Workout Generator")
 
12
 
13
+ st.write("Fill your details to generate AI workout plan.")
14
 
15
  # -------------------------
16
+ # Load Model (Small for HF Spaces)
17
  # -------------------------
18
 
19
+ @st.cache_resource
20
+ def load_model():
21
+ tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
22
+ model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
23
+ return tokenizer, model
24
 
25
+ tokenizer, model = load_model()
 
26
 
27
  # -------------------------
28
+ # User Inputs
29
  # -------------------------
30
 
31
+ name = st.text_input("Enter Your Name")
32
 
33
  goal = st.selectbox(
34
  "Select Your Fitness Goal",
 
46
  )
47
 
48
  # -------------------------
49
+ # Function: Create Prompt
50
  # -------------------------
51
 
52
+ def create_prompt(goal, equipment, fitness_level):
53
+
54
+ equipment_list = ", ".join(equipment)
55
+
56
+ # Goal instructions
57
+ goal_map = {
58
+ "Build Muscle": "Focus on muscle hypertrophy training.",
59
+ "Weight Loss": "Focus on fat burning and cardio workouts.",
60
+ "Strength Gain": "Focus on heavy compound strength exercises.",
61
+ "Abs Building": "Focus on core and abdominal exercises.",
62
+ "Flexible": "Focus on stretching and mobility exercises."
63
+ }
64
+
65
+ # Level instructions
66
+ level_map = {
67
+ "Beginner": "Use simple low intensity exercises.",
68
+ "Intermediate": "Use moderate intensity exercises.",
69
+ "Advanced": "Use high intensity advanced exercises."
70
+ }
71
+
72
+ prompt = f"""
73
+ Generate a 5-day workout plan.
74
+
75
+ Goal: {goal}
76
+ Fitness Level: {fitness_level}
77
+ Equipment: {equipment_list}
78
+
79
+ Goal Instruction:
80
+ {goal_map[goal]}
81
+
82
+ Level Instruction:
83
+ {level_map[fitness_level]}
84
+
85
+ Requirements:
86
+ - Include warm-up
87
+ - Exercises with sets and reps
88
+ - Rest time
89
+ - Day-wise structure
90
+ """
91
+
92
+ return prompt
93
 
94
  # -------------------------
95
+ # Generate Plan
96
  # -------------------------
97
 
98
+ if st.button("Generate Workout Plan"):
99
 
 
100
  if not name:
101
+ st.error("Enter your name")
 
 
102
  elif not equipment:
103
+ st.error("Select at least one equipment")
104
  else:
105
+ prompt = create_prompt(goal, equipment, fitness_level)
 
 
 
 
 
 
106
 
107
+ with st.spinner("Generating your AI workout plan..."):
108
 
109
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
 
110
 
111
+ outputs = model.generate(
112
+ **inputs,
113
+ max_new_tokens=300,
114
+ temperature=0.7,
115
+ do_sample=True
116
+ )
117
 
118
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
119
 
120
+ st.success(f"Workout Plan for {name}")
121
  st.write(result)