Shrey0405 commited on
Commit
b72c85c
Β·
verified Β·
1 Parent(s): bf20139

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +49 -39
src/streamlit_app.py CHANGED
@@ -1,6 +1,6 @@
1
  import streamlit as st
2
- import transformers
3
- from transformers import pipeline
4
 
5
  # --- Page Config ---
6
  st.set_page_config(page_title="FitPlan AI", page_icon="πŸ‹", layout="wide")
@@ -23,26 +23,26 @@ if 'user_data' not in st.session_state:
23
  if 'profile_complete' not in st.session_state:
24
  st.session_state.profile_complete = False
25
 
26
- # --- Functions ---
27
  def calculate_bmi(w, h_cm):
28
  if h_cm > 0:
29
  return round(w / ((h_cm / 100) ** 2), 2)
30
- return None
31
 
32
  def bmi_category(bmi):
33
- if bmi is None: return "Unknown"
34
  if bmi < 18.5: return "Underweight"
35
  if bmi < 25: return "Normal"
36
  if bmi < 30: return "Overweight"
37
  return "Obese"
38
 
39
- # - ---
40
  @st.cache_resource
41
  def load_model():
42
- # Using text-generation as per the environment's supported list
43
- return pipeline("text-generation", model="google/flan-t5-base")
 
44
 
45
- generator = load_model()
46
 
47
  # --- Sidebar Navigation ---
48
  with st.sidebar:
@@ -57,29 +57,33 @@ if menu == "πŸ‘€ Profile":
57
  name = st.text_input("Name", value=st.session_state.user_data['name'])
58
  gender = st.selectbox("Gender", ["Male", "Female", "Other"])
59
  col1, col2 = st.columns(2)
60
- u_height = col1.number_input("Height (cm)", min_value=0.0, value=st.session_state.user_data['height'])
61
- u_weight = col2.number_input("Weight (kg)", min_value=0.0, value=st.session_state.user_data['weight'])
62
 
63
  goal = st.selectbox("Fitness Goal", ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"])
64
  equipment = st.multiselect("Equipment", ["Dumbbells", "Barbell", "Kettlebell", "Resistance Band", "Yoga Mat", "Full Gym", "No Equipment"])
65
  fitness_level = st.select_slider("Fitness Level", options=["Beginner", "Intermediate", "Advanced"])
66
 
67
- bmi = calculate_bmi(u_weight, u_height)
68
 
69
- # ---
70
- if st.button("Submit Profile"):
71
 
72
  if not name:
73
  st.error("Please enter your name.")
 
 
 
 
74
  elif not equipment:
75
  st.error("Please select at least one equipment option.")
76
- elif bmi is None:
77
- st.error("Please enter valid height and weight.")
78
  else:
79
- st.success("βœ… Profile Submitted Successfully!")
80
 
 
81
  st.session_state.user_data.update({
82
- 'name': name, 'height': u_height, 'weight': u_weight,
83
  'goal': goal, 'equip': equipment, 'level': fitness_level, 'gender': gender
84
  })
85
  st.session_state.profile_complete = True
@@ -87,31 +91,37 @@ if menu == "πŸ‘€ Profile":
87
  bmi_status = bmi_category(bmi)
88
  equipment_list = ", ".join(equipment)
89
 
90
- # Prompt
91
  prompt = f"""
92
- Generate a 5-day structured workout plan.
93
-
94
- User Details:
95
- Name: {name}
96
- Gender: {gender}
97
- BMI: {bmi:.2f} ({bmi_status})
98
- Goal: {goal}
99
- Fitness Level: {fitness_level}
100
- Available Equipment: {equipment_list}
101
-
102
- Requirements:
103
- - Include warmup
104
- - Include exercises with sets and reps
105
- - Include rest time
106
- - Adjust intensity based on BMI and fitness level
107
- - Keep it structured day-wise
108
- """
109
 
110
  with st.spinner("Generating your AI workout plan..."):
111
 
112
- result = generator(prompt, max_new_tokens=400)[0]["generated_text"]
 
 
 
 
 
 
 
 
 
113
 
114
- st.subheader("πŸ‹οΈ Your Personalized Workout Plan")
115
  st.write(result)
116
 
117
  elif menu == "πŸ“Š Dashboard":
@@ -123,4 +133,4 @@ elif menu == "πŸ“Š Dashboard":
123
  st.title(f"Welcome, {ud['name']}!")
124
  st.metric("Current BMI", f"{bmi}")
125
  st.write(f"**Current Goal:** {ud['goal']}")
126
- st.write(f"**Equipment:** {', '.join(ud['equip'])}")
 
1
  import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
+ import torch
4
 
5
  # --- Page Config ---
6
  st.set_page_config(page_title="FitPlan AI", page_icon="πŸ‹", layout="wide")
 
23
  if 'profile_complete' not in st.session_state:
24
  st.session_state.profile_complete = False
25
 
26
+ # --- Helper Functions ---
27
  def calculate_bmi(w, h_cm):
28
  if h_cm > 0:
29
  return round(w / ((h_cm / 100) ** 2), 2)
30
+ return 0
31
 
32
  def bmi_category(bmi):
 
33
  if bmi < 18.5: return "Underweight"
34
  if bmi < 25: return "Normal"
35
  if bmi < 30: return "Overweight"
36
  return "Obese"
37
 
38
+
39
  @st.cache_resource
40
  def load_model():
41
+ tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base")
42
+ model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
43
+ return tokenizer, model
44
 
45
+ tokenizer, model = load_model()
46
 
47
  # --- Sidebar Navigation ---
48
  with st.sidebar:
 
57
  name = st.text_input("Name", value=st.session_state.user_data['name'])
58
  gender = st.selectbox("Gender", ["Male", "Female", "Other"])
59
  col1, col2 = st.columns(2)
60
+ height = col1.number_input("Height (cm)", min_value=0.0, value=st.session_state.user_data['height'])
61
+ weight = col2.number_input("Weight (kg)", min_value=0.0, value=st.session_state.user_data['weight'])
62
 
63
  goal = st.selectbox("Fitness Goal", ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"])
64
  equipment = st.multiselect("Equipment", ["Dumbbells", "Barbell", "Kettlebell", "Resistance Band", "Yoga Mat", "Full Gym", "No Equipment"])
65
  fitness_level = st.select_slider("Fitness Level", options=["Beginner", "Intermediate", "Advanced"])
66
 
67
+ bmi = calculate_bmi(weight, height)
68
 
69
+
70
+ if st.button(" Submit Profile"):
71
 
72
  if not name:
73
  st.error("Please enter your name.")
74
+
75
+ elif height <= 0 or weight <= 0:
76
+ st.error("Please enter valid height and weight.")
77
+
78
  elif not equipment:
79
  st.error("Please select at least one equipment option.")
80
+
 
81
  else:
82
+ st.success(" Profile Submitted Successfully!")
83
 
84
+ # Syncing data for dashboard
85
  st.session_state.user_data.update({
86
+ 'name': name, 'height': height, 'weight': weight,
87
  'goal': goal, 'equip': equipment, 'level': fitness_level, 'gender': gender
88
  })
89
  st.session_state.profile_complete = True
 
91
  bmi_status = bmi_category(bmi)
92
  equipment_list = ", ".join(equipment)
93
 
 
94
  prompt = f"""
95
+ You are a certified professional fitness trainer.
96
+
97
+ Create a detailed 5-day workout plan.
98
+
99
+ User Information:
100
+ - Gender: {gender}
101
+ - BMI: {bmi:.2f} ({bmi_status})
102
+ - Goal: {goal}
103
+ - Fitness Level: {fitness_level}
104
+ - Equipment Available: {equipment_list}
105
+
106
+ Start directly with:
107
+
108
+ Day 1:
109
+ """
 
 
110
 
111
  with st.spinner("Generating your AI workout plan..."):
112
 
113
+ inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
114
+
115
+ outputs = model.generate(
116
+ **inputs,
117
+ max_new_tokens=900,
118
+ temperature=0.7,
119
+ do_sample=True
120
+ )
121
+
122
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
123
 
124
+ st.subheader(" Your Personalized Workout Plan")
125
  st.write(result)
126
 
127
  elif menu == "πŸ“Š Dashboard":
 
133
  st.title(f"Welcome, {ud['name']}!")
134
  st.metric("Current BMI", f"{bmi}")
135
  st.write(f"**Current Goal:** {ud['goal']}")
136
+ st.write(f"**Equipment Available:** {', '.join(ud['equip'])}")