File size: 6,229 Bytes
1f6d7ba
bf87aeb
1f6d7ba
b8f77e5
1f6d7ba
bf87aeb
 
 
8dedb65
bf87aeb
17b2277
bf87aeb
17b2277
 
bf87aeb
8dedb65
bf87aeb
17b2277
 
 
 
 
 
 
 
 
 
bf87aeb
 
167ab39
bf87aeb
 
 
c704836
bf87aeb
2cf4a4b
bf87aeb
 
 
850b633
7f56c8a
 
444c89f
167ab39
444c89f
167ab39
2cf4a4b
 
167ab39
 
 
bf87aeb
850b633
 
 
167ab39
 
 
 
 
 
 
850b633
 
167ab39
 
 
 
 
 
 
850b633
bf87aeb
 
 
 
8dedb65
 
 
167ab39
8dedb65
167ab39
8dedb65
 
 
850b633
167ab39
 
 
 
 
 
 
 
 
bf87aeb
2cf4a4b
bf87aeb
 
850b633
 
8d6f3e2
167ab39
8d6f3e2
 
 
 
 
 
 
 
 
bf87aeb
167ab39
bf87aeb
89aa247
bf87aeb
444c89f
 
 
6178398
8e4ec5e
6178398
 
 
 
 
 
 
444c89f
6178398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444c89f
8d6f3e2
bf87aeb
8d6f3e2
bf87aeb
 
850b633
8d6f3e2
 
167ab39
8d6f3e2
 
 
 
bf87aeb
e14cd6a
bf87aeb
 
 
8dedb65
e14cd6a
 
55852ba
e14cd6a
 
89aa247
e14cd6a
 
1f3a745
e14cd6a
 
1ecb439
e14cd6a
 
1ecb439
bf87aeb
e14cd6a
ad523fb
 
 
17b2277
 
 
 
 
 
ad523fb
17b2277
ad523fb
 
 
 
17b2277
 
1ecb439
e14cd6a
1ecb439
bf87aeb
 
 
 
17b2277
 
bf87aeb
 
 
17b2277
 
 
 
 
 
 
 
 
bf87aeb
17b2277
bf87aeb
e14cd6a
bf87aeb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import streamlit as st
import requests

st.set_page_config(page_title="FitPlan AI", layout="centered")

# ---------------------------------------------------
# HUGGING FACE API CONFIG
# ---------------------------------------------------

API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"

headers = {
    "Authorization": f"Bearer {st.secrets['HF_TOKEN']}",
    "Content-Type": "application/json"
}

def query(payload):
    try:
        response = requests.post(API_URL, headers=headers, json=payload, timeout=60)

        if response.status_code != 200:
            return {"error": f"API Error {response.status_code}: {response.text}"}

        return response.json()

    except requests.exceptions.RequestException as e:
        return {"error": f"Request failed: {str(e)}"}

# ---------------------------------------------------
# TITLE
# ---------------------------------------------------

st.title("πŸ‹οΈ FitPlan AI – User Fitness Profile")

# ---------------------------------------------------
# PERSONAL INFORMATION
# ---------------------------------------------------

st.subheader("πŸ‘€ Personal Information")

name = st.text_input("Enter Your Name")

gender = st.radio(
    "Gender",
    ["Male", "Female"],
    horizontal=True
)

# ---------------------------------------------------
# HEIGHT & WEIGHT
# ---------------------------------------------------

col1, col2 = st.columns(2)

with col1:
    height = st.number_input(
        "Height (in cm)",
        min_value=0.0,
        max_value=250.0,
        value=0.0,
        step=0.1
    )

with col2:
    weight = st.number_input(
        "Weight (in kg)",
        min_value=0.0,
        max_value=200.0,
        value=0.0,
        step=0.1
    )

# ---------------------------------------------------
# BMI FUNCTION
# ---------------------------------------------------

def bmi_category(bmi):
    if bmi < 18.5:
        return "Underweight"
    elif bmi < 25:
        return "Normal weight"
    elif bmi < 30:
        return "Overweight"
    else:
        return "Obese"

bmi = None

if height > 0 and weight > 0:
    height_m = height / 100
    bmi = weight / (height_m ** 2)

    st.metric("πŸ“Š Your BMI", f"{bmi:.2f}")
    st.info(f"BMI Category: {bmi_category(bmi)}")

# ---------------------------------------------------
# FITNESS GOAL
# ---------------------------------------------------

st.subheader("🎯 Fitness Goal")

goal = st.selectbox(
    "Select Your Goal",
    [
        "Flexible",
        "Weight Loss",
        "Build Muscle",
        "Strength Gaining",
        "Abs Building"
    ]
)

# ---------------------------------------------------
# EQUIPMENT
# ---------------------------------------------------

st.subheader("πŸ‹οΈ Available Equipment")

equipment_map = {}

col1, col2, col3 = st.columns(3)

with col1:
    equipment_map["No Equipment"] = st.checkbox("No Equipment")
    equipment_map["Pull-up Bar"] = st.checkbox("Pull-up Bar")
    equipment_map["Dip Bars"] = st.checkbox("Dip Bars")
    equipment_map["Push-up Handles"] = st.checkbox("Push-up Handles")
    equipment_map["Dumbbells"] = st.checkbox("Dumbbells")
    equipment_map["Adjustable Dumbbells"] = st.checkbox("Adjustable Dumbbells")

with col2:
    equipment_map["Barbell"] = st.checkbox("Barbell")
    equipment_map["Weight Plates"] = st.checkbox("Weight Plates")
    equipment_map["Kettlebells"] = st.checkbox("Kettlebells")
    equipment_map["Medicine Ball"] = st.checkbox("Medicine Ball")
    equipment_map["Yoga Mat"] = st.checkbox("Yoga Mat")
    equipment_map["Resistance Band"] = st.checkbox("Resistance Band")

with col3:
    equipment_map["Bosu Ball"] = st.checkbox("Bosu Ball")
    equipment_map["Stability Ball"] = st.checkbox("Stability Ball")
    equipment_map["Foam Roller"] = st.checkbox("Foam Roller")
    equipment_map["Treadmill"] = st.checkbox("Treadmill")
    equipment_map["Exercise Bike"] = st.checkbox("Exercise Bike")
    equipment_map["Skipping Rope"] = st.checkbox("Skipping Rope")

equipment = [item for item, selected in equipment_map.items() if selected]

# ---------------------------------------------------
# FITNESS LEVEL
# ---------------------------------------------------

st.subheader("πŸ“ˆ Fitness Level")

fitness_level = st.radio(
    "Select Fitness Level",
    ["Beginner", "Intermediate", "Advanced"],
    horizontal=True
)

# ---------------------------------------------------
# SUBMIT BUTTON
# ---------------------------------------------------

if st.button("πŸš€ Submit Profile"):

    if not name:
        st.error("Please enter your name.")

    elif height <= 0 or weight <= 0:
        st.error("Please enter valid height and weight.")

    elif not equipment:
        st.error("Please select at least one equipment option.")

    else:
        st.success("βœ… Profile Submitted Successfully!")

        bmi_status = bmi_category(bmi)
        equipment_list = ", ".join(equipment)

        prompt = f"""<s>[INST]
You are a certified professional fitness trainer.

Generate a structured 5-day workout plan.

Rules:
- Clearly label Day 1 to Day 5
- Include exercises
- Include sets and reps
- Include short rest guidance
- Return only the workout plan

User Details:
Gender: {gender}
BMI: {bmi:.2f} ({bmi_status})
Goal: {goal}
Fitness Level: {fitness_level}
Equipment Available: {equipment_list}
[/INST]"""

        with st.spinner("Generating your AI workout plan..."):

            output = query({
                "inputs": prompt,
                "parameters": {
                    "max_new_tokens": 600,
                    "temperature": 0.3,
                    "return_full_text": False
                }
            })

            if isinstance(output, dict) and "error" in output:
                response = f"⚠️ {output['error']}\n\nPlease try again."

            elif isinstance(output, list):
                response = output[0]["generated_text"].strip()

                if "[/INST]" in response:
                    response = response.split("[/INST]")[-1].strip()

            else:
                response = "Unexpected response from model."

        st.subheader("πŸ‹οΈ Your Personalized Workout Plan")
        st.markdown(response)