Renukalaxmi commited on
Commit
8ea0ea1
Β·
verified Β·
1 Parent(s): dff4ac7

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +48 -107
src/streamlit_app.py CHANGED
@@ -1,144 +1,85 @@
1
  import streamlit as st
2
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
  import torch
4
 
5
  # -----------------------------
6
- # LOAD MODEL (Stable Version)
7
  # -----------------------------
8
  @st.cache_resource
9
  def load_model():
10
- tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
11
- model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large")
12
- return tokenizer, model
 
 
 
13
 
14
- tokenizer, model = load_model()
15
 
16
  # -----------------------------
17
- # PAGE CONFIG
18
  # -----------------------------
19
- st.set_page_config(
20
- page_title="FitPlan AI",
21
- page_icon="πŸ’ͺ",
22
- layout="centered"
23
- )
24
-
25
- st.title("πŸ’ͺ FitPlan AI – User Fitness Profile")
26
-
27
- # -----------------------------
28
- # BMI FUNCTION
29
- # -----------------------------
30
- def calculate_bmi(height_cm, weight_kg):
31
- height_m = height_cm / 100
32
- return weight_kg / (height_m ** 2)
33
-
34
- def bmi_category(bmi):
35
- if bmi < 18.5:
36
- return "Underweight"
37
- elif bmi < 25:
38
- return "Normal Weight"
39
- elif bmi < 30:
40
- return "Overweight"
41
- else:
42
- return "Obese"
43
-
44
- # -----------------------------
45
- # PERSONAL INFORMATION
46
- # -----------------------------
47
- st.header("πŸ‘€ Personal Information")
48
-
49
- name = st.text_input("Enter Your Name *")
50
 
51
- height_cm = st.number_input(
52
- "Height (cm) *",
53
- min_value=1.0,
54
- step=0.1
55
- )
56
-
57
- weight_kg = st.number_input(
58
- "Weight (kg) *",
59
- min_value=1.0,
60
- step=0.1
61
- )
62
 
63
- gender = st.selectbox(
64
- "Gender",
65
- ["Male", "Female", "Other"]
66
  )
67
 
68
- # -----------------------------
69
- # FITNESS DETAILS
70
- # -----------------------------
71
- st.header("πŸ‹οΈ Fitness Details")
72
-
73
- fitness_goal = st.selectbox(
74
  "Fitness Goal",
75
- ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexibility"]
76
  )
77
 
78
- equipment = st.selectbox(
79
- "Available Equipment",
80
- ["Dumbbells", "Resistance Band", "Yoga Mat", "No Equipment"]
81
- )
82
-
83
- fitness_level = st.radio(
84
  "Fitness Level",
85
  ["Beginner", "Intermediate", "Advanced"]
86
  )
87
 
 
 
 
 
 
88
  # -----------------------------
89
- # SUBMIT BUTTON
90
  # -----------------------------
91
- if st.button("Submit Profile"):
92
-
93
- if not name:
94
- st.error("Please enter your name.")
95
-
96
- elif height_cm <= 0 or weight_kg <= 0:
97
- st.error("Please enter valid height and weight.")
98
 
99
- else:
100
- st.success("Profile Submitted Successfully!")
101
-
102
- # Calculate BMI
103
- bmi = calculate_bmi(height_cm, weight_kg)
104
- bmi_status = bmi_category(bmi)
105
-
106
- # -----------------------------
107
- # AI PROMPT
108
- # -----------------------------
109
- prompt = f"""
110
  You are a certified professional fitness trainer.
111
 
112
  Create a structured 5-day workout plan.
113
 
114
  User Details:
115
- Name: {name}
116
  Gender: {gender}
117
  BMI: {bmi:.2f} ({bmi_status})
118
- Goal: {fitness_goal}
119
  Fitness Level: {fitness_level}
120
- Equipment: {equipment}
121
-
122
- Include:
123
- - Warmup
124
- - Exercises with sets and reps
125
- - Rest time
126
- - Structured day-wise format
127
 
128
- Start directly with:
 
 
 
 
 
 
129
 
130
- Day 1:
131
  """
132
 
133
- with st.spinner("Generating your AI workout plan..."):
134
- inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
135
- outputs = model.generate(
136
- **inputs,
137
- max_new_tokens=600,
138
- temperature=0.7,
139
- do_sample=True
140
- )
141
- result = tokenizer.decode(outputs[0], skip_special_tokens=True)
142
-
143
- st.subheader("πŸ‹οΈ Your Personalized Workout Plan")
144
- st.write(result)
 
1
  import streamlit as st
2
+ from transformers import pipeline
3
  import torch
4
 
5
  # -----------------------------
6
+ # LOAD MODEL (Correct Pipeline)
7
  # -----------------------------
8
  @st.cache_resource
9
  def load_model():
10
+ generator = pipeline(
11
+ "text2text-generation", # βœ… Correct for FLAN-T5
12
+ model="google/flan-t5-base",
13
+ device=0 if torch.cuda.is_available() else -1
14
+ )
15
+ return generator
16
 
17
+ generator = load_model()
18
 
19
  # -----------------------------
20
+ # STREAMLIT UI
21
  # -----------------------------
22
+ st.title("πŸ‹οΈ AI Fitness Workout Planner")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ gender = st.selectbox("Select Gender", ["Male", "Female"])
25
+ bmi = st.number_input("Enter BMI", min_value=10.0, max_value=50.0, value=22.0)
 
 
 
 
 
 
 
 
 
26
 
27
+ bmi_status = st.selectbox(
28
+ "BMI Status",
29
+ ["Underweight", "Normal Weight", "Overweight", "Obese"]
30
  )
31
 
32
+ goal = st.selectbox(
 
 
 
 
 
33
  "Fitness Goal",
34
+ ["Weight Loss", "Muscle Gain", "General Fitness", "Strength Training"]
35
  )
36
 
37
+ fitness_level = st.selectbox(
 
 
 
 
 
38
  "Fitness Level",
39
  ["Beginner", "Intermediate", "Advanced"]
40
  )
41
 
42
+ equipment_list = st.text_input(
43
+ "Available Equipment (comma separated)",
44
+ "Dumbbells, Yoga Mat"
45
+ )
46
+
47
  # -----------------------------
48
+ # GENERATE WORKOUT PLAN
49
  # -----------------------------
50
+ if st.button("Generate Workout Plan"):
 
 
 
 
 
 
51
 
52
+ prompt = f"""
 
 
 
 
 
 
 
 
 
 
53
  You are a certified professional fitness trainer.
54
 
55
  Create a structured 5-day workout plan.
56
 
57
  User Details:
 
58
  Gender: {gender}
59
  BMI: {bmi:.2f} ({bmi_status})
60
+ Goal: {goal}
61
  Fitness Level: {fitness_level}
62
+ Equipment: {equipment_list}
 
 
 
 
 
 
63
 
64
+ Rules:
65
+ - Exactly 5 days (Day 1 to Day 5)
66
+ - 4 to 6 exercises per day
67
+ - Include Sets, Reps, Rest
68
+ - Vary exercises based on user details
69
+ - No explanation
70
+ - Only workout plan
71
 
72
+ Start directly from Day 1.
73
  """
74
 
75
+ with st.spinner("Generating personalized workout plan..."):
76
+ result = generator(
77
+ prompt,
78
+ max_length=700,
79
+ temperature=0.9, # βœ… Higher = more variation
80
+ do_sample=True
81
+ )
82
+ workout_plan = result[0]["generated_text"]
83
+
84
+ st.subheader("πŸ“… Your Personalized 5-Day Workout Plan")
85
+ st.text_area("", workout_plan, height=650)