Amrutha04 commited on
Commit
c81e377
·
verified ·
1 Parent(s): 35a61f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -70
app.py CHANGED
@@ -5,98 +5,95 @@ from model_api import query_model
5
  # 1. Page Configuration
6
  st.set_page_config(page_title="FitPlan AI", layout="wide")
7
 
8
- # 2. Custom Styling (Alignment & Space Fix)
9
  st.markdown("""
10
  <style>
11
  .stApp { background: linear-gradient(135deg, #f6d365 0%, #fda085 100%); background-attachment: fixed; }
12
  section[data-testid="stSidebar"] { background-color: #000000 !important; }
13
  section[data-testid="stSidebar"] h1, section[data-testid="stSidebar"] p { color: #FFFFFF !important; }
14
-
15
  h1, h2, h3, p, label { color: #2D3436 !important; font-weight: 600; }
16
-
17
  .result-card {
18
  background-color: white !important; padding: 25px; border-radius: 15px;
19
  border-left: 10px solid #004d57; color: #000000 !important; margin-bottom: 20px;
20
  }
21
-
22
  .day-card {
23
- background-color: #ffffff !important;
24
- padding: 20px 30px !important;
25
- border-radius: 15px;
26
- margin-bottom: 20px;
27
- border-left: 10px solid #004d57;
28
- color: #000000 !important;
29
- white-space: pre-line !important; /* Collapses multiple blank lines into one */
30
- line-height: 1.4 !important; /* Tighter line spacing */
31
- box-shadow: 0 4px 12px rgba(0,0,0,0.08);
32
- width: 100%;
33
- }
34
- /* Force tight margins on any text elements to prevent excessive gapping */
35
- .day-card p, .day-card ul, .day-card li {
36
- margin-top: 2px !important;
37
- margin-bottom: 2px !important;
38
- color: #000000 !important;
39
- text-align: left !important;
40
  }
 
41
  </style>
42
  """, unsafe_allow_html=True)
43
 
44
- # 3. Initialize Session State
 
 
45
  if 'workout_plan' not in st.session_state:
46
  st.session_state.workout_plan = None
47
- if 'assessment_html' not in st.session_state:
48
- st.session_state.assessment_html = None
49
 
50
  # --- SIDEBAR ---
51
  with st.sidebar:
52
  st.markdown("# 👤 Personalised Fitness Plan")
53
  st.write("---")
54
- st.write("Professional AI Coaching.")
55
-
56
- # --- MAIN CONTENT ---
57
- st.title("🏋️ Create Your Fitness Profile")
58
 
59
- col1, col2, col3 = st.columns([2, 1, 1])
60
- with col1: name = st.text_input("Name")
61
- with col2: gender = st.selectbox("Gender", ["Male", "Female", "Other"])
62
- with col3: age = st.number_input("Age", min_value=1, value=19)
63
-
64
- col_h, col_w = st.columns(2)
65
- with col_h: height = st.number_input("Height (cm)", min_value=1.0)
66
- with col_w: weight = st.number_input("Weight (kg)", min_value=1.0)
67
 
68
- goal = st.selectbox("Goal", ["Build Muscle", "Weight Loss", "Strength", "Flexibility"])
69
- level = st.selectbox("Fitness Level", ["Beginner", "Intermediate", "Advanced"])
70
- equip = st.multiselect("Equipment", ["Dumbbells", "Kettlebells", "Pull-up Bar", "Resistance Bands", "Yoga Mat", "No Equipment"])
71
 
72
- if st.button("Submit Profile"):
73
- if not name or height <= 0 or weight <= 0:
74
- st.error("Please fill in all details correctly.")
75
- else:
76
- # Get 4 return values from prompt_builder
77
- prompt, bmi, bmi_status, status_color = build_prompt(name, gender, height, weight, goal, level, equip)
78
 
79
- st.session_state.assessment_html = f"""
80
- <div class="result-card">
81
- <h2>Assessment for {name}</h2>
82
- <p>BMI: <strong>{bmi:.2f}</strong> | Category: <span style="color:{status_color}">{bmi_status}</span></p>
83
- </div>
84
- """
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
- with st.spinner("AI Trainer is generating your plan..."):
87
- raw_workout = query_model(prompt)
88
- if "Error" in raw_workout:
89
- st.error(raw_workout)
90
- else:
91
- # Store the result in session state
92
- st.session_state.workout_plan = raw_workout
 
 
 
 
 
93
 
94
- # --- DISPLAY LOGIC ---
95
- if st.session_state.assessment_html:
96
- st.markdown(st.session_state.assessment_html, unsafe_allow_html=True)
97
 
98
- if st.session_state.workout_plan:
99
- st.markdown("### 📋 Your Personalized Routine")
100
  raw_content = st.session_state.workout_plan
101
  days = ["Day 1:", "Day 2:", "Day 3:", "Day 4:", "Day 5:"]
102
 
@@ -105,17 +102,14 @@ if st.session_state.workout_plan:
105
  if start != -1:
106
  end = raw_content.find(days[i+1]) if i+1 < len(days) else len(raw_content)
107
  day_text = raw_content[start:end].strip()
108
-
109
- # --- THE SPACE-FIXER LOGIC ---
110
- # This cleans out every line that is just whitespace or empty
111
- clean_day_text = "\n".join([line for line in day_text.splitlines() if line.strip()])
112
-
113
- st.markdown(f'<div class="day-card">{clean_day_text}</div>', unsafe_allow_html=True)
114
 
115
  st.write("---")
116
  st.download_button(
117
  label="📥 Download Full Plan",
118
  data=st.session_state.workout_plan,
119
- file_name=f"{name}_Workout_Plan.txt",
120
  mime="text/plain"
121
  )
 
5
  # 1. Page Configuration
6
  st.set_page_config(page_title="FitPlan AI", layout="wide")
7
 
8
+ # 2. Custom Styling (Preserving your exact UI style)
9
  st.markdown("""
10
  <style>
11
  .stApp { background: linear-gradient(135deg, #f6d365 0%, #fda085 100%); background-attachment: fixed; }
12
  section[data-testid="stSidebar"] { background-color: #000000 !important; }
13
  section[data-testid="stSidebar"] h1, section[data-testid="stSidebar"] p { color: #FFFFFF !important; }
 
14
  h1, h2, h3, p, label { color: #2D3436 !important; font-weight: 600; }
 
15
  .result-card {
16
  background-color: white !important; padding: 25px; border-radius: 15px;
17
  border-left: 10px solid #004d57; color: #000000 !important; margin-bottom: 20px;
18
  }
 
19
  .day-card {
20
+ background-color: #ffffff !important; padding: 20px 30px !important;
21
+ border-radius: 15px; margin-bottom: 20px; border-left: 10px solid #004d57;
22
+ color: #000000 !important; white-space: pre-line !important;
23
+ line-height: 1.4 !important; box-shadow: 0 4px 12px rgba(0,0,0,0.08);
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  }
25
+ .day-card p, .day-card ul, .day-card li { margin: 2px 0 !important; text-align: left !important; }
26
  </style>
27
  """, unsafe_allow_html=True)
28
 
29
+ # 3. Initialize Session State for Page Navigation
30
+ if 'page' not in st.session_state:
31
+ st.session_state.page = 'input'
32
  if 'workout_plan' not in st.session_state:
33
  st.session_state.workout_plan = None
34
+ if 'user_data' not in st.session_state:
35
+ st.session_state.user_data = {}
36
 
37
  # --- SIDEBAR ---
38
  with st.sidebar:
39
  st.markdown("# 👤 Personalised Fitness Plan")
40
  st.write("---")
41
+ if st.session_state.page == 'result':
42
+ if st.button("⬅️ New Profile / Back"):
43
+ st.session_state.page = 'input'
44
+ st.rerun()
45
 
46
+ # --- PAGE 1: INPUT FORM ---
47
+ if st.session_state.page == 'input':
48
+ st.title("🏋️ Create Your Fitness Profile")
49
+
50
+ col1, col2, col3 = st.columns([2, 1, 1])
51
+ with col1: name = st.text_input("Name", value=st.session_state.user_data.get('name', ''))
52
+ with col2: gender = st.selectbox("Gender", ["Male", "Female", "Other"])
53
+ with col3: age = st.number_input("Age", min_value=1, value=st.session_state.user_data.get('age', 19))
54
 
55
+ col_h, col_w = st.columns(2)
56
+ with col_h: height = st.number_input("Height (cm)", min_value=1.0, value=st.session_state.user_data.get('height', 170.0))
57
+ with col_w: weight = st.number_input("Weight (kg)", min_value=1.0, value=st.session_state.user_data.get('weight', 65.0))
58
 
59
+ goal = st.selectbox("Goal", ["Build Muscle", "Weight Loss", "Strength", "Flexibility"])
60
+ level = st.selectbox("Fitness Level", ["Beginner", "Intermediate", "Advanced"])
61
+ equip = st.multiselect("Equipment", ["Dumbbells", "Kettlebells", "Pull-up Bar", "Resistance Bands", "Yoga Mat", "No Equipment"])
 
 
 
62
 
63
+ if st.button("Submit Profile"):
64
+ if not name or height <= 50.0 or weight <= 10.0:
65
+ st.error("Please fill in valid details.")
66
+ else:
67
+ with st.spinner("Analyzing profile and generating routine..."):
68
+ prompt, bmi, bmi_status, status_color = build_prompt(name, gender, age, height, weight, goal, level, equip)
69
+ raw_workout = query_model(prompt)
70
+
71
+ if "Error" in raw_workout:
72
+ st.error("API Error. Please check your HF Token.")
73
+ else:
74
+ st.session_state.workout_plan = raw_workout
75
+ st.session_state.assessment_data = {
76
+ "name": name, "bmi": bmi, "status": bmi_status, "color": status_color, "age": age, "gender": gender
77
+ }
78
+ st.session_state.user_data = {"name": name, "age": age, "height": height, "weight": weight}
79
+ st.session_state.page = 'result'
80
+ st.rerun()
81
 
82
+ # --- PAGE 2: RESULTS PAGE ---
83
+ elif st.session_state.page == 'result':
84
+ data = st.session_state.assessment_data
85
+ st.title("📋 Your Personalized Routine")
86
+
87
+ st.markdown(f"""
88
+ <div class="result-card">
89
+ <h2>Assessment for {data['name']}</h2>
90
+ <p>{data['gender']} | {data['age']} Years Old</p>
91
+ <p>BMI: <strong>{data['bmi']:.2f}</strong> | Category: <span style="color:{data['color']}">{data['status']}</span></p>
92
+ </div>
93
+ """, unsafe_allow_html=True)
94
 
95
+
 
 
96
 
 
 
97
  raw_content = st.session_state.workout_plan
98
  days = ["Day 1:", "Day 2:", "Day 3:", "Day 4:", "Day 5:"]
99
 
 
102
  if start != -1:
103
  end = raw_content.find(days[i+1]) if i+1 < len(days) else len(raw_content)
104
  day_text = raw_content[start:end].strip()
105
+ # Clean empty spaces logic
106
+ clean_text = "\n".join([line for line in day_text.splitlines() if line.strip()])
107
+ st.markdown(f'<div class="day-card">{clean_text}</div>', unsafe_allow_html=True)
 
 
 
108
 
109
  st.write("---")
110
  st.download_button(
111
  label="📥 Download Full Plan",
112
  data=st.session_state.workout_plan,
113
+ file_name=f"{data['name']}_Workout_Plan.txt",
114
  mime="text/plain"
115
  )