js02vel commited on
Commit
2c22b90
·
verified ·
1 Parent(s): 0085977

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +80 -142
src/streamlit_app.py CHANGED
@@ -2,169 +2,107 @@ import streamlit as st
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
  import torch
4
 
5
- # 1. Basic Configuration
 
 
6
  st.set_page_config(page_title="FitPlan AI", page_icon="💪", layout="centered")
7
 
 
 
 
 
8
  def load_model():
9
- tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base")
10
- model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-base")
11
  return tokenizer, model
12
-
13
  tokenizer, model = load_model()
14
- )
15
-
16
- generator = load_model()
17
- # 2. Enhanced CSS
18
- st.markdown("""
19
- <style>
20
- /* Full Page Background */
21
- [data-testid="stAppViewContainer"] {
22
- background: linear-gradient(rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0.75)),
23
- url("https://images.unsplash.com/photo-1517836357463-d25dfeac3438?q=80&w=2070&auto=format&fit=crop");
24
- background-size: cover;
25
- background-position: center;
26
- background-attachment: fixed;
27
- }
28
- /* Hide the "Press Enter to submit form" hint globally */
29
- [data-testid="InputInstructions"] {
30
- display: none !important;
31
- }
32
- /* Form Container Polish */
33
- div[data-testid="stForm"] {
34
- background-color: rgba(255, 255, 255, 0.98) !important;
35
- padding: 3rem !important;
36
- border-radius: 20px !important;
37
- border: none !important;
38
- box-shadow: 0 15px 35px rgba(0,0,0,0.5);
39
- }
40
- /* --- FONT SIZE UPDATES --- */
41
- /* Main Title (FitPlan AI) */
42
- .main-title {
43
- color: #FFFFFF !important;
44
- font-size: 60px !important;
45
- font-weight: 850 !important;
46
- text-align: center;
47
- margin-bottom: 0px;
48
- text-shadow: 3px 3px 6px rgba(0,0,0,0.9);
49
- font-family: 'Helvetica Neue', sans-serif;
50
- }
51
- /* Subtitle (Your personalized gym companion) */
52
- .sub-title {
53
- color: #E0E0E0 !important;
54
- font-size: 24px !important;
55
- text-align: center;
56
- margin-top: -10px;
57
- margin-bottom: 40px;
58
- font-style: italic;
59
- text-shadow: 1px 1px 3px rgba(0,0,0,0.8);
60
- }
61
- /* Form Section Headers (Orange) */
62
- .form-header {
63
- color: #f97316 !important;
64
- font-size: 22px !important;
65
- font-weight: bold !important;
66
- text-transform: uppercase;
67
- letter-spacing: 1px;
68
- }
69
- /* Labels for inputs */
70
- label p {
71
- font-size: 18px !important;
72
- color: #1e293b !important;
73
- font-weight: 600 !important;
74
- }
75
- /* Button Styling */
76
- .stButton > button {
77
- width: 100%;
78
- background: linear-gradient(90deg, #f97316, #ea580c) !important;
79
- color: white !important;
80
- font-size: 20px !important;
81
- font-weight: bold !important;
82
- height: 3.5rem !important;
83
- border-radius: 12px !important;
84
- border: none !important;
85
- margin-top: 20px;
86
- }
87
- </style>
88
- """, unsafe_allow_html=True)
89
 
90
  # --------------------------------------------------
91
- # UI Header Section
92
  # --------------------------------------------------
93
- st.markdown('<p class="main-title">💪 FitPlan AI</p>', unsafe_allow_html=True)
94
- st.markdown('<p class="sub-title">Your personalized gym companion</p>', unsafe_allow_html=True)
95
 
96
  # --------------------------------------------------
97
- # Fitness Profile Form
98
  # --------------------------------------------------
99
- with st.form("fitness_form", clear_on_submit=False):
100
- st.markdown('<p class="form-header">🧍‍♂️ 1. PERSONAL INFORMATION</p>', unsafe_allow_html=True)
101
-
102
- name = st.text_input("Full Name *", placeholder="Enter your full name")
103
 
104
  col1, col2 = st.columns(2)
105
  with col1:
106
- height = st.number_input("Height (cm) *", min_value=0.0, step=1.0, value=0.0)
107
  with col2:
108
- weight = st.number_input("Weight (kg) *", min_value=0.0, step=0.1, value=0.0)
109
-
110
- st.markdown("<br>", unsafe_allow_html=True)
111
- st.markdown('<p class="form-header">🏋️ 2. FITNESS DETAILS</p>', unsafe_allow_html=True)
112
-
113
- goal = st.selectbox("Fitness Goal", ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"])
114
-
115
- equipment = st.multiselect("Available Equipment",
116
- ["Dumbbells", "Resistance Band", "Yoga Mat", "Kettlebell", "Pull-up Bar", "No Equipment"],
117
- default=["No Equipment"])
118
-
119
- level = st.radio("Fitness Level", ["Beginner", "Intermediate", "Advanced"], horizontal=True)
120
-
121
- # SUBMIT BUTTON
122
- if st.button(" Submit Profile"):
123
-
124
- if not name:
 
 
 
 
 
 
 
 
125
  st.error("Please enter your name.")
126
-
127
  elif height <= 0 or weight <= 0:
128
- st.error("Please enter valid height and weight.")
129
-
130
- elif not equipment:
131
- st.error("Please select at least one equipment option.")
132
-
133
  else:
134
- st.success(" Profile Submitted Successfully!")
135
-
136
- bmi_status = bmi_category(bmi)
137
- equipment_list = ", ".join(equipment)
138
-
 
 
 
 
 
 
 
 
 
 
 
 
139
  prompt = f"""
140
- You are a certified professional fitness trainer.
141
-
142
- Create a detailed 5-day workout plan.
143
-
144
- User Information:
145
- - Gender: {gender}
146
- - BMI: {bmi:.2f} ({bmi_status})
147
- - Goal: {goal}
148
- - Fitness Level: {fitness_level}
149
- - Equipment Available: {equipment_list}
150
-
151
- Start directly with:
152
-
153
- Day 1:
154
  """
155
-
156
- with st.spinner("Generating your AI workout plan..."):
157
-
158
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
159
-
160
  outputs = model.generate(
161
  **inputs,
162
- max_new_tokens=900,
163
- temperature=0.7,
164
- do_sample=True
165
  )
166
-
167
- result = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
168
-
169
- st.subheader(" Your Personalized Workout Plan")
170
- st.write(result)
 
2
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
  import torch
4
 
5
+ # --------------------------------------------------
6
+ # Page Config
7
+ # --------------------------------------------------
8
  st.set_page_config(page_title="FitPlan AI", page_icon="💪", layout="centered")
9
 
10
+ # --------------------------------------------------
11
+ # Load Model (CACHED – IMPORTANT)
12
+ # --------------------------------------------------
13
+ @st.cache_resource
14
  def load_model():
15
+ tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
16
+ model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
17
  return tokenizer, model
18
+
19
  tokenizer, model = load_model()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # --------------------------------------------------
22
+ # UI Header
23
  # --------------------------------------------------
24
+ st.title("💪 FitPlan AI")
25
+ st.subheader("AI-powered Fitness Planner")
26
 
27
  # --------------------------------------------------
28
+ # Form
29
  # --------------------------------------------------
30
+ with st.form("fitness_form"):
31
+
32
+ name = st.text_input("Full Name *")
 
33
 
34
  col1, col2 = st.columns(2)
35
  with col1:
36
+ height = st.number_input("Height (cm) *", min_value=0.0)
37
  with col2:
38
+ weight = st.number_input("Weight (kg) *", min_value=0.0)
39
+
40
+ goal = st.selectbox(
41
+ "Fitness Goal",
42
+ ["Build Muscle", "Weight Loss", "Strength Gain", "Abs Building", "Flexible"]
43
+ )
44
+
45
+ equipment = st.multiselect(
46
+ "Available Equipment",
47
+ ["Dumbbells", "Resistance Band", "Yoga Mat", "No Equipment"]
48
+ )
49
+
50
+ level = st.radio(
51
+ "Fitness Level",
52
+ ["Beginner", "Intermediate", "Advanced"]
53
+ )
54
+
55
+ submit = st.form_submit_button("Generate Plan")
56
+
57
+ # --------------------------------------------------
58
+ # Logic
59
+ # --------------------------------------------------
60
+ if submit:
61
+
62
+ if not name.strip():
63
  st.error("Please enter your name.")
 
64
  elif height <= 0 or weight <= 0:
65
+ st.error("Height and weight must be greater than zero.")
 
 
 
 
66
  else:
67
+ # BMI Calculation
68
+ height_m = height / 100
69
+ bmi = round(weight / (height_m ** 2), 2)
70
+
71
+ # BMI Category
72
+ if bmi < 18.5:
73
+ bmi_status = "Underweight"
74
+ elif bmi < 24.9:
75
+ bmi_status = "Normal"
76
+ elif bmi < 29.9:
77
+ bmi_status = "Overweight"
78
+ else:
79
+ bmi_status = "Obese"
80
+
81
+ st.success(f"Profile created for {name}")
82
+ st.write(f"**BMI:** {bmi} ({bmi_status})")
83
+
84
  prompt = f"""
85
+ You are a certified fitness trainer.
86
+
87
+ Create a 5-day workout plan.
88
+
89
+ User details:
90
+ BMI: {bmi} ({bmi_status})
91
+ Goal: {goal}
92
+ Level: {level}
93
+ Equipment: {", ".join(equipment) if equipment else "None"}
94
+
95
+ Start with Day 1.
 
 
 
96
  """
97
+
98
+ with st.spinner("Generating AI workout plan..."):
 
99
  inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
 
100
  outputs = model.generate(
101
  **inputs,
102
+ max_new_tokens=400,
103
+ temperature=0.7
 
104
  )
105
+
106
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True)
107
+ st.subheader("🏋️ Your Workout Plan")
108
+ st.write(result)